def test_add_user_with_inactive_user(self): add_admin_user('testuser', '*****@*****.**', 'testpass') # update user to inactive user = User.query.filter_by(email='*****@*****.**').first() user.active = False db.session.commit() with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**', 'password': '******', }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) print(data) self.assertTrue(data['status'] == 'fail') self.assertTrue(data['message'] == 'Unauthorized') self.assertEqual(response.status_code, 401)
def test_add_user_missing_password(self): """ Throw error if no password key """ add_admin_user('testuser', '*****@*****.**', 'testpass') user = User.query.filter_by(email='*****@*****.**').first() user.admin = True with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn('Invalid payload', data['message']) self.assertIn('fail', data['status'])
def test_delete_user_admin(self): user = add_user('testname', '*****@*****.**', 'testpass') # add admin user and log in add_admin_user('admin', '*****@*****.**', 'testpass') with self.client: login_response = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(login_response.data.decode())['token'] get_response = self.client.get( f'/users/{user.id}', content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(get_response.data.decode()) self.assertEqual(get_response.status_code, 200) self.assertIn('testname', data['data']['username']) self.assertIn('*****@*****.**', data['data']['email']) delete_response = self.client.delete( f'/users/{user.id}', content_type='application/json', headers={'Authorization': f'Bearer {token}'}) self.assertEqual(delete_response.status_code, 204)
def test_add_user(self): """POST /users add a user""" # How can I make admin login DRY add_admin_user('testuser', '*****@*****.**', 'testpass') with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) # get response data data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 201) # test 201 - CREATED self.assertIn('*****@*****.**', data['message']) # content contains email self.assertIn('success', data['status']) # return status message
def test_get_all_users(self): """ Ensure get all users behaves correctly """ # Add test users add_user('testuser1', '*****@*****.**', 'testpass') add_user('testuser2', '*****@*****.**', 'testpass') add_admin_user('admin', '*****@*****.**', 'testpass') with self.client: login_response = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(login_response.data.decode())['token'] response = self.client.get( '/users', content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data['data']['users']), 3) self.assertIn('success', data['status']) self.assertIn('testuser1', 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('testuser2', data['data']['users'][1]['username']) self.assertIn('*****@*****.**', data['data']['users'][1]['email']) self.assertTrue(data['data']['users'][1]['active']) self.assertFalse(data['data']['users'][1]['admin'])
def test_get_all_questions(self): """ Ensure getting all questions is working for admin""" # By default this user will have id=1. At least 1 user is required to add questions add_admin_user('testuser', '*****@*****.**', 'testpass') with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] # author_id = 1 add_question() add_question(1, 'Testing', 'print("Testing")', 'Testing', 'Easy') response = self.client.get( '/questions', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data['data']['questions']), 2) self.assertIn('test', data['data']['questions'][0]['body']) self.assertIn('test', data['data']['questions'][0]['test_code']) self.assertIn('Testing', data['data']['questions'][1]['body']) self.assertIn('print("Testing")', data['data']['questions'][1]['test_code']) self.assertIn('Testing', data['data']['questions'][1]['test_solution']) self.assertIn('Easy', data['data']['questions'][1]['difficulty'])
def test_add_user_invalid_json_keys(self): """ Ensure error is thrown if the JSON object does not have a username key. """ add_admin_user('alex', '*****@*****.**', 'password') 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.post( '/users', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn('Invalid payload.', data['message']) self.assertIn('fail', data['status'])
def test_add_user_duplicate_email(self): """Ensure error is thrown if the email already exists.""" add_admin_user('nikki', '*****@*****.**', 'password') 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.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn('Sorry. That email already exists.', data['message']) self.assertIn('fail', data['status'])
def test_add_user(self): """Ensure a new user can be added to the database""" add_admin_user('alex', '*****@*****.**', 'password') 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.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 201) self.assertIn('[email protected] was added!', data['message']) self.assertIn('success', data['status'])
def test_add_user_empty_payload(self): """ Throw error if JSON object is empty """ add_admin_user('testuser', '*****@*****.**', 'testpass') with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post( '/users', data=json.dumps({}), # empty payload content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn('Empty payload', data['message']) self.assertIn('fail', data['status'])
def test_add_question_admin(self): """ Ensure a new question can be added to the database """ # Must be authenticated to add question add_admin_user('testuser', '*****@*****.**', 'testpass') with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post('/questions', data=json.dumps({ 'author_id': 1, 'body': 'Sample question', 'test_code': 'testing', 'test_solution': 'still testing', 'difficulty': 'Moderate', }), content_type='application/json', headers=({ 'Authorization': f'Bearer {token}' })) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 201) self.assertIn('Question added', data['message']) self.assertIn('success', data['status'])
def test_add_user_duplicate_email(self): """ Throw error if email already exists """ add_admin_user('testuser', '*****@*****.**', 'testpass') with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) # Add duplicate email response = self.client.post( '/users', data=json.dumps({ 'username': '******', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', headers={'Authorization': f'Bearer {token}'}) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn('Email already exists', data['message']) self.assertIn('fail', data['status'])
def test_add_question_invalid_keys(self): """ Error thrown if invalid JSON object """ add_admin_user('testuser', '*****@*****.**', 'testpass') with self.client: response_login = self.client.post('/login/login', data=json.dumps({ 'email': '*****@*****.**', 'password': '******' }), content_type='application/json') token = json.loads(response_login.data.decode())['token'] response = self.client.post('/questions', data=json.dumps({'body': 'test'}), content_type='application/json', headers=({ 'Authorization': f'Bearer {token}' })) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) self.assertIn('Invalid payload', data['message']) self.assertIn('fail', data['status'])