1
16
17 package unit
18
19 import (
20 "context"
21 "fmt"
22 "net/http"
23 "testing"
24
25 "github.com/jarcoal/httpmock"
26 "github.com/okta/okta-sdk-golang/v2/okta"
27
28 "github.com/okta/okta-sdk-golang/v2/tests"
29 "github.com/stretchr/testify/assert"
30 )
31
32 func Test_error_on_empty_url(t *testing.T) {
33 _, _, err := tests.NewClient(context.TODO(), okta.WithOrgUrl(""))
34 assert.Error(t, err, "Does not error when org url is missing")
35 }
36
37 func Test_error_when_url_contains_yourOktaDomain(t *testing.T) {
38 _, _, err := tests.NewClient(context.TODO(), okta.WithOrgUrl("https://{yourOktaDomain}"))
39 assert.Error(t, err, "Does not error when org url contains {yourOktaDomain}")
40 }
41
42 func Test_error_when_url_contains_admin_okta_com(t *testing.T) {
43 _, _, err := tests.NewClient(context.TODO(), okta.WithOrgUrl("https://test-admin.okta.com"))
44 assert.Error(t, err, "Does not error when org url contains test-admin.okta.com")
45 }
46
47 func Test_error_when_url_contains_admin_oktapreview_com(t *testing.T) {
48 _, _, err := tests.NewClient(context.TODO(), okta.WithOrgUrl("https://test-admin.oktapreview.com"))
49 assert.Error(t, err, "Does not error when org url contains test-admin.oktapreview.com")
50 }
51
52 func Test_error_when_url_contains_admin_okta_emea_com(t *testing.T) {
53 _, _, err := tests.NewClient(context.TODO(), okta.WithOrgUrl("https://test-admin.okta-emea.com"))
54 assert.Error(t, err, "Does not error when org url contains test-admin.okta-emea.com")
55 }
56
57 func Test_error_when_url_contains_com_com(t *testing.T) {
58 _, _, err := tests.NewClient(context.TODO(), okta.WithOrgUrl("https://test.okta.com.com"))
59 assert.Error(t, err, "Does not error when org url contains .com.com")
60 }
61
62 func Test_error_when_url_does_not_begin_with_https(t *testing.T) {
63 _, _, err := tests.NewClient(context.TODO(), okta.WithTestingDisableHttpsCheck(false), okta.WithOrgUrl("http://test.okta.com"))
64 assert.Error(t, err, "Does not error when url contains only http")
65 }
66
67 func Test_error_when_api_token_is_empty(t *testing.T) {
68 _, _, err := tests.NewClient(context.TODO(), okta.WithToken(""))
69 assert.Error(t, err, "Does not error when api token is empty")
70 }
71
72 func Test_error_when_api_token_contains_placeholder(t *testing.T) {
73 _, _, err := tests.NewClient(context.TODO(), okta.WithToken("{apiToken}"))
74 assert.Error(t, err, "Does not error when api token contains {apiToken}")
75 }
76
77 func Test_error_when_authorization_mode_is_not_valid(t *testing.T) {
78 _, _, err := tests.NewClient(context.TODO(), okta.WithAuthorizationMode("invalid"))
79 assert.Error(t, err, "Does not error when authorization mode is invalid")
80 }
81
82 func Test_does_not_error_when_authorization_mode_is_valid(t *testing.T) {
83 _, _, err := tests.NewClient(context.TODO(), okta.WithAuthorizationMode("SSWS"))
84 assert.NoError(t, err, "Should not error when authorization mode is SSWS")
85 }
86
87 func Test_does_not_error_when_authorization_mode_is_brearer(t *testing.T) {
88 _, _, err := tests.NewClient(context.TODO(), okta.WithAuthorizationMode("Bearer"))
89 assert.NoError(t, err, "Should not error when authorization mode is Bearer")
90 }
91
92 func Test_will_error_if_private_key_authorization_type_with_missing_properties(t *testing.T) {
93 _, _, err := tests.NewClient(context.TODO(), okta.WithAuthorizationMode("PrivateKey"), okta.WithClientId(""))
94 assert.Error(t, err, "Does not error if private key selected with no other required options")
95 }
96
97 type InterceptingRoundTripperTest struct {
98 Name string
99 Blocking bool
100 Interceptor func(*http.Request) error
101 ExpectedTransportCalls int
102 ExpectInterceptorCalled bool
103 ExpectSdkErrorThrown bool
104 }
105
106 func Test_Intercepting_RoundTripper(t *testing.T) {
107 interceptorCalled := false
108 testsToRun := []InterceptingRoundTripperTest{
109 {
110 Name: "Calls interceptor",
111 Blocking: false,
112 Interceptor: func(r *http.Request) error {
113 interceptorCalled = true
114 return nil
115 },
116 ExpectedTransportCalls: 1,
117 ExpectInterceptorCalled: true,
118 ExpectSdkErrorThrown: false,
119 },
120 {
121 Name: "Does not call transport when interceptor panics when blocking",
122 Blocking: true,
123 Interceptor: func(r *http.Request) error {
124 interceptorCalled = true
125 panic("Some err")
126 },
127 ExpectedTransportCalls: 0,
128 ExpectInterceptorCalled: true,
129 ExpectSdkErrorThrown: true,
130 },
131 {
132 Name: "Calls transport when interceptor panics when non blocking",
133 Blocking: false,
134 Interceptor: func(r *http.Request) error {
135 interceptorCalled = true
136 panic("Some err")
137 },
138 ExpectedTransportCalls: 1,
139 ExpectInterceptorCalled: true,
140 ExpectSdkErrorThrown: false,
141 },
142 {
143 Name: "Does not call transport when interceptor throws err when blocking",
144 Blocking: true,
145 Interceptor: func(r *http.Request) error {
146 interceptorCalled = true
147 return fmt.Errorf("Some error")
148 },
149 ExpectedTransportCalls: 0,
150 ExpectInterceptorCalled: true,
151 ExpectSdkErrorThrown: true,
152 },
153 {
154 Name: "Calls transport when interceptor throws err when not blocking",
155 Blocking: false,
156 Interceptor: func(r *http.Request) error {
157 interceptorCalled = true
158 return fmt.Errorf("Some error")
159 },
160 ExpectedTransportCalls: 1,
161 ExpectInterceptorCalled: true,
162 ExpectSdkErrorThrown: false,
163 },
164 }
165
166 for _, test := range testsToRun {
167 t.Run(
168 test.Name,
169 func(t *testing.T) {
170 mockHttpClient := http.DefaultClient
171 mockTransport := httpmock.DefaultTransport
172 mockTransport.RegisterNoResponder(func(r *http.Request) (*http.Response, error) {
173 return &http.Response{StatusCode: 200}, nil
174 })
175 mockHttpClient.Transport = mockTransport
176
177 _, oktaClient, err := tests.NewClient(
178 context.TODO(),
179 okta.WithHttpInterceptorAndHttpClientPtr(test.Interceptor, mockHttpClient, test.Blocking),
180 )
181 assert.NoError(t, err)
182
183 _, _, err = oktaClient.IdentityProvider.ActivateIdentityProvider(context.TODO(), "Anything")
184
185 if test.ExpectSdkErrorThrown {
186 assert.Error(t, err)
187 } else {
188 assert.NoError(t, err)
189 }
190
191 assert.Equal(t, test.ExpectInterceptorCalled, interceptorCalled)
192
193 callCount := mockTransport.GetTotalCallCount()
194
195 assert.Equal(t, test.ExpectedTransportCalls, callCount)
196
197 interceptorCalled = false
198 mockTransport.ZeroCallCounters()
199 },
200 )
201 }
202 }
203
View as plain text