@@ -8,6 +8,112 @@ import (
8
8
"github.com/coder/coder/v2/codersdk"
9
9
)
10
10
11
+ func TestGitlabDefaults (t * testing.T ) {
12
+ t .Parallel ()
13
+
14
+ // The default cloud setup. Copying this here as hard coded
15
+ // values.
16
+ cloud := codersdk.ExternalAuthConfig {
17
+ Type : string (codersdk .EnhancedExternalAuthProviderGitLab ),
18
+ ID : string (codersdk .EnhancedExternalAuthProviderGitLab ),
19
+ AuthURL : "https://gitlab.com/oauth/authorize" ,
20
+ TokenURL : "https://gitlab.com/oauth/token" ,
21
+ ValidateURL : "https://gitlab.com/oauth/token/info" ,
22
+ DisplayName : "GitLab" ,
23
+ DisplayIcon : "/icon/gitlab.svg" ,
24
+ Regex : `^(https?://)?gitlab\.com(/.*)?$` ,
25
+ Scopes : []string {"write_repository" },
26
+ }
27
+
28
+ tests := []struct {
29
+ name string
30
+ input codersdk.ExternalAuthConfig
31
+ expected codersdk.ExternalAuthConfig
32
+ mutateExpected func (* codersdk.ExternalAuthConfig )
33
+ }{
34
+ // Cloud
35
+ {
36
+ name : "OnlyType" ,
37
+ input : codersdk.ExternalAuthConfig {
38
+ Type : string (codersdk .EnhancedExternalAuthProviderGitLab ),
39
+ },
40
+ expected : cloud ,
41
+ },
42
+ {
43
+ // If someone was to manually configure the gitlab cli.
44
+ name : "CloudByConfig" ,
45
+ input : codersdk.ExternalAuthConfig {
46
+ Type : string (codersdk .EnhancedExternalAuthProviderGitLab ),
47
+ AuthURL : "https://gitlab.com/oauth/authorize" ,
48
+ },
49
+ expected : cloud ,
50
+ },
51
+ {
52
+ // Changing some of the defaults of the cloud option
53
+ name : "CloudWithChanges" ,
54
+ input : codersdk.ExternalAuthConfig {
55
+ Type : string (codersdk .EnhancedExternalAuthProviderGitLab ),
56
+ // Adding an extra query param intentionally to break simple
57
+ // string comparisons.
58
+ AuthURL : "https://gitlab.com/oauth/authorize?foo=bar" ,
59
+ DisplayName : "custom" ,
60
+ Regex : ".*" ,
61
+ },
62
+ expected : cloud ,
63
+ mutateExpected : func (config * codersdk.ExternalAuthConfig ) {
64
+ config .AuthURL = "https://gitlab.com/oauth/authorize?foo=bar"
65
+ config .DisplayName = "custom"
66
+ config .Regex = ".*"
67
+ },
68
+ },
69
+ // Self-hosted
70
+ {
71
+ // Dynamically figures out the Validate, Token, and Regex fields.
72
+ name : "SelfHostedOnlyAuthURL" ,
73
+ input : codersdk.ExternalAuthConfig {
74
+ Type : string (codersdk .EnhancedExternalAuthProviderGitLab ),
75
+ AuthURL : "https://gitlab.company.org/oauth/authorize?foo=bar" ,
76
+ },
77
+ expected : cloud ,
78
+ mutateExpected : func (config * codersdk.ExternalAuthConfig ) {
79
+ config .AuthURL = "https://gitlab.company.org/oauth/authorize?foo=bar"
80
+ config .ValidateURL = "https://gitlab.company.org/oauth/token/info"
81
+ config .TokenURL = "https://gitlab.company.org/oauth/token"
82
+ config .Regex = `^(https?://)?gitlab\.company\.org(/.*)?$`
83
+ },
84
+ },
85
+ {
86
+ // Strange values
87
+ name : "RandomValues" ,
88
+ input : codersdk.ExternalAuthConfig {
89
+ Type : string (codersdk .EnhancedExternalAuthProviderGitLab ),
90
+ AuthURL : "https://auth.com/auth" ,
91
+ ValidateURL : "https://validate.com/validate" ,
92
+ TokenURL : "https://token.com/token" ,
93
+ Regex : "random" ,
94
+ },
95
+ expected : cloud ,
96
+ mutateExpected : func (config * codersdk.ExternalAuthConfig ) {
97
+ config .AuthURL = "https://auth.com/auth"
98
+ config .ValidateURL = "https://validate.com/validate"
99
+ config .TokenURL = "https://token.com/token"
100
+ config .Regex = `random`
101
+ },
102
+ },
103
+ }
104
+ for _ , c := range tests {
105
+ c := c
106
+ t .Run (c .name , func (t * testing.T ) {
107
+ t .Parallel ()
108
+ applyDefaultsToConfig (& c .input )
109
+ if c .mutateExpected != nil {
110
+ c .mutateExpected (& c .expected )
111
+ }
112
+ require .Equal (t , c .input , c .expected )
113
+ })
114
+ }
115
+ }
116
+
11
117
func Test_bitbucketServerConfigDefaults (t * testing.T ) {
12
118
t .Parallel ()
13
119
0 commit comments