Fix PyPI badge
[bluepill.git] / tests / test_resource_r0_register.py
blob7b6fdd842a5fdd4f1cab328e675d9a06f62fa112
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.
16 import json
18 from flask import url_for
20 from re import search
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({
37 'username': 'john',
38 'bind_email': False,
39 'password': 'testpassword',
40 }))
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({
50 'username': 'liam',
51 'bind_email': False,
52 'password': 'testpassword',
53 }))
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',
65 }))
67 assert response.status == OK
68 # The regex extracts the username from the user ID.
69 # Ugly, I know.
70 username = search(r'\A@([^@:]*):', response.json['user_id']).group(1)
71 # Make sure the username is a number.
72 int(username)
74 def test_malformed_requests(client):
75 # Test a request with the argument malformed.
76 response = client.post(URL + '?kind=use', data=json.dumps({
77 'username': 'john',
78 'bind_email': False,
79 'password': 'testpassword',
80 }))
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({
86 'username': 'john',
87 'bind_email': False,
88 }))
90 assert response.status == BAD_REQUEST
91 assert response.json == get_error_message(
92 'M_BAD_JSON',
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(
101 'M_NOT_JSON',
102 'Content not JSON.',
105 # Test a request with the JSON being invalid.
106 response = client.post(URL + '?kind=user', data=""""
108 "username": "john"
109 "password": "testpassword"
111 """)
113 assert response.status == BAD_REQUEST
114 assert response.json == get_error_message(
115 'M_NOT_JSON',
116 'Content not JSON.',
119 def test_registering_user_that_exists(client):
120 data = json.dumps({
121 'username': 'alice',
122 'bind_email': False,
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(
129 'M_USER_IN_USE',
130 'User ID already taken.',