def test_login_user(self):
     """Test logging in and logging out."""
     users = self.engine.users()
     users.register(username='******', password='******')
     response = users.login(username='******', password='******')
     self.assertEqual(len(response), 2)
     self.assertTrue(labels.ACCESS_TOKEN in response)
     self.assertTrue(labels.LINKS in response)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 2)
     self.assertTrue(hateoas.SERVICE in links)
     self.assertTrue(hateoas.user(hateoas.LOGOUT) in links)
     response = users.logout(response[labels.ACCESS_TOKEN])
     self.assertEqual(len(response), 2)
     self.assertTrue(labels.STATE in response)
     self.assertTrue(labels.LINKS in response)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 1)
     self.assertTrue(hateoas.user(hateoas.LOGIN) in links)
     response = users.logout('unknown')
     self.assertEqual(len(response), 2)
     self.assertTrue(labels.STATE in response)
     self.assertTrue(labels.LINKS in response)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 1)
     self.assertTrue(hateoas.user(hateoas.LOGIN) in links)
示例#2
0
 def test_init_from_environ(self):
     """Test initializing the engine API using the values of environment
     variables.
     """
     # Set environment variable for database and engine base directory
     os.environ[config.ENV_DATABASE] = CONNECT
     os.environ[config.ENV_BASEDIR] = TMP_DIR
     os.environ[config.ENV_SERVICE_NAME] = 'Test service'
     # Create engine without any arguments
     api = EngineApi()
     # The temporary base directory should contain sub-folders for templates
     # and uploaded files. The directory also contains the database file
     tmpl_dir = os.path.join(TMP_DIR, config.TEMPLATE_DIR)
     self.assertTrue(os.path.isdir(tmpl_dir))
     upload_dir = os.path.join(TMP_DIR, config.UPLOAD_DIR)
     self.assertTrue(os.path.isdir(upload_dir))
     db_file = os.path.join(TMP_DIR, 'test.db')
     self.assertTrue(os.path.isfile(db_file))
     # Get the service descriptor
     service = api.service_descriptor()
     self.assertEqual(service[labels.NAME], 'Test service')
     self.assertEqual(service[labels.VERSION], api.version)
     links = hateoas.deserialize(service[labels.LINKS])
     self.assertEqual(len(links), 5)
     self.assertTrue(hateoas.SELF in links)
     self.assertTrue(hateoas.user(hateoas.LOGIN) in links)
     self.assertTrue(hateoas.user(hateoas.LOGOUT) in links)
     self.assertTrue(hateoas.user(hateoas.REGISTER) in links)
     self.assertTrue(hateoas.benchmark(hateoas.LIST) in links)
     # Make sure to close the database connesction
     api.close()
