summaryrefslogtreecommitdiff
path: root/internal/authorization/regexp_test.go
blob: a47567b0e59ea4a1d78773b271716272839b5294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package authorization

import (
	"regexp"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestRegexpGroupStringSubjectMatcher_IsMatch(t *testing.T) {
	testCases := []struct {
		name     string
		have     *RegexpGroupStringSubjectMatcher
		input    string
		subject  Subject
		expected bool
	}{
		{
			"Abc",
			&RegexpGroupStringSubjectMatcher{
				MustCompileRegexNoPtr(`^(?P<User>[a-zA-Z0-9]+)\.regex.com$`),
				1,
				0,
			},
			"example.com",
			Subject{Username: "a-user"},
			false,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			assert.Equal(t, tc.expected, tc.have.IsMatch(tc.input, tc.subject))
		})
	}
}

func MustCompileRegexNoPtr(input string) regexp.Regexp {
	out := regexp.MustCompile(input)

	return *out
}