def test_create_user_role_endpoint(self, mock_andela_get_user_by_email): location = LocationFactory.create() user = UserFactory() mock_andela_get_user_by_email.return_value = {'id': user.user_id} create_user_role('create_user_roles') new_role = RoleFactory.create() user_role_data = { 'roleId': new_role.id, 'emailAddress': self.user_email() } headers = self.headers() headers.update({'X-Location': location.id}) response = self.client().post( self.make_url(f'/roles/user'), data=self.encode_to_json_string(user_role_data), headers=headers) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 201) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['user_role']['roleId'], new_role.id)
def test_delete_user_role(self): create_user_role('delete_user_roles') new_role, _ = create_user_role('test_role') new_role.save() response = self.client().delete( self.make_url(f'/roles/user/delete/{new_role.id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(response_json['msg'], 'user_role deleted for user') self.assertEqual(response_json['payload']['status'], 'success')
def test_autocomplete_endpoint(self): role = RoleFactory() user_role = UserRoleFactory(role_id=role.id) user = UserFactory(slack_id='-LMnsyxrsj_TEYEAHCBk', user_type_id=user_role.id) create_user_role('view_user_roles') response = self.client().get(self.make_url('/roles/autocomplete'), query_string={'q': user_role.email[0]}, headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json self.assertEqual(response.status_code, 200) self.assertTrue(user_role.email in payload['msg'])
def test_list_user_endpoint(self): role = RoleFactory() user_role = UserRoleFactory(role_id=role.id) user = UserFactory(slack_id='-LMnsyxrsj_TEYEAHCBk', user_type_id=user_role.id) create_user_role('view_users') response = self.client().get(self.make_url(f'/users/{user.slack_id}/'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['user']['firstName'], user.first_name) self.assertEqual(response_json['payload']['user']['lastName'], user.last_name)
def test_get_single_permission_endpoint(self): create_user_role('view_permissions') new_role = RoleFactory.create() new_permission = PermissionFactory.create(keyword='delete_roles', name='delete_roles', role_id=new_role.id) response = self.client().get(self.make_url( f'/roles/{new_role.id}/permissions/{new_permission.id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['permission'][0]['id'], new_permission.id)
def test_disable_user_role_endpoint(self): create_user_role('delete_user_roles') new_role, _ = create_user_role('test_role') user_role_data = { 'roleId': new_role.role_id, 'userId': new_role.user_id } response = self.client().post( self.make_url(f'/roles/user/disable/'), data=self.encode_to_json_string(user_role_data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(response_json['msg'], 'user_role disabled for user') self.assertEqual(response_json['payload']['status'], 'success')
def test_get_role_permissions(self): new_role, _ = create_user_role('view_permissions') new_role.save() response = self.client().get( self.make_url(f'/roles/{new_role.id}/permissions'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['role_id'], new_role.id)
def test_get_user_role_endpoint(self): new_role, user_id = create_user_role('view_user_roles') new_role.save() role = RoleFactory() user_role = UserRoleFactory(role=role) user = UserFactory(user_type_id=user_role.id) response = self.client().get( self.make_url(f'/roles/user/{user_role.user_id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assert200(response) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['user_role'][0]['id'], user_role.id)