示例#3
0
 def test_list_benchmarks(self):
     """Test benchmark listing."""
     repo = self.engine.benchmarks().repository
     benchmark1 = repo.add_benchmark(name='First competition',
                                     description='Some text',
                                     instructions='More text',
                                     src_dir=TEMPLATE_DIR)
     benchmark2 = repo.add_benchmark(name='Second competition',
                                     src_dir=TEMPLATE_DIR)
     response = self.engine.benchmarks().list_benchmarks()
     self.assertEqual(len(response), 2)
     self.assertTrue(labels.BENCHMARKS in response)
     self.assertTrue(labels.LINKS in response)
     benchmarks = response[labels.BENCHMARKS]
     self.assertEqual(len(benchmarks), 2)
     names = set()
     for benchmark in benchmarks:
         names.add(benchmark[labels.NAME])
         self.validate_benchmark(
             benchmark,
             has_description=benchmark[labels.ID] == benchmark1.identifier,
             has_instructions=benchmark[labels.ID] == benchmark1.identifier)
     self.assertTrue('First competition' in names)
     self.assertTrue('Second competition' in names)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 1)
     self.assertTrue(hateoas.SELF in links)
     # Get listing for invalid access token
     with self.assertRaises(err.UnauthenticatedAccessError):
         self.engine.benchmarks().list_benchmarks(access_token='unknown')
 def test_create_team(self):
     """Test creating a new team."""
     users = self.engine.users()
     users.register(username='******', password='******')
     response = users.login(username='******', password='******')
     access_token = response[labels.ACCESS_TOKEN]
     teams = self.engine.teams()
     response = teams.create_team(access_token=access_token, name='My Team')
     self.assertEqual(len(response), 5)
     self.assertTrue(labels.ID in response)
     self.assertTrue(labels.NAME in response)
     self.assertTrue(labels.OWNER_ID in response)
     self.assertTrue(labels.MEMBER_COUNT in response)
     self.assertTrue(labels.LINKS in response)
     self.assertEqual(response[labels.MEMBER_COUNT], 1)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 4)
     self.assertTrue(hateoas.SELF in links)
     self.assertTrue(hateoas.ADD in links)
     self.assertTrue(hateoas.DELETE in links)
     self.assertTrue(hateoas.UPLOAD in links)
     # Error when specifying unknonw user
     with self.assertRaises(err.UnknownUserError):
         teams.create_team(access_token=access_token,
                           name='No Team',
                           members=['myfriend'])
     response = users.register(username='******', password='******')
     user_id = response[labels.ID]
     response = teams.create_team(access_token=access_token,
                                  name='No Team',
                                  members=[user_id])
     self.assertEqual(response[labels.MEMBER_COUNT], 2)
 def test_team_get(self):
     """Test get team handle."""
     users = self.engine.users()
     users.register(username='******', password='******')[labels.ID]
     users.register(username='******', password='******')[labels.ID]
     token1 = users.login(username='******', password='******')[labels.ACCESS_TOKEN]
     token2 = users.login(username='******', password='******')[labels.ACCESS_TOKEN]
     teams = self.engine.teams()
     # User 1 and 2 are member of two teams.
     team_id = teams.create_team(access_token=token1,
                                 name='Team1')[labels.ID]
     response = teams.get_team(team_id=team_id, access_token=token1)
     self.assertEqual(len(response), 6)
     self.assertTrue(labels.ID in response)
     self.assertTrue(labels.NAME in response)
     self.assertTrue(labels.MEMBERS in response)
     self.assertTrue(labels.MEMBER_COUNT in response)
     self.assertTrue(labels.OWNER_ID in response)
     self.assertTrue(labels.LINKS in response)
     members = response[labels.MEMBERS]
     for user in members:
         self.assertEqual(len(user), 3)
         self.assertTrue(labels.ID in user)
         self.assertTrue(labels.USERNAME in user)
         self.assertTrue(labels.LINKS in user)
         links = hateoas.deserialize(user[labels.LINKS])
         self.assertEqual(len(links), 1)
         self.assertTrue(hateoas.DELETE in links)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 4)
     self.assertTrue(hateoas.SELF in links)
     self.assertTrue(hateoas.ADD in links)
     self.assertTrue(hateoas.DELETE in links)
     self.assertTrue(hateoas.UPLOAD in links)
     team_id = teams.create_team(access_token=token2,
                                 name='Team2')[labels.ID]
     with self.assertRaises(err.UnauthorizedAccessError):
         teams.get_team(team_id=team_id, access_token=token1)
     teams.get_team(team_id=team_id)
 def test_register_user(self):
     """Test new user registration via API."""
     users = self.engine.users()
     response = users.register(username='******', password='******')
     self.assertEqual(len(response), 3)
     self.assertTrue(labels.ID in response)
     self.assertTrue(labels.USERNAME in response)
     self.assertTrue(labels.LINKS in response)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 2)
     self.assertTrue(hateoas.user(hateoas.LOGIN) in links)
     self.assertTrue(hateoas.user(hateoas.LOGOUT) in links)
     # Errors when registering users with existing or invalid user names
     with self.assertRaises(err.DuplicateUserError):
         users.register(username='******', password='******')
     with self.assertRaises(err.ConstraintViolationError):
         users.register(username='******' * 256, password='******')
 def test_list_teams(self):
     """Test getting team listings."""
     users = self.engine.users()
     u1 = users.register(username='******', password='******')[labels.ID]
     u2 = users.register(username='******', password='******')[labels.ID]
     token1 = users.login(username='******', password='******')[labels.ACCESS_TOKEN]
     token2 = users.login(username='******', password='******')[labels.ACCESS_TOKEN]
     teams = self.engine.teams()
     # User 1 and 2 are member of two teams.
     teams.create_team(access_token=token1, name='Team1')
     teams.create_team(access_token=token2, name='Team2', members=[u1])
     teams.create_team(access_token=token2, name='Team3')
     tlist = teams.list_teams(access_token=token1)
     self.assertEqual(len(tlist[labels.TEAMS]), 2)
     links = hateoas.deserialize(tlist[labels.LINKS])
     self.assertEqual(len(links), 2)
     self.assertTrue(hateoas.SELF in links)
     self.assertTrue(hateoas.CREATE in links)
     tlist = teams.list_teams(access_token=token2)
     self.assertEqual(len(tlist[labels.TEAMS]), 2)
     tlist = teams.list_teams()
     self.assertEqual(len(tlist[labels.TEAMS]), 3)
