Beispiel #1
0
 def test_main_with_users(self):
     """Ensure the main route behaves correctly when users have been added"""
     add_user('michael', '*****@*****.**', 'greaterthaneight')
     add_user('fletcher', '*****@*****.**', 'greaterthaneight')
     with self.client:
         response = self.client.get('/')
         self.assertEqual(response.status_code, 200)
         self.assertNotIn(b'<p>No users!</p>', response.data)
         self.assertIn(b'michael', response.data)
         self.assertIn(b'fletcher', response.data)
 def test_add_user(self):
     user = add_user('justatest', '*****@*****.**', 'greaterthaneight')
     self.assertTrue(user.id)
     self.assertEqual(user.username, 'justatest')
     self.assertEqual(user.email, '*****@*****.**')
     self.assertTrue(user.active)
     self.assertTrue(user.password)
 def test_user_registration_duplicate_username(self):
     add_user('test', '*****@*****.**', 'test')
     with self.client:
         response = self.client.post(
             'auth/register',
             data=json.dumps({
                 'username': '******',
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 400)
         self.assertIn('fail', data['status'])
         self.assertIn('Sorry. That user already exists', data['message'])
def register_user():
    # get post data
    post_data = request.get_json()
    response_object = {'status': 'fail', 'message': 'Invalid payload.'}

    if not post_data:
        return (jsonify(response_object), 400)

    username = post_data.get('username')
    email = post_data.get('email')
    password = post_data.get('password')
    try:
        # check for existing user
        user = User.query.filter(
            or_(User.username == username, User.email == email)).first()
        if not user:
            new_user = add_user(username, email, password)
            auth_token = new_user.encode_auth_token(new_user.id)
            response_object['status'] = 'success'
            response_object['message'] = 'Successfully registered.'
            response_object['auth_token'] = auth_token.decode()
            return jsonify(response_object), 201
        else:
            response_object['message'] = 'Sorry. That user already exists.'
            return jsonify(response_object), 400
    except (exc.IntegrityError, ValueError):
        db.session.rollback()
        return jsonify(response_object), 400
 def test_registered_user_login(self):
     with self.client:
         add_user('test', '*****@*****.**', 'test')
         response = self.client.post(
             'auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         data = json.loads(response.data.decode())
         self.assertIn('success', data['status'])
         self.assertTrue(data['message'] == 'Successfully logged in.')
         self.assertTrue(data['auth_token'])
         self.assertTrue(response.content_type == 'application/json')
         self.assertEqual(response.status_code, 200)
Beispiel #6
0
 def test_single_user(self):
     """Ensire get single user behaves correctly"""
     user = add_user('michael', '*****@*****.**', 'greaterthaneight')
     with self.client:
         response = self.client.get(f'/users/{user.id}')
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 200)
         self.assertIn('michael', data['data']['username'])
         self.assertIn('*****@*****.**', data['data']['email'])
         self.assertIn('success', data['status'])
Beispiel #7
0
 def test_all_user(self):
     """Ensure get all users behaves correctly"""
     add_user('michael', '*****@*****.**', 'greaterthaneight')
     add_user('fletcher', '*****@*****.**', 'greaterthaneight')
     with self.client:
         response = self.client.get('/users')
         data = json.loads(response.data.decode())
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(data['data']['users']), 2)
         self.assertIn('michael', data['data']['users'][0]['username'])
         self.assertIn('*****@*****.**',
                       data['data']['users'][0]['email'])
         self.assertTrue(data['data']['users'][0]['active'])
         self.assertFalse(data['data']['users'][0]['admin'])
         self.assertIn('fletcher', data['data']['users'][1]['username'])
         self.assertIn('*****@*****.**',
                       data['data']['users'][1]['email'])
         self.assertIn('success', data['status'])
         self.assertTrue(data['data']['users'][1]['active'])
         self.assertFalse(data['data']['users'][1]['admin'])
 def test_registered_user_logout(self):
     add_user('test', '*****@*****.**', 'test')
     with self.client:
         resp_login = self.client.post(
             'auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         # valid token logout
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             'auth/logout',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'success')
         self.assertIn('Successfully logged out.', data['message'])
         self.assertEqual(response.status_code, 200)
 def test_invalid_logout_expired_token(self):
     add_user('test', '*****@*****.**', 'test')
     current_app.config['TOKEN_EXPIRATION_SECONDS'] = -1
     with self.client:
         resp_login = self.client.post(
             'auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         # invalid token logout
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             'auth/logout',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertTrue(data['status'] == 'fail')
         self.assertIn(
             'Signature expired. Please log in again', data['message'])
         self.assertEqual(response.status_code, 401)
 def test_user_status(self):
     add_user('test', '*****@*****.**', 'test')
     with self.client:
         resp_login = self.client.post(
             'auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             'auth/status',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertIn('Success.', data['message'])
         self.assertTrue(data['data'] is not None)
         self.assertTrue(data['data']['username'] == 'test')
         self.assertTrue(data['data']['email'] == '*****@*****.**')
         self.assertTrue(data['data']['active'] is True)
         self.assertFalse(data['data']['admin'])
         self.assertEqual(response.status_code, 200)
 def test_invalid_logout_inactive(self):
     user = add_user('test', '*****@*****.**', 'test')
     user.active = False
     db.session.commit()
     with self.client:
         resp_login = self.client.post(
             'auth/login',
             data=json.dumps({
                 'email': '*****@*****.**',
                 'password': '******'
             }),
             content_type='application/json'
         )
         token = json.loads(resp_login.data.decode())['auth_token']
         response = self.client.get(
             'auth/logout',
             headers={'Authorization': f'Bearer {token}'}
         )
         data = json.loads(response.data.decode())
         self.assertIn('fail', data['status'])
         self.assertTrue(data['message'] == 'Provide a valid auth token.')
         self.assertEqual(response.status_code, 401)
 def test_encode_auth_token(self):
     user = add_user('justatest', '*****@*****.**', 'test')
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
     self.assertEqual(user.decode_auth_token(auth_token), user.id)
 def test_passwords_are_random(self):
     user_one = add_user('justatest', '*****@*****.**', 'greaterthaneight')
     user_two = add_user('justatest2', '*****@*****.**', 'greaterthaneight')
     self.assertNotEqual(user_one.password, user_two.password)
 def test_to_json(self):
     user = add_user('justatest', '*****@*****.**', 'greaterthaneight')
     self.assertTrue(isinstance(user.to_json(), dict))
 def test_add_user_duplicate_email(self):
     add_user('justatest', '*****@*****.**', 'greaterthaneight')
     with self.assertRaises(IntegrityError):
         add_user('justanotertest', '*****@*****.**', 'greaterthaneight')