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_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_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_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_delete_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.delete( '/api/users/%d' % user_id, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['count']) response = self.app.delete( '/api/users/%d' % manager_id, headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(1, response.json['count']) response = self.app.get('/api/users/%d' % user_id, headers={"Authorization": authorization}) self.assertEqual(404, response.status_code)
def test_read_all_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'] response = self.app.get('/api/users/all', headers={"Authorization": authorization}) self.assertEqual(200, response.status_code) self.assertEqual(2, response.json['_meta']['total_items'])