示例#8
0
 def validate_benchmark(
         self,
         benchmark,
         is_handle=False,
         has_description=False,
         has_instructions=False,
         rels=[hateoas.SELF,
               hateoas.benchmark(hateoas.LEADERBOARD)]):
     """Validate a given serialization of a benchmark descriptor."""
     elements = [labels.ID, labels.NAME, labels.LINKS]
     if has_description:
         elements.append(labels.DESCRIPTION)
     if has_instructions:
         elements.append(labels.INSTRUCTIONS)
     if is_handle:
         elements.append(labels.PARAMETERS)
     self.assertEqual(len(benchmark), len(elements))
     for key in elements:
         self.assertTrue(key in benchmark)
     links = hateoas.deserialize(benchmark[labels.LINKS])
     self.assertEqual(len(links), len(rels))
     for key in rels:
         self.assertTrue(key in links)
 def test_upload_files(self):
     """Test uploading and deleting files."""
     # Create two users and one team
     users = self.engine.users()
     users.register(username='******', password='******')[labels.ID]
     users.register(username='******', password='******')[labels.ID]
     token1 = users.login(username='******', password='******')[labels.ACCESS_TOKEN]
     token2 = users.login(username='******', password='******')[labels.ACCESS_TOKEN]
     teams = self.engine.teams()
     team_id = teams.create_team(access_token=token1,
                                 name='Team1')[labels.ID]
     # Upload file
     response = teams.upload_file(file=FakeStream(),
                                  file_name='file.txt',
                                  team_id=team_id,
                                  access_token=token1)
     self.assertEqual(len(response), 5)
     self.assertTrue(labels.ID in response)
     self.assertTrue(labels.NAME in response)
     self.assertTrue(labels.CREATED_AT in response)
     self.assertTrue(labels.FILESIZE in response)
     self.assertTrue(labels.LINKS in response)
     links = hateoas.deserialize(response[labels.LINKS])
     self.assertEqual(len(links), 2)
     self.assertTrue(hateoas.DELETE in links)
     self.assertTrue(hateoas.DOWNLOAD in links)
     # Error when uploading as a non-member
     with self.assertRaises(err.UnauthorizedAccessError):
         teams.upload_file(file=FakeStream(),
                           file_name='file.txt',
                           team_id=team_id,
                           access_token=token2)
     # Get handle for uploaded file
     file_id = response[labels.ID]
     fh = teams.get_file(team_id=team_id,
                         file_id=file_id,
                         access_token=token1)
     self.assertEqual(fh, response)
     # Error when getting file as a non-member
     with self.assertRaises(err.UnauthorizedAccessError):
         teams.get_file(team_id=team_id,
                        file_id=file_id,
                        access_token=token2)
     # Error when deleting file as non member
     with self.assertRaises(err.UnauthorizedAccessError):
         teams.delete_file(team_id=team_id,
                           file_id=file_id,
                           access_token=token2)
     # Delete file
     response = teams.delete_file(team_id=team_id,
                                  file_id=file_id,
                                  access_token=token1)
     self.assertEqual(response, {labels.STATE: 'SUCCESS'})
     # Error when accessing or deleting a non-existing file
     with self.assertRaises(err.UnknownFileError):
         teams.get_file(team_id=team_id,
                        file_id=file_id,
                        access_token=token1)
     with self.assertRaises(err.UnknownFileError):
         teams.delete_file(team_id=team_id,
                           file_id=file_id,
                           access_token=token1)