allowlist_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package config
  2. import (
  3. "regexp"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestCommitAllowed(t *testing.T) {
  8. tests := []struct {
  9. allowlist Allowlist
  10. commit string
  11. commitAllowed bool
  12. }{
  13. {
  14. allowlist: Allowlist{
  15. Commits: []string{"commitA"},
  16. },
  17. commit: "commitA",
  18. commitAllowed: true,
  19. },
  20. {
  21. allowlist: Allowlist{
  22. Commits: []string{"commitB"},
  23. },
  24. commit: "commitA",
  25. commitAllowed: false,
  26. },
  27. {
  28. allowlist: Allowlist{
  29. Commits: []string{"commitB"},
  30. },
  31. commit: "",
  32. commitAllowed: false,
  33. },
  34. }
  35. for _, tt := range tests {
  36. assert.Equal(t, tt.commitAllowed, tt.allowlist.CommitAllowed(tt.commit))
  37. }
  38. }
  39. func TestRegexAllowed(t *testing.T) {
  40. tests := []struct {
  41. allowlist Allowlist
  42. secret string
  43. regexAllowed bool
  44. }{
  45. {
  46. allowlist: Allowlist{
  47. Regexes: []*regexp.Regexp{regexp.MustCompile("matchthis")},
  48. },
  49. secret: "a secret: matchthis, done",
  50. regexAllowed: true,
  51. },
  52. {
  53. allowlist: Allowlist{
  54. Regexes: []*regexp.Regexp{regexp.MustCompile("matchthis")},
  55. },
  56. secret: "a secret",
  57. regexAllowed: false,
  58. },
  59. }
  60. for _, tt := range tests {
  61. assert.Equal(t, tt.regexAllowed, tt.allowlist.RegexAllowed(tt.secret))
  62. }
  63. }
  64. func TestPathAllowed(t *testing.T) {
  65. tests := []struct {
  66. allowlist Allowlist
  67. path string
  68. pathAllowed bool
  69. }{
  70. {
  71. allowlist: Allowlist{
  72. Paths: []*regexp.Regexp{regexp.MustCompile("path")},
  73. },
  74. path: "a path",
  75. pathAllowed: true,
  76. },
  77. {
  78. allowlist: Allowlist{
  79. Paths: []*regexp.Regexp{regexp.MustCompile("path")},
  80. },
  81. path: "a ???",
  82. pathAllowed: false,
  83. },
  84. }
  85. for _, tt := range tests {
  86. assert.Equal(t, tt.pathAllowed, tt.allowlist.PathAllowed(tt.path))
  87. }
  88. }