1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2016 Severen Redwood <severen@shrike.me>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from flask
import url_for
22 from bluepill
.server
import get_application
23 from bluepill
.server
.resource
import HOMESERVER
, users
, get_error_message
25 from test_resource
import OK
, NOT_FOUND
, BAD_REQUEST
, UNPROCESSABLE_ENTITY
, app
# noqa
27 # Create a local copy of the @alice:example.com user entry.
28 USER
= users
['@alice:example.com'].copy()
30 # Get the URL for the register endpoint.
31 with
get_application().test_request_context():
32 URL
= url_for('r0.register')
34 def test_correct_requests(client
):
35 # Test a correct request with the kind set to user.
36 response
= client
.post(URL
+ '?kind=user', data
=json
.dumps({
39 'password': 'testpassword',
42 assert response
.status
== OK
43 assert response
.json
['user_id'] == '@john:%s' % HOMESERVER
44 assert len(str(response
.json
['access_token'])) == 10
45 assert len(str(response
.json
['refresh_token'])) == 10
46 assert response
.json
['home_server'] == HOMESERVER
48 # Test a correct request with the kind set to guest.
49 response
= client
.post(URL
+ '?kind=guest', data
=json
.dumps({
52 'password': 'testpassword',
55 assert response
.status
== OK
56 assert response
.json
['user_id'] == '@liam:%s' % HOMESERVER
57 assert len(str(response
.json
['access_token'])) == 10
58 assert len(str(response
.json
['refresh_token'])) == 10
59 assert response
.json
['home_server'] == HOMESERVER
61 # Test a correct request with no username specified.
62 # A random number should be generated as the username.
63 response
= client
.post(URL
+ '?kind=guest', data
=json
.dumps({
64 'password': 'testpassword',
67 assert response
.status
== OK
68 # The regex extracts the username from the user ID.
70 username
= search(r
'\A@([^@:]*):', response
.json
['user_id']).group(1)
71 # Make sure the username is a number.
74 def test_malformed_requests(client
):
75 # Test a request with the argument malformed.
76 response
= client
.post(URL
+ '?kind=use', data
=json
.dumps({
79 'password': 'testpassword',
82 assert response
.status
== UNPROCESSABLE_ENTITY
84 # Test a request with the JSON missing a required field.
85 response
= client
.post(URL
+ '?kind=user', data
=json
.dumps({
90 assert response
.status
== BAD_REQUEST
91 assert response
.json
== get_error_message(
93 'The supplied JSON was missing the password field.',
96 # Test a request with the JSON missing.
97 response
= client
.post(URL
+ '?kind=user')
99 assert response
.status
== BAD_REQUEST
100 assert response
.json
== get_error_message(
105 # Test a request with the JSON being invalid.
106 response
= client
.post(URL
+ '?kind=user', data
=""""
109 "password": "testpassword"
113 assert response
.status
== BAD_REQUEST
114 assert response
.json
== get_error_message(
119 def test_registering_user_that_exists(client
):
123 'password': 'testpassword',
125 response
= client
.post(URL
+ '?kind=user', data
=data
)
127 assert response
.status
== BAD_REQUEST
128 assert response
.json
== get_error_message(
130 'User ID already taken.',