utils_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package detect
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  6. "github.com/zricethezav/gitleaks/v8/report"
  7. )
  8. func Test_createScmLink(t *testing.T) {
  9. tests := map[string]struct {
  10. platform scm.Platform
  11. url string
  12. finding report.Finding
  13. want string
  14. }{
  15. // None
  16. "no platform": {
  17. platform: scm.NoPlatform,
  18. want: "",
  19. },
  20. // GitHub
  21. "github - single line": {
  22. platform: scm.GitHubPlatform,
  23. url: "https://github.com/gitleaks/test",
  24. finding: report.Finding{
  25. Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
  26. File: "metrics/% of sales/.env",
  27. StartLine: 25,
  28. EndLine: 25,
  29. },
  30. want: "https://github.com/gitleaks/test/blob/20553ad96a4a080c94a54d677db97eed8ce2560d/metrics/%25%20of%20sales/.env#L25",
  31. },
  32. "github - multi line": {
  33. platform: scm.GitHubPlatform,
  34. url: "https://github.com/gitleaks/test",
  35. finding: report.Finding{
  36. Commit: "7bad9f7654cf9701b62400281748c0e8efd97666",
  37. File: "config.json",
  38. StartLine: 235,
  39. EndLine: 238,
  40. },
  41. want: "https://github.com/gitleaks/test/blob/7bad9f7654cf9701b62400281748c0e8efd97666/config.json#L235-L238",
  42. },
  43. "github - markdown": {
  44. platform: scm.GitHubPlatform,
  45. url: "https://github.com/gitleaks/test",
  46. finding: report.Finding{
  47. Commit: "1fc8961d172f39ffb671766e472aa76f8d713e87",
  48. File: "docs/guides/ecosystem/discordjs.MD",
  49. StartLine: 34,
  50. EndLine: 34,
  51. },
  52. want: "https://github.com/gitleaks/test/blob/1fc8961d172f39ffb671766e472aa76f8d713e87/docs/guides/ecosystem/discordjs.MD?plain=1#L34",
  53. },
  54. "github - jupyter notebook": {
  55. platform: scm.GitHubPlatform,
  56. url: "https://github.com/gitleaks/test",
  57. finding: report.Finding{
  58. Commit: "8f56bd2369595bcadbb007e88ba294630fb05c7b",
  59. File: "Cloud/IPYNB/Overlapping Recommendation algorithm _OCuLaR_.ipynb",
  60. StartLine: 293,
  61. EndLine: 293,
  62. },
  63. want: "https://github.com/gitleaks/test/blob/8f56bd2369595bcadbb007e88ba294630fb05c7b/Cloud/IPYNB/Overlapping%20Recommendation%20algorithm%20_OCuLaR_.ipynb?plain=1#L293",
  64. },
  65. // GitLab
  66. "gitlab - single line": {
  67. platform: scm.GitLabPlatform,
  68. url: "https://gitlab.com/example-org/example-group/gitleaks",
  69. finding: report.Finding{
  70. Commit: "213ffd1c9bfa906eb4c7731771132c58a4ca0139",
  71. File: ".gitlab-ci.yml",
  72. StartLine: 41,
  73. EndLine: 41,
  74. },
  75. want: "https://gitlab.com/example-org/example-group/gitleaks/blob/213ffd1c9bfa906eb4c7731771132c58a4ca0139/.gitlab-ci.yml#L41",
  76. },
  77. "gitlab - multi line": {
  78. platform: scm.GitLabPlatform,
  79. url: "https://gitlab.com/example-org/example-group/gitleaks",
  80. finding: report.Finding{
  81. Commit: "63410f74e23a4e51e1f60b9feb073b5d325af878",
  82. File: ".vscode/launchSettings.json",
  83. StartLine: 6,
  84. EndLine: 8,
  85. },
  86. want: "https://gitlab.com/example-org/example-group/gitleaks/blob/63410f74e23a4e51e1f60b9feb073b5d325af878/.vscode/launchSettings.json#L6-8",
  87. },
  88. }
  89. for name, tt := range tests {
  90. t.Run(name, func(t *testing.T) {
  91. actual := createScmLink(tt.platform, tt.url, tt.finding)
  92. assert.Equal(t, tt.want, actual)
  93. })
  94. }
  95. }