def test_add_organziation_duplicate_name(self): """Ensure duplicate names are not allowed.""" add_organization('Test Organization', '*****@*****.**') duplicate_organization = Organization( name='Test Organization', admin_email='*****@*****.**', ) db.session.add(duplicate_organization) self.assertRaises(IntegrityError, db.session.commit)
def test_set_admin_user_to_organization(self): # pylint: disable=invalid-name """Ensure user can be added to organization.""" organization = add_organization('Test Organization', '*****@*****.**') user = add_user('justatest', '*****@*****.**', 'test') organization.add_admin(user) db.session.commit() self.assertIn(user, organization.admin_users)
def test_add_organization(self): """Ensure organization model is created correctly.""" organization = add_organization('Test Organization', '*****@*****.**') self.assertTrue(organization.id) self.assertEqual(organization.name, 'Test Organization') self.assertEqual(organization.admin_email, '*****@*****.**') self.assertTrue(organization.created_at)
def test_add_user_to_organization(self): """Ensure user can be added to organization.""" organization = add_organization('Test Organization', '*****@*****.**') user = add_user('justatest', '*****@*****.**', 'test') organization.users.append(user) db.session.commit() self.assertIn(user, organization.users)
def test_add_duplicate_users_to_organization(self): # pylint: disable=invalid-name """Ensure user can only be added to organization once.""" organization = add_organization('Test Organization', '*****@*****.**') user = add_user('justatest', '*****@*****.**', 'test') with db.session.no_autoflush: organization.users.append(user) db.session.commit() organization.users.append(user) self.assertRaises(FlushError, db.session.commit)
def test_all_organizations(self): """Ensure get all organizations behaves correctly.""" add_organization('Test Organization', '*****@*****.**') add_organization('Test Organization Two', '*****@*****.**') with self.client: response = self.client.get( f'/api/v1/organizations', content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(data['data']['organizations']), 2) self.assertIn('Test Organization', data['data']['organizations'][0]['name']) self.assertIn( '*****@*****.**', data['data']['organizations'][0]['admin_email']) self.assertIn('Test Organization Two', data['data']['organizations'][1]['name']) self.assertIn( '*****@*****.**', data['data']['organizations'][1]['admin_email']) self.assertTrue('created_at' in data['data']['organizations'][0]) self.assertTrue('created_at' in data['data']['organizations'][1]) self.assertIn('success', data['status'])
def test_single_organization(self): """Ensure get single organization behaves correctly.""" organization = add_organization('Test Organization', '*****@*****.**') uuid = str(organization.id) with self.client: response = self.client.get( f'/api/v1/organizations/{uuid}', content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn('Test Organization', data['data']['organization']['name']) self.assertIn('*****@*****.**', data['data']['organization']['admin_email']) self.assertTrue('created_at' in data['data']['organization']) self.assertTrue('users' in data['data']['organization']) self.assertTrue('sample_groups' in data['data']['organization']) self.assertIn('success', data['status'])
def test_unauthenticated_add_user_to_organiztion(self): """Ensure unauthenticated user cannot attempt action.""" organization = add_organization('Test Organization', '*****@*****.**') user_uuid = str(uuid4()) with self.client: org_uuid = str(organization.id) response = self.client.post( f'/api/v1/organizations/{org_uuid}/users', data=json.dumps(dict( user_id=user_uuid, )), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 401) self.assertIn('Provide a valid auth token.', data['message']) self.assertIn('error', data['status'])
def test_single_organization_sample_groups(self): """Ensure getting sample groups for an organization behaves correctly.""" sample_group = add_sample_group('Pilot Sample Group') organization = add_organization('Test Organization', '*****@*****.**') organization.sample_groups = [sample_group] db.session.commit() uuid = str(organization.id) with self.client: response = self.client.get( f'/api/v1/organizations/{uuid}/sample_groups', content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertTrue(len(data['data']['sample_groups']) == 1) self.assertTrue('name' in data['data']['sample_groups'][0]) self.assertIn('success', data['status'])
def test_unauthorized_add_user_to_organiztion(self, auth_headers, *_): """Ensure user cannot be added to organization by non-organization admin user.""" organization = add_organization('Test Organization', '*****@*****.**') user_uuid = str(uuid4()) with self.client: org_uuid = str(organization.id) response = self.client.post( f'/api/v1/organizations/{org_uuid}/users', headers=auth_headers, data=json.dumps(dict( user_id=user_uuid, )), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 403) self.assertIn('You do not have permission to add a user to that group.', data['message']) self.assertIn('error', data['status'])
def test_single_organization_users(self): """Ensure getting users for an organization behaves correctly.""" user = add_user('test', '*****@*****.**', 'test') organization = add_organization('Test Organization', '*****@*****.**') organization.users = [user] db.session.commit() uuid = str(organization.id) with self.client: response = self.client.get( f'/api/v1/organizations/{uuid}/users', content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertTrue(len(data['data']['users']) == 1) self.assertTrue('username' in data['data']['users'][0]) self.assertTrue('email' in data['data']['users'][0]) self.assertIn('success', data['status'])
def test_add_user_to_organiztion(self, auth_headers, login_user): """Ensure user can be added to organization by admin user.""" organization = add_organization('Test Organization', '*****@*****.**') organization.add_admin(login_user) db.session.commit() user = add_user('new_user', '*****@*****.**', 'somepassword') with self.client: org_uuid = str(organization.id) response = self.client.post( f'/api/v1/organizations/{org_uuid}/users', headers=auth_headers, data=json.dumps(dict( user_id=str(user.id), )), content_type='application/json', ) data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn(user, organization.users) self.assertIn('success', data['status'])