def test_delete_all_filtered_users_by_user_manager(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'] q = "role>=1" response = self.app.delete( '/api/users/all?filter=%s' % q, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(0, response.json['count'])
def test_create_record_by_admin(self): with app.app_context(): BaseCase.add_admin(self) admin_id = self.get_user_id('admin') 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": int(admin_id + 1) }) response = self.app.post('/api/records', headers={ "Content-Type": "application/json", "Authorization": authorization }, data=payload) self.assertEqual(404, response.status_code)
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_user_id_by_admin(self): with app.app_context(): BaseCase.add_user(self) user_id = BaseCase.get_user_id(self, 'user') 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/%d' % user_id, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(user_id, response.json['id']) self.assertEqual('user', response.json['username']) response = self.app.get('/api/users/%d' % manager_id, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(manager_id, response.json['id']) self.assertEqual('manager', response.json['username'])
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_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_delete_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-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']) record_id = int(response.json['id']) response = self.app.delete( '/api/records/%d' % record_id, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['count']) response = self.app.get('/api/records/%d' % record_id, headers={"Authorization": authorization}) self.assertEqual(404, response.status_code) response = self.app.delete( '/api/records/%d' % admin_record_id, headers={"Authorization": authorization}) self.assertEqual(404, response.status_code)