def test_create_record_by_user_change_user_id(self): with app.app_context(): BaseCase.add_user(self) user_id = self.get_user_id('user') BaseCase.add_user_manager(self) manager_id = self.get_user_id('manager') payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({ "date": "2020-01-01", "distance": 1000, "time": 3600, "latitude": 51.5, "longitude": 0.127, "user_id": manager_id }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(403, response.status_code)
def test_update_user_id_by_user_manager(self): with app.app_context(): BaseCase.add_user(self) user_id = BaseCase.get_user_id(self, 'user') BaseCase.add_user_manager(self) payload = json.dumps({ "username": "******", "password": "******" }) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({"username": "******"}) response = self.app.put('/api/users/%d' % user_id, headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(200, response.status_code) self.assertEqual(user_id, response.json['id']) self.assertEqual('new_user', response.json['username'])
def test_unsubscribe_by_non_subscriber(self): with app.app_context(): BaseCase.add_user(self) user_id = BaseCase.get_user_id(self, 'user') payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] response = self.app.post('/api/unsubscribe', headers={"Authorization": authorization}) self.assertEqual(201, response.status_code) self.assertEqual('Unsubscribed successfully', response.json['message']) response = self.app.post('/api/unsubscribe', headers={"Authorization": authorization}) self.assertEqual(201, response.status_code) self.assertEqual('Not a subscriber', response.json['message'])
def test_read_all_filtered_users_by_user_manager(self): with app.app_context(): BaseCase.add_user(self) BaseCase.add_user_manager(self) payload = json.dumps({ "username": "******", "password": "******" }) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] q = "role=1" response = self.app.get('/api/users/all?filter=%s' % q, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['_meta']['total_items'])
def test_update_admin_records_id_by_user(self): with app.app_context(): BaseCase.add_admin(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({ "date": "2020-01-01", "distance": 1000, "time": 3600, "latitude": 51.5, "longitude": 0.127 }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['id']) admin_record_id = int(response.json['id']) BaseCase.add_user(self) user_id = self.get_user_id('user') payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({"date": "2020-01-20", "distance": 100}) response = self.app.put('/api/records/%d' % admin_record_id, headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(404, response.status_code)
def test_auth_validation_int_password(self): with app.app_context(): BaseCase.add_user(self) payload = json.dumps({"username": "******", "password": 1}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(400, response.status_code)
def test_admin_token(self): with app.app_context(): BaseCase.add_admin(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token'])
def test_pagination_read_all_filtered_users_by_admin(self): with app.app_context(): BaseCase.add_user(self) user_id = BaseCase.get_user_id(self, 'user') payload = json.dumps({ "username": "******", "password": "******", "email": "*****@*****.**" }) response = self.app.post( '/api/users', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) BaseCase.add_user_manager(self) manager_id = BaseCase.get_user_id(self, 'manager') BaseCase.add_admin(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] response = self.app.get('/api/users/all?page=1', headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(2, response.json['_meta']['total_items']) self.assertEqual('admin', response.json['items'][0]['username']) self.assertEqual('manager', response.json['items'][1]['username']) self.assertTrue('next_page' in response.json) self.assertTrue('prev_page' not in response.json) q = "role!=2" response = self.app.get('/api/users/all?page=2&filter=%s' % q, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['_meta']['total_items']) self.assertEqual('user', response.json['items'][0]['username']) self.assertTrue('next_page' not in response.json) self.assertTrue('prev_page' in response.json) self.assertEqual(1, response.json['prev_page']) page = 3 response = self.app.get('/api/users/all?page=%d&filter=%s' % (page, q), headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(0, response.json['_meta']['total_items'])
def test_read_records_all_by_user(self): with app.app_context(): BaseCase.add_user(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] response = self.app.get('/api/records/all', headers={"Authorization": authorization}) self.assertEqual(403, response.status_code)
def test_delete_user_id_self_by_user(self): with app.app_context(): BaseCase.add_user(self) user_id = BaseCase.get_user_id(self, 'user') payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] response = self.app.delete( '/api/users/%d' % user_id, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['count'])
def test_delete_all_users_by_admin(self): with app.app_context(): BaseCase.add_user(self) BaseCase.add_user_manager(self) BaseCase.add_admin(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] response = self.app.delete( '/api/users/all', headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(2, response.json['count'])
def test_pagination_read_filtered_records_by_admin(self): with app.app_context(): BaseCase.add_user(self) user_id = self.get_user_id('user') payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({ "date": "2020-04-24", "distance": 1000, "time": 3600, "latitude": 51.5, "longitude": 0.127 }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['id']) record_id_24 = int(response.json['id']) payload = json.dumps({ "date": "2020-04-25", "distance": 1500, "time": 3600, "latitude": 51.5, "longitude": 0.127 }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['id']) record_id_25 = int(response.json['id']) BaseCase.add_admin(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({ "date": "2020-04-01", "distance": 1000, "time": 5000, "latitude": 51.5, "longitude": 0.127 }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['id']) admin_record_id = int(response.json['id']) q = "time=5000 or date>'2020-04-01' or distance<1500" response = self.app.get('/api/records/all?filter=%s&page=1' % q, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(2, response.json['_meta']['total_items']) self.assertEqual(2, response.json['_meta']['total_pages']) self.assertEqual(record_id_25, response.json['items'][0]['id']) self.assertEqual(record_id_24, response.json['items'][1]['id']) self.assertEqual(2, response.json['next_page']) self.assertTrue('prev_page' not in response.json) q = "time=5000 or date>'2020-04-01' or distance<1500" response = self.app.get( '/api/records/all?filter=%s&page=2&per_page=1' % q, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['_meta']['total_items']) self.assertEqual(3, response.json['_meta']['total_pages']) self.assertEqual(record_id_24, response.json['items'][0]['id']) self.assertTrue(3, response.json['next_page']) self.assertEqual(1, response.json['prev_page'])
def test_pagination_default_read_records_by_admin(self): with app.app_context(): BaseCase.add_user(self) user_id = self.get_user_id('user') payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({ "date": "2020-01-01", "distance": 1000, "time": 3600, "latitude": 51.5, "longitude": 0.127 }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(201, response.status_code) BaseCase.add_admin(self) payload = json.dumps({"username": "******", "password": "******"}) response = self.app.post( '/api/auth', headers={"Content-Type": "application/json"}, data=payload) self.assertEqual(201, response.status_code) self.assertIsNotNone(response.json['access_token']) authorization = "Bearer " + response.json['access_token'] payload = json.dumps({ "date": "2020-01-01", "distance": 1000, "time": 3600, "latitude": 51.5, "longitude": 0.127 }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(201, response.status_code) response = self.app.get('/api/records/all', headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(2, response.json['_meta']['total_items'])
def test_update_user_id_by_none(self): with app.app_context(): BaseCase.add_user(self) response = self.app.put('/api/users/1') self.assertEqual(400, response.status_code)