@@ -63,6 +63,151 @@ def test_application_registration_user(self):
63
63
self .assertEqual (app .algorithm , form_data ["algorithm" ])
64
64
65
65
66
+ @pytest .mark .usefixtures ("oauth2_settings" )
67
+ @pytest .mark .oauth2_settings ({"ALLOW_REDIRECT_URI_WILDCARDS" : True })
68
+ class TestApplicationRegistrationViewRedirectURIWithWildcardRedirectURIs (BaseTest ):
69
+ def _test_valid (self , redirect_uri ):
70
+ self .client .login (username = "foo_user" , password = "123456" )
71
+
72
+ form_data = {
73
+ "name" : "Foo app" ,
74
+ "client_id" : "client_id" ,
75
+ "client_secret" : "client_secret" ,
76
+ "client_type" : Application .CLIENT_CONFIDENTIAL ,
77
+ "redirect_uris" : redirect_uri ,
78
+ "post_logout_redirect_uris" : "http://example.com" ,
79
+ "authorization_grant_type" : Application .GRANT_AUTHORIZATION_CODE ,
80
+ "algorithm" : "" ,
81
+ }
82
+
83
+ response = self .client .post (reverse ("oauth2_provider:register" ), form_data )
84
+ self .assertEqual (response .status_code , 302 )
85
+
86
+ app = get_application_model ().objects .get (name = "Foo app" )
87
+ self .assertEqual (app .user .username , "foo_user" )
88
+ app = Application .objects .get ()
89
+ self .assertEqual (app .name , form_data ["name" ])
90
+ self .assertEqual (app .client_id , form_data ["client_id" ])
91
+ self .assertEqual (app .redirect_uris , form_data ["redirect_uris" ])
92
+ self .assertEqual (app .post_logout_redirect_uris , form_data ["post_logout_redirect_uris" ])
93
+ self .assertEqual (app .client_type , form_data ["client_type" ])
94
+ self .assertEqual (app .authorization_grant_type , form_data ["authorization_grant_type" ])
95
+ self .assertEqual (app .algorithm , form_data ["algorithm" ])
96
+
97
+ def _test_invalid (self , uri , error_message ):
98
+ self .client .login (username = "foo_user" , password = "123456" )
99
+
100
+ form_data = {
101
+ "name" : "Foo app" ,
102
+ "client_id" : "client_id" ,
103
+ "client_secret" : "client_secret" ,
104
+ "client_type" : Application .CLIENT_CONFIDENTIAL ,
105
+ "redirect_uris" : uri ,
106
+ "post_logout_redirect_uris" : "http://example.com" ,
107
+ "authorization_grant_type" : Application .GRANT_AUTHORIZATION_CODE ,
108
+ "algorithm" : "" ,
109
+ }
110
+
111
+ response = self .client .post (reverse ("oauth2_provider:register" ), form_data )
112
+ self .assertEqual (response .status_code , 400 )
113
+ self .assertContains (response , error_message )
114
+
115
+ def test_application_registration_valid_3ld_wildcard (self ):
116
+ self ._test_valid ("http://*.example.com" )
117
+
118
+ def test_application_registration_valid_3ld_partial_wildcard (self ):
119
+ self ._test_valid ("http://*-partial.example.com" )
120
+
121
+ def test_application_registration_invalid_tld_wildcard (self ):
122
+ self ._test_invalid ("http://*" , "Wildcard redirect URIs must be at least 3 levels deep" )
123
+
124
+ def test_application_registration_invalid_tld_partial_wildcard (self ):
125
+ self ._test_invalid ("http://*-partial" , "Wildcard redirect URIs must be at least 3 levels deep" )
126
+
127
+ def test_application_registration_invalid_tld_not_startswith_wildcard_tld (self ):
128
+ self ._test_invalid ("http://example.*" , "Wildcard redirect URIs must start with a wildcard character" )
129
+
130
+ def test_application_registration_invalid_2ld_wildcard (self ):
131
+ self ._test_invalid ("http://*.com" , "Wildcard redirect URIs must be at least 3 levels deep" )
132
+
133
+ def test_application_registration_invalid_2ld_partial_wildcard (self ):
134
+ self ._test_invalid ("http://*-partial.com" , "Wildcard redirect URIs must be at least 3 levels deep" )
135
+
136
+ def test_application_registration_invalid_2ld_not_startswith_wildcard_tld (self ):
137
+ self ._test_invalid (
138
+ "http://example.*.com" , "Wildcard redirect URIs must start with a wildcard character"
139
+ )
140
+
141
+ def test_application_registration_invalid_3ld_partial_not_startswith_wildcard_2ld (self ):
142
+ self ._test_invalid (
143
+ "http://invalid-*.example.com" , "Wildcard redirect URIs must start with a wildcard character"
144
+ )
145
+
146
+ def test_application_registration_invalid_4ld_not_startswith_wildcard_3ld (self ):
147
+ self ._test_invalid (
148
+ "http://invalid/.*.invalid.example.com" ,
149
+ "Wildcard redirect URIs must start with a wildcard character" ,
150
+ )
151
+
152
+ def test_application_registration_invalid_4ld_partial_not_startswith_wildcard_2ld (self ):
153
+ self ._test_invalid (
154
+ "http://invalid-*.invalid.example.com" ,
155
+ "Wildcard redirect URIs must start with a wildcard character" ,
156
+ )
157
+
158
+
159
+ @pytest .mark .usefixtures ("oauth2_settings" )
160
+ @pytest .mark .oauth2_settings ({"ALLOW_REDIRECT_URI_WILDCARDS" : True })
161
+ class TestApplicationRegistrationViewPostLogoutRedirectURIWithWildcardRedirectURIs (
162
+ TestApplicationRegistrationViewRedirectURIWithWildcardRedirectURIs
163
+ ):
164
+ def _test_valid (self , redirect_uri ):
165
+ self .client .login (username = "foo_user" , password = "123456" )
166
+
167
+ form_data = {
168
+ "name" : "Foo app" ,
169
+ "client_id" : "client_id" ,
170
+ "client_secret" : "client_secret" ,
171
+ "client_type" : Application .CLIENT_CONFIDENTIAL ,
172
+ "redirect_uris" : "http://example.com" ,
173
+ "post_logout_redirect_uris" : redirect_uri ,
174
+ "authorization_grant_type" : Application .GRANT_AUTHORIZATION_CODE ,
175
+ "algorithm" : "" ,
176
+ }
177
+
178
+ response = self .client .post (reverse ("oauth2_provider:register" ), form_data )
179
+ self .assertEqual (response .status_code , 302 )
180
+
181
+ app = get_application_model ().objects .get (name = "Foo app" )
182
+ self .assertEqual (app .user .username , "foo_user" )
183
+ app = Application .objects .get ()
184
+ self .assertEqual (app .name , form_data ["name" ])
185
+ self .assertEqual (app .client_id , form_data ["client_id" ])
186
+ self .assertEqual (app .redirect_uris , form_data ["redirect_uris" ])
187
+ self .assertEqual (app .post_logout_redirect_uris , form_data ["post_logout_redirect_uris" ])
188
+ self .assertEqual (app .client_type , form_data ["client_type" ])
189
+ self .assertEqual (app .authorization_grant_type , form_data ["authorization_grant_type" ])
190
+ self .assertEqual (app .algorithm , form_data ["algorithm" ])
191
+
192
+ def _test_invalid (self , uri , error_message ):
193
+ self .client .login (username = "foo_user" , password = "123456" )
194
+
195
+ form_data = {
196
+ "name" : "Foo app" ,
197
+ "client_id" : "client_id" ,
198
+ "client_secret" : "client_secret" ,
199
+ "client_type" : Application .CLIENT_CONFIDENTIAL ,
200
+ "redirect_uris" : "http://example.com" ,
201
+ "post_logout_redirect_uris" : uri ,
202
+ "authorization_grant_type" : Application .GRANT_AUTHORIZATION_CODE ,
203
+ "algorithm" : "" ,
204
+ }
205
+
206
+ response = self .client .post (reverse ("oauth2_provider:register" ), form_data )
207
+ self .assertEqual (response .status_code , 400 )
208
+ self .assertContains (response , error_message )
209
+
210
+
66
211
class TestApplicationViews (BaseTest ):
67
212
@classmethod
68
213
def _create_application (cls , name , user ):
0 commit comments