hosts_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package hosts
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/zricethezav/gitleaks/config"
  6. "github.com/zricethezav/gitleaks/manager"
  7. "github.com/zricethezav/gitleaks/options"
  8. "os"
  9. "testing"
  10. )
  11. var (
  12. integration = flag.Bool("integration", false, "run github/gitlab integration test")
  13. )
  14. func TestGithub(t *testing.T) {
  15. flag.Parse()
  16. if !*integration {
  17. fmt.Println("skipping github integration tests")
  18. return
  19. }
  20. if os.Getenv("GITHUB_TOKEN") == "" {
  21. t.Log("skipping github integration tests, need env var GITLAB_TOKEN")
  22. return
  23. }
  24. tests := []struct {
  25. opts options.Options
  26. desiredLeaks int
  27. }{
  28. {
  29. opts: options.Options{
  30. Host: "github",
  31. User: "gitleakstest",
  32. AccessToken: os.Getenv("GITHUB_TOKEN"),
  33. },
  34. desiredLeaks: 2,
  35. },
  36. {
  37. opts: options.Options{
  38. Host: "github",
  39. PullRequest: "https://github.com/gitleakstest/gronit/pull/1",
  40. AccessToken: os.Getenv("GITHUB_TOKEN"),
  41. },
  42. desiredLeaks: 4,
  43. },
  44. }
  45. for _, test := range tests {
  46. cfg, err := config.NewConfig(test.opts)
  47. if err != nil {
  48. t.Error(err)
  49. }
  50. m, err := manager.NewManager(test.opts, cfg)
  51. if err != nil {
  52. t.Error(err)
  53. }
  54. err = Run(m)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if test.desiredLeaks != len(m.GetLeaks()) {
  59. t.Errorf("got %d leaks, want %d", len(m.GetLeaks()), test.desiredLeaks)
  60. }
  61. }
  62. }
  63. func TestGitlab(t *testing.T) {
  64. flag.Parse()
  65. if !*integration {
  66. fmt.Println("skipping gitlab integration tests")
  67. return
  68. }
  69. if os.Getenv("GITLAB_TOKEN") == "" {
  70. t.Log("skipping github integration tests, need env var GITLAB_TOKEN")
  71. return
  72. }
  73. tests := []struct {
  74. opts options.Options
  75. desiredLeaks int
  76. }{
  77. {
  78. opts: options.Options{
  79. Host: "gitlab",
  80. User: "gitleakstest",
  81. AccessToken: os.Getenv("GITLAB_TOKEN"),
  82. },
  83. desiredLeaks: 2,
  84. },
  85. }
  86. for _, test := range tests {
  87. cfg, err := config.NewConfig(test.opts)
  88. if err != nil {
  89. t.Error(err)
  90. }
  91. m, err := manager.NewManager(test.opts, cfg)
  92. if err != nil {
  93. t.Error(err)
  94. }
  95. err = Run(m)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if test.desiredLeaks != len(m.GetLeaks()) {
  100. t.Errorf("got %d leaks, want %d", len(m.GetLeaks()), test.desiredLeaks)
  101. }
  102. }
  103. }