def test_registered_with_already_registered_user(self): """ Test registration with already registered email""" register_user(self) with self.client: response = register_user(self) data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'fail') self.assertTrue( data['message'] == 'User already exists. Please Log in.') self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 409)
def test_valid_logout(self): """ Test for logout before token expires """ with self.client: # user registration resp_register = register_user(self) data_register = json.loads(resp_register.data.decode()) self.assertTrue(data_register['status'] == 'success') self.assertTrue( data_register['message'] == 'Successfully registered.') self.assertTrue(data_register['Authorization']) self.assertTrue(resp_register.content_type == 'application/json') self.assertEqual(resp_register.status_code, 201) # user login resp_login = login_user(self) data_login = json.loads(resp_login.data.decode()) self.assertTrue(data_login['status'] == 'success') self.assertTrue(data_login['message'] == 'Successfully logged in.') self.assertTrue(data_login['Authorization']) self.assertTrue(resp_login.content_type == 'application/json') self.assertEqual(resp_login.status_code, 200) # valid token logout response = self.client.post( '/auth/logout', headers=dict( Authorization='Bearer ' + json.loads(resp_login.data.decode())['Authorization'])) data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'success') self.assertTrue(data['message'] == 'Successfully logged out.') self.assertEqual(response.status_code, 200)
def test_registration(self): """ Test for user registration """ with self.client: response = register_user(self) data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'success') self.assertTrue(data['message'] == 'Successfully registered.') self.assertTrue(data['Authorization']) self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 201)
def test_new_post_invalid_file_type(self): """ Test for new post wrong file type """ with self.client: resp_register = register_user(self) auth_data = json.loads(resp_register.data.decode()) token = auth_data['Authorization'] post_data = dict(image=(BytesIO(b"this is a test"), 'test.pdf'), caption='This is a caption') response = new_post(token, post_data, self) data = json.loads(response.data) self.assertTrue(data['status'] == 'fail') self.assertTrue( data['message'] == 'Please upload a valid file type') self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 400)
def test_new_post_no_image(self): """ Test for new post without image upload """ with self.client: resp_register = register_user(self) auth_data = json.loads(resp_register.data.decode()) token = auth_data['Authorization'] post_data = dict(caption='This is a caption') response = new_post(token, post_data, self) data = json.loads(response.data) self.assertTrue( data['message'] == 'The browser (or proxy) sent a request that this server could not understand.' ) self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 400)
def test_new_post(self): """ Test for new post """ with self.client: resp_register = register_user(self) auth_data = json.loads(resp_register.data.decode()) token = auth_data['Authorization'] file = BytesIO() image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0)) image.save(file, 'png') file.name = 'test.png' file.seek(0) post_data = dict(image=file, caption='This is a caption') response = new_post(token, post_data, self) data = json.loads(response.data) self.assertTrue(data['status'] == 'success') self.assertTrue( data['message'] == 'Successfully added post to db.') self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 201)
def test_registered_user_login(self): """ Test for login of registered-user login """ with self.client: # user registration resp_register = register_user(self) data_register = json.loads(resp_register.data.decode()) self.assertTrue(data_register['status'] == 'success') self.assertTrue( data_register['message'] == 'Successfully registered.') self.assertTrue(data_register['Authorization']) self.assertTrue(resp_register.content_type == 'application/json') self.assertEqual(resp_register.status_code, 201) # registered user login response = login_user(self) data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'success') self.assertTrue(data['message'] == 'Successfully logged in.') self.assertTrue(data['Authorization']) self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 200)
def test_new_post_no_caption(self): """ Test for new post without caption """ with self.client: resp_register = register_user(self) auth_data = json.loads(resp_register.data.decode()) token = auth_data['Authorization'] file = BytesIO() image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0)) image.save(file, 'png') file.name = 'test.png' file.seek(0) post_data = dict(image=file, ) response = new_post(token, post_data, self) data = json.loads(response.data) self.assertTrue( data['message'] == 'The browser (or proxy) sent a request that this server could not understand.' ) self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 400)
def test_valid_blacklisted_token_logout(self): """ Test for logout after a valid token gets blacklisted """ with self.client: # user registration resp_register = register_user(self) data_register = json.loads(resp_register.data.decode()) self.assertTrue(data_register['status'] == 'success') self.assertTrue( data_register['message'] == 'Successfully registered.') self.assertTrue(data_register['Authorization']) self.assertTrue(resp_register.content_type == 'application/json') self.assertEqual(resp_register.status_code, 201) # user login resp_login = login_user(self) data_login = json.loads(resp_login.data.decode()) self.assertTrue(data_login['status'] == 'success') self.assertTrue(data_login['message'] == 'Successfully logged in.') self.assertTrue(data_login['Authorization']) self.assertTrue(resp_login.content_type == 'application/json') self.assertEqual(resp_login.status_code, 200) # blacklist a valid token blacklist_token = BlacklistToken( token=json.loads(resp_login.data.decode())['Authorization']) db.session.add(blacklist_token) db.session.commit() # blacklisted valid token logout response = self.client.post( '/auth/logout', headers=dict( Authorization='Bearer ' + json.loads(resp_login.data.decode())['Authorization'])) data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'fail') self.assertTrue( data['message'] == 'Token blacklisted. Please log in again.') self.assertEqual(response.status_code, 401)