1 package githubclient
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestAuth_Validate(t *testing.T) {
10 tcs := map[string]struct {
11 config Config
12 valid bool
13 }{
14
15 "badConfigNoAuthPresent": {
16 Config{
17 Repository: "https://github.com/foo/bar.git",
18 }, false,
19 },
20
21 "badConfigNoAppID": {
22 Config{
23 Repository: "https://github.com/foo/bar.git",
24 PrivateKey: []byte("foo"),
25 InstallationID: 1234,
26 }, false,
27 },
28 "badConfigNoPrivateKey": {
29 Config{
30 Repository: "https://github.com/foo/bar.git",
31 AppID: 1234,
32 InstallationID: 1234,
33 AccessToken: "asdf",
34 }, true,
35 },
36 "badConfigNoPrivateKeyOrAccessToken": {
37 Config{
38 Repository: "https://github.com/foo/bar.git",
39 AppID: 1234,
40 InstallationID: 1234,
41 }, false,
42 },
43
44 "goodConfigGithubApp": {
45 Config{
46 Repository: "https://github.com/place-holder/for-unit-tests",
47 PrivateKey: []byte("foo"),
48 AppID: 1234,
49 InstallationID: 1234,
50 }, true,
51 },
52 "goodConfigNoInstallationID": {
53 Config{
54 Repository: "https://github.com/foo/bar.git",
55 PrivateKey: []byte("foo"),
56 AppID: 1234,
57 }, true,
58 },
59 }
60 for name, tc := range tcs {
61 t.Run(name, func(t *testing.T) {
62 switch tc.valid {
63 case true:
64 assert.NoError(t, tc.config.Validate())
65 case false:
66 assert.Error(t, tc.config.Validate())
67 }
68 })
69 }
70 }
71 func TestRepo_Validate(t *testing.T) {
72 type Test struct {
73 Config
74 expectOwner string
75 expectRepo string
76 }
77 tcs := map[string]struct {
78 test Test
79 valid bool
80 }{
81 "badConfigNoRepository": {
82 Test{
83 Config: Config{
84 Repository: "",
85 PrivateKey: []byte("foo"),
86 AppID: 1234,
87 },
88 }, false,
89 },
90 "badConfigNonHTTPSConfig": {
91 Test{
92 Config: Config{
93 Repository: "http://github.com/foo/bar.git",
94 PrivateKey: []byte("foo"),
95 AppID: 1234,
96 },
97 }, false,
98 },
99
100 "goodConfigOne": {
101 Test{
102 Config: Config{
103 Repository: "https://github.com/foo/bar.git",
104 PrivateKey: []byte("foo"),
105 AppID: 1234,
106 },
107 expectOwner: "foo",
108 expectRepo: "bar",
109 }, true,
110 },
111 "goodConfigTwo": {
112 Test{
113 Config: Config{
114 Repository: "https://github.com/foo-bar/baz-bit.git",
115 PrivateKey: []byte("foo"),
116 AppID: 1234,
117 },
118 expectOwner: "foo-bar",
119 expectRepo: "baz-bit",
120 }, true,
121 },
122 "goodConfigThree": {
123 Test{
124 Config: Config{
125 Repository: "https://github.com/foo/bar-baz",
126 PrivateKey: []byte("foo"),
127 AppID: 1234,
128 },
129 expectOwner: "foo",
130 expectRepo: "bar-baz",
131 }, true,
132 },
133 }
134 for name, tc := range tcs {
135 t.Run(name, func(t *testing.T) {
136 switch tc.valid {
137 case true:
138 actualOwner := tc.test.Config.Owner()
139 actualRepo := tc.test.Config.Repo()
140 assert.Equal(t, tc.test.expectOwner, actualOwner)
141 assert.Equal(t, tc.test.expectRepo, actualRepo)
142 case false:
143 err := tc.test.Config.Validate()
144 assert.Error(t, err)
145 }
146 })
147 }
148 }
149
View as plain text