2014-09-23 15:19:31 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
import unittest
|
2015-09-16 19:34:20 +00:00
|
|
|
import datetime
|
2015-12-18 19:18:52 +00:00
|
|
|
import logging
|
2016-11-30 21:54:04 +00:00
|
|
|
import time
|
2015-12-18 19:18:52 +00:00
|
|
|
import re
|
2014-02-06 19:40:36 +00:00
|
|
|
import json as py_json
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2017-02-17 17:01:41 +00:00
|
|
|
from mock import patch
|
2016-04-05 19:27:45 +00:00
|
|
|
from calendar import timegm
|
2017-02-14 19:30:53 +00:00
|
|
|
from contextlib import contextmanager
|
2016-12-17 07:13:36 +00:00
|
|
|
from httmock import urlmatch, HTTMock, all_requests
|
2014-03-25 19:17:02 +00:00
|
|
|
from urllib import urlencode
|
|
|
|
from urlparse import urlparse, urlunparse, parse_qs
|
|
|
|
|
2015-12-18 19:18:52 +00:00
|
|
|
from playhouse.test_utils import assert_query_count, _QueryLogHandler
|
2016-04-05 19:27:45 +00:00
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
2015-05-08 01:11:15 +00:00
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
from endpoints.api import api_bp, api
|
2015-04-29 21:30:24 +00:00
|
|
|
from endpoints.building import PreparedBuild
|
2014-02-26 20:19:07 +00:00
|
|
|
from endpoints.webhooks import webhooks
|
2018-11-12 21:27:49 +00:00
|
|
|
from app import (app, config_provider, all_queues, dockerfile_build_queue, notification_queue,
|
|
|
|
storage)
|
2015-09-11 21:40:32 +00:00
|
|
|
from buildtrigger.basehandler import BuildTriggerHandler
|
2014-01-31 21:19:29 +00:00
|
|
|
from initdb import setup_database_for_testing, finished_database_for_testing
|
2018-05-23 21:08:00 +00:00
|
|
|
from data import database, model, appr_model
|
2018-07-08 07:15:43 +00:00
|
|
|
from data.appr_model.models import NEW_MODELS
|
2016-08-10 19:08:06 +00:00
|
|
|
from data.database import RepositoryActionCount, Repository as RepositoryTable
|
2019-01-08 19:03:28 +00:00
|
|
|
from data.logs_model import logs_model
|
|
|
|
from data.registry_model import registry_model
|
2016-06-07 22:12:11 +00:00
|
|
|
from test.helpers import assert_action_logged
|
2016-12-14 22:11:45 +00:00
|
|
|
from util.secscan.fake import fake_security_scanner
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.team import (
|
|
|
|
TeamMember, TeamMemberList, TeamMemberInvite, OrganizationTeam, TeamPermissions, InviteTeamMember
|
|
|
|
)
|
2017-03-03 22:23:23 +00:00
|
|
|
from endpoints.api.tag import RepositoryTagImages, RepositoryTag, RestoreTag, ListRepositoryTags
|
2016-03-07 15:07:41 +00:00
|
|
|
from endpoints.api.search import EntitySearch, ConductSearch
|
2014-03-18 23:34:26 +00:00
|
|
|
from endpoints.api.image import RepositoryImage, RepositoryImageList
|
2016-01-12 20:57:03 +00:00
|
|
|
from endpoints.api.build import RepositoryBuildStatus, RepositoryBuildList, RepositoryBuildResource
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.robot import (
|
|
|
|
UserRobotList, OrgRobot, OrgRobotList, UserRobot, RegenerateUserRobot, RegenerateOrgRobot)
|
|
|
|
from endpoints.api.trigger import (
|
|
|
|
BuildTriggerActivate, BuildTriggerSources, BuildTriggerSubdirs, TriggerBuildList,
|
|
|
|
ActivateBuildTrigger, BuildTrigger, BuildTriggerList, BuildTriggerAnalyze,
|
|
|
|
BuildTriggerFieldValues, BuildTriggerSourceNamespaces)
|
2014-07-28 18:58:12 +00:00
|
|
|
from endpoints.api.repoemail import RepositoryAuthorizedEmail
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.repositorynotification import (
|
|
|
|
RepositoryNotification, RepositoryNotificationList, TestRepositoryNotification)
|
2014-03-25 00:57:02 +00:00
|
|
|
from endpoints.api.user import (PrivateRepositories, ConvertToOrganization, Signout, Signin, User,
|
2014-07-28 22:23:46 +00:00
|
|
|
UserAuthorizationList, UserAuthorization, UserNotification,
|
2015-02-20 20:11:41 +00:00
|
|
|
UserNotificationList, StarredRepositoryList, StarredRepository)
|
2014-03-25 00:57:02 +00:00
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
from endpoints.api.repotoken import RepositoryToken, RepositoryTokenList
|
|
|
|
from endpoints.api.prototype import PermissionPrototype, PermissionPrototypeList
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.logs import (
|
|
|
|
UserLogs, OrgLogs, OrgAggregateLogs, UserAggregateLogs, RepositoryLogs, RepositoryAggregateLogs)
|
|
|
|
from endpoints.api.billing import (
|
|
|
|
UserCard, UserPlan, ListPlans, OrganizationCard, OrganizationPlan)
|
2014-03-18 23:34:26 +00:00
|
|
|
from endpoints.api.discovery import DiscoveryResource
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.api.error import Error
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.organization import (
|
|
|
|
OrganizationList, OrganizationMember, OrgPrivateRepositories, OrganizationMemberList,
|
|
|
|
Organization, ApplicationInformation, OrganizationApplications, OrganizationApplicationResource,
|
|
|
|
OrganizationApplicationResetClientSecret, Organization)
|
|
|
|
from endpoints.api.repository import (
|
|
|
|
RepositoryList, RepositoryVisibility, Repository, REPOS_PER_PAGE)
|
2014-03-18 23:34:26 +00:00
|
|
|
from endpoints.api.permission import (RepositoryUserPermission, RepositoryTeamPermission,
|
|
|
|
RepositoryTeamPermissionList, RepositoryUserPermissionList)
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.superuser import (
|
2017-12-22 21:45:49 +00:00
|
|
|
SuperUserLogs, SuperUserManagement, SuperUserServiceKeyManagement,
|
2018-08-21 15:40:59 +00:00
|
|
|
SuperUserServiceKey, SuperUserServiceKeyApproval, SuperUserTakeOwnership)
|
2017-07-24 15:05:15 +00:00
|
|
|
from endpoints.api.globalmessages import (
|
|
|
|
GlobalUserMessage,
|
|
|
|
GlobalUserMessages,)
|
2017-02-02 22:51:18 +00:00
|
|
|
from endpoints.api.secscan import RepositoryImageSecurity, RepositoryManifestSecurity
|
2016-07-18 22:20:00 +00:00
|
|
|
from endpoints.api.manifest import RepositoryManifestLabels, ManageRepositoryManifestLabel
|
2017-02-17 17:01:41 +00:00
|
|
|
from util.morecollections import AttrDict
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2014-03-19 22:21:58 +00:00
|
|
|
try:
|
|
|
|
app.register_blueprint(api_bp, url_prefix='/api')
|
|
|
|
except ValueError:
|
|
|
|
# This blueprint was already registered
|
|
|
|
pass
|
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
app.register_blueprint(webhooks, url_prefix='/webhooks')
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2015-05-08 02:25:23 +00:00
|
|
|
# The number of queries we run for guests on API calls.
|
|
|
|
BASE_QUERY_COUNT = 0
|
|
|
|
|
|
|
|
# The number of queries we run for logged in users on API calls.
|
2016-03-16 20:08:53 +00:00
|
|
|
BASE_LOGGEDIN_QUERY_COUNT = BASE_QUERY_COUNT + 1
|
2015-05-08 02:25:23 +00:00
|
|
|
|
|
|
|
# The number of queries we run for logged in users on API calls that check
|
|
|
|
# access permissions.
|
2016-03-16 20:08:53 +00:00
|
|
|
BASE_PERM_ACCESS_QUERY_COUNT = BASE_LOGGEDIN_QUERY_COUNT + 2
|
2015-05-08 02:25:23 +00:00
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
NO_ACCESS_USER = 'freshuser'
|
|
|
|
READ_ACCESS_USER = 'reader'
|
|
|
|
ADMIN_ACCESS_USER = 'devtable'
|
2014-02-03 23:18:33 +00:00
|
|
|
PUBLIC_USER = 'public'
|
|
|
|
|
2014-07-08 22:19:13 +00:00
|
|
|
ADMIN_ACCESS_EMAIL = 'jschorr@devtable.com'
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
ORG_REPO = 'orgrepo'
|
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
ORGANIZATION = 'buynlarge'
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
NEW_USER_DETAILS = {
|
|
|
|
'username': 'bobby',
|
|
|
|
'password': 'password',
|
|
|
|
'email': 'bobby@tables.com',
|
|
|
|
}
|
|
|
|
|
2014-03-20 19:46:13 +00:00
|
|
|
FAKE_APPLICATION_CLIENT_ID = 'deadbeef'
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-03-25 19:17:02 +00:00
|
|
|
CSRF_TOKEN_KEY = '_csrf_token'
|
|
|
|
CSRF_TOKEN = '123csrfforme'
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2017-02-03 21:04:31 +00:00
|
|
|
|
2017-02-14 20:20:51 +00:00
|
|
|
class AppConfigChange(object):
|
|
|
|
""" AppConfigChange takes a dictionary that overrides the global app config
|
|
|
|
for a given block of code. The values are restored on exit. """
|
2017-07-24 15:05:15 +00:00
|
|
|
|
2017-02-14 20:20:51 +00:00
|
|
|
def __init__(self, changes=None):
|
|
|
|
self._changes = changes or {}
|
|
|
|
self._originals = {}
|
|
|
|
self._to_rm = []
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
for key in self._changes.keys():
|
|
|
|
try:
|
|
|
|
self._originals[key] = app.config[key]
|
|
|
|
except KeyError:
|
|
|
|
self._to_rm.append(key)
|
|
|
|
app.config[key] = self._changes[key]
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
for key in self._originals.keys():
|
|
|
|
app.config[key] = self._originals[key]
|
|
|
|
|
|
|
|
for key in self._to_rm:
|
|
|
|
del app.config[key]
|
|
|
|
|
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
class ApiTestCase(unittest.TestCase):
|
2014-05-20 22:26:29 +00:00
|
|
|
maxDiff = None
|
|
|
|
|
2014-03-25 19:17:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def _add_csrf(without_csrf):
|
|
|
|
parts = urlparse(without_csrf)
|
|
|
|
query = parse_qs(parts[4])
|
|
|
|
query[CSRF_TOKEN_KEY] = CSRF_TOKEN
|
|
|
|
return urlunparse(list(parts[0:4]) + [urlencode(query)] + list(parts[5:]))
|
|
|
|
|
2016-12-09 04:46:31 +00:00
|
|
|
def url_for(self, resource_name, params=None, skip_csrf=False):
|
|
|
|
params = params or {}
|
2014-03-25 19:17:02 +00:00
|
|
|
url = api.url_for(resource_name, **params)
|
2016-12-09 04:46:31 +00:00
|
|
|
if not skip_csrf:
|
|
|
|
url = ApiTestCase._add_csrf(url)
|
2014-03-25 19:17:02 +00:00
|
|
|
return url
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
def setUp(self):
|
|
|
|
setup_database_for_testing(self)
|
|
|
|
self.app = app.test_client()
|
|
|
|
self.ctx = app.test_request_context()
|
|
|
|
self.ctx.__enter__()
|
2014-03-25 19:17:02 +00:00
|
|
|
self.setCsrfToken(CSRF_TOKEN)
|
2014-01-31 21:19:29 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
finished_database_for_testing(self)
|
2016-02-11 09:04:37 +00:00
|
|
|
config_provider.clear()
|
2014-01-31 21:19:29 +00:00
|
|
|
self.ctx.__exit__(True, None, None)
|
|
|
|
|
2014-03-25 19:17:02 +00:00
|
|
|
def setCsrfToken(self, token):
|
|
|
|
with self.app.session_transaction() as sess:
|
|
|
|
sess[CSRF_TOKEN_KEY] = token
|
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
@contextmanager
|
|
|
|
def toggleFeature(self, name, enabled):
|
|
|
|
import features
|
|
|
|
previous_value = getattr(features, name)
|
|
|
|
setattr(features, name, enabled)
|
|
|
|
yield
|
|
|
|
setattr(features, name, previous_value)
|
|
|
|
|
2014-03-19 00:32:37 +00:00
|
|
|
def getJsonResponse(self, resource_name, params={}, expected_code=200):
|
2014-03-18 23:34:26 +00:00
|
|
|
rv = self.app.get(api.url_for(resource_name, **params))
|
2014-03-19 00:32:37 +00:00
|
|
|
self.assertEquals(expected_code, rv.status_code)
|
2014-01-31 22:54:01 +00:00
|
|
|
data = rv.data
|
2014-02-06 19:40:36 +00:00
|
|
|
parsed = py_json.loads(data)
|
2014-01-31 22:54:01 +00:00
|
|
|
return parsed
|
|
|
|
|
2016-05-09 09:16:01 +00:00
|
|
|
def postResponse(self, resource_name, params={}, data={}, file=None, headers=None,
|
|
|
|
expected_code=200):
|
2016-02-11 09:04:37 +00:00
|
|
|
data = py_json.dumps(data)
|
2016-05-09 09:16:01 +00:00
|
|
|
|
|
|
|
headers = headers or {}
|
|
|
|
headers.update({"Content-Type": "application/json"})
|
2016-02-11 09:04:37 +00:00
|
|
|
|
|
|
|
if file is not None:
|
|
|
|
data = {'file': file}
|
|
|
|
headers = None
|
|
|
|
|
|
|
|
rv = self.app.post(self.url_for(resource_name, params), data=data, headers=headers)
|
2014-01-31 22:54:01 +00:00
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
|
|
|
return rv.data
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
def getResponse(self, resource_name, params={}, expected_code=200):
|
|
|
|
rv = self.app.get(api.url_for(resource_name, **params))
|
2014-01-31 22:54:01 +00:00
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
|
|
|
return rv.data
|
|
|
|
|
2014-03-26 23:42:29 +00:00
|
|
|
def putResponse(self, resource_name, params={}, data={}, expected_code=200):
|
2017-07-24 15:05:15 +00:00
|
|
|
rv = self.app.put(
|
|
|
|
self.url_for(resource_name, params), data=py_json.dumps(data),
|
|
|
|
headers={"Content-Type": "application/json"})
|
2014-02-28 05:12:09 +00:00
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
|
|
|
return rv.data
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
def deleteResponse(self, resource_name, params={}, expected_code=204):
|
2014-03-25 19:17:02 +00:00
|
|
|
rv = self.app.delete(self.url_for(resource_name, params))
|
2014-09-08 21:20:01 +00:00
|
|
|
|
|
|
|
if rv.status_code != expected_code:
|
|
|
|
print 'Mismatch data for resource DELETE %s: %s' % (resource_name, rv.data)
|
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
|
|
|
return rv.data
|
|
|
|
|
2016-12-14 21:24:06 +00:00
|
|
|
def deleteEmptyResponse(self, resource_name, params={}, expected_code=204):
|
|
|
|
rv = self.app.delete(self.url_for(resource_name, params))
|
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEquals(rv.data, '') # ensure response body empty
|
2016-12-14 21:24:06 +00:00
|
|
|
return
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
def postJsonResponse(self, resource_name, params={}, data={}, expected_code=200):
|
|
|
|
rv = self.app.post(
|
|
|
|
self.url_for(resource_name, params), data=py_json.dumps(data),
|
|
|
|
headers={"Content-Type": "application/json"})
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
if rv.status_code != expected_code:
|
2014-03-18 23:34:26 +00:00
|
|
|
print 'Mismatch data for resource POST %s: %s' % (resource_name, rv.data)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
2014-01-31 21:19:29 +00:00
|
|
|
data = rv.data
|
2014-02-06 19:40:36 +00:00
|
|
|
parsed = py_json.loads(data)
|
2014-01-31 21:19:29 +00:00
|
|
|
return parsed
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
def putJsonResponse(self, resource_name, params={}, data={}, expected_code=200, skip_csrf=False):
|
|
|
|
rv = self.app.put(
|
|
|
|
self.url_for(resource_name, params, skip_csrf), data=py_json.dumps(data),
|
|
|
|
headers={"Content-Type": "application/json"})
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
if rv.status_code != expected_code:
|
2014-03-18 23:34:26 +00:00
|
|
|
print 'Mismatch data for resource PUT %s: %s' % (resource_name, rv.data)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
self.assertEquals(rv.status_code, expected_code)
|
|
|
|
data = rv.data
|
2014-02-06 19:40:36 +00:00
|
|
|
parsed = py_json.loads(data)
|
2014-01-31 22:54:01 +00:00
|
|
|
return parsed
|
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
def assertNotInTeam(self, data, membername):
|
|
|
|
for memberData in data['members']:
|
|
|
|
if memberData['name'] == membername:
|
2016-11-28 23:39:28 +00:00
|
|
|
self.fail(membername + ' found in team: ' + data['name'])
|
2015-07-16 12:00:51 +00:00
|
|
|
|
2014-09-11 19:45:41 +00:00
|
|
|
def assertInTeam(self, data, membername):
|
2015-07-15 21:25:41 +00:00
|
|
|
for member_data in data['members']:
|
|
|
|
if member_data['name'] == membername:
|
2014-09-11 19:45:41 +00:00
|
|
|
return
|
|
|
|
|
2016-11-28 23:39:28 +00:00
|
|
|
self.fail(membername + ' not found in team: ' + data['name'])
|
2014-09-11 19:45:41 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
def login(self, username, password='password'):
|
2014-03-18 23:34:26 +00:00
|
|
|
return self.postJsonResponse(Signin, data=dict(username=username, password=password))
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2014-03-25 19:17:02 +00:00
|
|
|
class TestCSRFFailure(ApiTestCase):
|
|
|
|
def test_csrf_failure(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
|
|
|
# Make sure a simple post call succeeds.
|
2016-12-09 04:46:31 +00:00
|
|
|
self.putJsonResponse(User, data=dict(password='newpasswordiscool'))
|
2014-03-25 19:17:02 +00:00
|
|
|
|
|
|
|
# Change the session's CSRF token.
|
|
|
|
self.setCsrfToken('someinvalidtoken')
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-25 19:17:02 +00:00
|
|
|
# Verify that the call now fails.
|
2016-12-09 04:46:31 +00:00
|
|
|
self.putJsonResponse(User, data=dict(password='newpasswordiscool'), expected_code=403)
|
|
|
|
|
|
|
|
def test_csrf_failure_empty_token(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
|
|
|
# Change the session's CSRF token to be empty.
|
|
|
|
self.setCsrfToken('')
|
|
|
|
|
|
|
|
# Verify that the call now fails.
|
|
|
|
self.putJsonResponse(User, data=dict(password='newpasswordiscool'), expected_code=403)
|
|
|
|
|
|
|
|
def test_csrf_failure_missing_token(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
|
|
|
# Make sure a simple post call without a token at all fails.
|
|
|
|
self.putJsonResponse(User, data=dict(password='newpasswordiscool'), skip_csrf=True,
|
|
|
|
expected_code=403)
|
|
|
|
|
|
|
|
# Change the session's CSRF token to be empty.
|
|
|
|
self.setCsrfToken('')
|
|
|
|
|
|
|
|
# Verify that the call still fails.
|
|
|
|
self.putJsonResponse(User, data=dict(password='newpasswordiscool'), skip_csrf=True,
|
2014-03-25 19:17:02 +00:00
|
|
|
expected_code=403)
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
class TestDiscovery(ApiTestCase):
|
|
|
|
def test_discovery(self):
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(DiscoveryResource)
|
2015-06-29 18:38:21 +00:00
|
|
|
assert 'paths' in json
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2016-04-11 20:20:11 +00:00
|
|
|
class TestErrorDescription(ApiTestCase):
|
2016-06-02 15:59:16 +00:00
|
|
|
def test_get_error(self):
|
|
|
|
json = self.getJsonResponse(Error, params=dict(error_type='not_found'))
|
|
|
|
assert json['title'] == 'not_found'
|
|
|
|
assert 'type' in json
|
|
|
|
assert 'description' in json
|
2016-04-11 18:51:58 +00:00
|
|
|
|
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
class TestPlans(ApiTestCase):
|
|
|
|
def test_plans(self):
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(ListPlans)
|
2014-01-31 21:19:29 +00:00
|
|
|
found = set([])
|
|
|
|
for method_info in json['plans']:
|
|
|
|
found.add(method_info['stripeId'])
|
|
|
|
|
|
|
|
assert 'free' in found
|
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
class TestLoggedInUser(ApiTestCase):
|
|
|
|
def test_guest(self):
|
2014-03-19 00:32:37 +00:00
|
|
|
self.getJsonResponse(User, expected_code=401)
|
2014-01-31 21:19:29 +00:00
|
|
|
|
|
|
|
def test_user(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(User)
|
2014-01-31 21:19:29 +00:00
|
|
|
assert json['anonymous'] == False
|
|
|
|
assert json['username'] == READ_ACCESS_USER
|
|
|
|
|
2015-02-24 22:50:54 +00:00
|
|
|
|
2015-02-20 20:11:41 +00:00
|
|
|
class TestUserStarredRepositoryList(ApiTestCase):
|
|
|
|
def test_get_stars_guest(self):
|
|
|
|
self.getJsonResponse(StarredRepositoryList, expected_code=401)
|
|
|
|
|
|
|
|
def test_get_stars_user(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2015-05-08 02:25:23 +00:00
|
|
|
|
|
|
|
# Queries: Base + the list query
|
2016-03-16 20:08:53 +00:00
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 1):
|
2015-05-08 02:25:23 +00:00
|
|
|
self.getJsonResponse(StarredRepositoryList, expected_code=200)
|
2015-02-20 20:11:41 +00:00
|
|
|
|
|
|
|
def test_star_repo_guest(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(StarredRepositoryList, data={
|
|
|
|
'namespace': 'public',
|
|
|
|
'repository': 'publicrepo',
|
|
|
|
}, expected_code=401)
|
2015-02-20 20:11:41 +00:00
|
|
|
|
2016-04-11 20:34:40 +00:00
|
|
|
def test_star_and_unstar_repo_user(self):
|
2015-02-20 20:11:41 +00:00
|
|
|
self.login(READ_ACCESS_USER)
|
2015-05-08 02:25:23 +00:00
|
|
|
|
|
|
|
# Queries: Base + the list query
|
2016-03-16 20:08:53 +00:00
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 1):
|
2015-05-08 02:25:23 +00:00
|
|
|
json = self.getJsonResponse(StarredRepositoryList)
|
|
|
|
assert json['repositories'] == []
|
2015-02-20 20:11:41 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(StarredRepositoryList, data={
|
|
|
|
'namespace': 'public',
|
|
|
|
'repository': 'publicrepo',
|
|
|
|
}, expected_code=201)
|
2015-02-20 20:11:41 +00:00
|
|
|
assert json['namespace'] == 'public'
|
|
|
|
assert json['repository'] == 'publicrepo'
|
|
|
|
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(StarredRepository, params=dict(repository='public/publicrepo'),
|
2017-07-24 15:05:15 +00:00
|
|
|
expected_code=204)
|
2015-02-20 20:11:41 +00:00
|
|
|
|
2015-02-24 22:50:54 +00:00
|
|
|
json = self.getJsonResponse(StarredRepositoryList)
|
|
|
|
assert json['repositories'] == []
|
2015-02-20 20:11:41 +00:00
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-07-28 22:23:46 +00:00
|
|
|
class TestUserNotification(ApiTestCase):
|
|
|
|
def test_get(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
json = self.getJsonResponse(UserNotificationList)
|
|
|
|
|
|
|
|
# Make sure each notification can be retrieved.
|
|
|
|
for notification in json['notifications']:
|
|
|
|
njson = self.getJsonResponse(UserNotification, params=dict(uuid=notification['id']))
|
|
|
|
self.assertEquals(notification['id'], njson['id'])
|
|
|
|
|
|
|
|
# Update a notification.
|
|
|
|
assert json['notifications']
|
|
|
|
assert not json['notifications'][0]['dismissed']
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2016-03-30 18:46:31 +00:00
|
|
|
notification = json['notifications'][0]
|
2014-07-28 22:23:46 +00:00
|
|
|
pjson = self.putJsonResponse(UserNotification, params=dict(uuid=notification['id']),
|
|
|
|
data=dict(dismissed=True))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 22:23:46 +00:00
|
|
|
self.assertEquals(True, pjson['dismissed'])
|
|
|
|
|
2016-03-30 18:46:31 +00:00
|
|
|
def test_org_notifications(self):
|
|
|
|
# Create a notification on the organization.
|
|
|
|
org = model.user.get_user_or_org(ORGANIZATION)
|
|
|
|
model.notification.create_notification('test_notification', org, {'org': 'notification'})
|
|
|
|
|
|
|
|
# Ensure it is visible to the org admin.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
json = self.getJsonResponse(UserNotificationList)
|
|
|
|
notification = json['notifications'][0]
|
|
|
|
|
|
|
|
self.assertEquals(notification['kind'], 'test_notification')
|
|
|
|
self.assertEquals(notification['metadata'], {'org': 'notification'})
|
|
|
|
|
|
|
|
# Ensure it is not visible to an org member.
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
json = self.getJsonResponse(UserNotificationList)
|
|
|
|
self.assertEquals(0, len(json['notifications']))
|
|
|
|
|
2014-07-28 22:23:46 +00:00
|
|
|
|
2014-03-06 23:36:52 +00:00
|
|
|
class TestGetUserPrivateAllowed(ApiTestCase):
|
2014-01-31 21:19:29 +00:00
|
|
|
def test_nonallowed(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(PrivateRepositories)
|
2014-01-31 21:19:29 +00:00
|
|
|
assert json['privateCount'] == 0
|
2014-03-06 23:36:52 +00:00
|
|
|
assert not json['privateAllowed']
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2016-04-11 20:20:11 +00:00
|
|
|
def test_allowed(self):
|
2014-01-31 21:19:29 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
# Change the subscription of the namespace.
|
2017-12-15 19:28:57 +00:00
|
|
|
self.putJsonResponse(UserPlan, data=dict(plan='personal-2018'))
|
2015-09-15 18:33:35 +00:00
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(PrivateRepositories)
|
2014-07-15 19:13:58 +00:00
|
|
|
assert json['privateCount'] >= 6
|
2015-09-15 18:33:35 +00:00
|
|
|
assert not json['privateAllowed']
|
|
|
|
|
|
|
|
# Change the subscription of the namespace.
|
2017-12-15 19:28:57 +00:00
|
|
|
self.putJsonResponse(UserPlan, data=dict(plan='bus-large-2018'))
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
json = self.getJsonResponse(PrivateRepositories)
|
2014-03-06 23:36:52 +00:00
|
|
|
assert json['privateAllowed']
|
2014-01-31 21:19:29 +00:00
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
class TestConvertToOrganization(ApiTestCase):
|
|
|
|
def test_sameadminuser(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(ConvertToOrganization, data={
|
|
|
|
'adminUser': READ_ACCESS_USER,
|
|
|
|
'adminPassword': 'password',
|
|
|
|
'plan': 'free'
|
|
|
|
}, expected_code=400)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2016-04-11 18:51:58 +00:00
|
|
|
self.assertEqual('The admin user is not valid', json['detail'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2015-04-30 00:30:37 +00:00
|
|
|
def test_sameadminuser_by_email(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(ConvertToOrganization, data={
|
|
|
|
'adminUser': 'no1@thanks.com',
|
|
|
|
'adminPassword': 'password',
|
|
|
|
'plan': 'free'
|
|
|
|
}, expected_code=400)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2016-04-11 18:51:58 +00:00
|
|
|
self.assertEqual('The admin user is not valid', json['detail'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_invalidadminuser(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(ConvertToOrganization, data={
|
|
|
|
'adminUser': 'unknownuser',
|
|
|
|
'adminPassword': 'password',
|
|
|
|
'plan': 'free'
|
|
|
|
}, expected_code=400)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEqual('The admin user credentials are not valid', json['detail'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_invalidadminpassword(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(ConvertToOrganization, data={
|
|
|
|
'adminUser': ADMIN_ACCESS_USER,
|
|
|
|
'adminPassword': 'invalidpass',
|
|
|
|
'plan': 'free'
|
|
|
|
}, expected_code=400)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEqual('The admin user credentials are not valid', json['detail'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_convert(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2016-04-14 21:39:45 +00:00
|
|
|
|
|
|
|
# Add at least one permission for the read-user.
|
|
|
|
read_user = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
simple_repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
read_role = database.Role.get(name='read')
|
|
|
|
|
|
|
|
database.RepositoryPermission.create(user=read_user, repository=simple_repo, role=read_role)
|
|
|
|
|
|
|
|
# Convert the read user into an organization.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(ConvertToOrganization, data={
|
|
|
|
'adminUser': ADMIN_ACCESS_USER,
|
|
|
|
'adminPassword': 'password',
|
|
|
|
'plan': 'free'
|
|
|
|
})
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
self.assertEqual(True, json['success'])
|
|
|
|
|
|
|
|
# Verify the organization exists.
|
2015-07-15 21:25:41 +00:00
|
|
|
organization = model.organization.get_organization(READ_ACCESS_USER)
|
2014-01-31 22:54:01 +00:00
|
|
|
assert organization is not None
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
# Verify the admin user is the org's admin.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname=READ_ACCESS_USER))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals(READ_ACCESS_USER, json['name'])
|
|
|
|
self.assertEquals(True, json['is_admin'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2016-04-14 21:39:45 +00:00
|
|
|
# Verify the now-org has no permissions.
|
2017-07-24 15:05:15 +00:00
|
|
|
count = (database.RepositoryPermission.select()
|
|
|
|
.where(database.RepositoryPermission.user == organization).count())
|
2016-04-14 21:39:45 +00:00
|
|
|
self.assertEquals(0, count)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2014-07-08 22:19:13 +00:00
|
|
|
def test_convert_via_email(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(ConvertToOrganization, data={
|
|
|
|
'adminUser': ADMIN_ACCESS_EMAIL,
|
|
|
|
'adminPassword': 'password',
|
|
|
|
'plan': 'free'
|
|
|
|
})
|
2014-07-08 22:19:13 +00:00
|
|
|
|
|
|
|
self.assertEqual(True, json['success'])
|
|
|
|
|
|
|
|
# Verify the organization exists.
|
2015-07-15 21:25:41 +00:00
|
|
|
organization = model.organization.get_organization(READ_ACCESS_USER)
|
2014-07-08 22:19:13 +00:00
|
|
|
assert organization is not None
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-08 22:19:13 +00:00
|
|
|
# Verify the admin user is the org's admin.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname=READ_ACCESS_USER))
|
2014-07-08 22:19:13 +00:00
|
|
|
|
|
|
|
self.assertEquals(READ_ACCESS_USER, json['name'])
|
|
|
|
self.assertEquals(True, json['is_admin'])
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
class TestChangeUserDetails(ApiTestCase):
|
|
|
|
def test_changepassword(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(User, data=dict(password='newpasswordiscool'))
|
2014-01-31 22:54:01 +00:00
|
|
|
self.login(READ_ACCESS_USER, password='newpasswordiscool')
|
2014-09-03 19:41:25 +00:00
|
|
|
|
2014-09-18 21:36:26 +00:00
|
|
|
def test_changepassword_unicode(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(User, data=dict(password=u'someunicode北京市pass'))
|
2014-09-22 22:49:52 +00:00
|
|
|
self.login(READ_ACCESS_USER, password=u'someunicode北京市pass')
|
2014-09-18 21:36:26 +00:00
|
|
|
|
2014-09-03 19:41:25 +00:00
|
|
|
def test_changeeemail(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(User, data=dict(email='test+foo@devtable.com'))
|
2014-09-03 19:41:25 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
def test_changeinvoiceemail(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(User, data=dict(invoice_email=True))
|
2014-01-31 22:54:01 +00:00
|
|
|
self.assertEquals(True, json['invoice_email'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(User, data=dict(invoice_email=False))
|
2014-01-31 22:54:01 +00:00
|
|
|
self.assertEquals(False, json['invoice_email'])
|
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
def test_changeusername_temp(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
user = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
model.user.create_user_prompt(user, 'confirm_username')
|
|
|
|
self.assertTrue(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
# Add a robot under the user's namespace.
|
|
|
|
model.user.create_robot('somebot', user)
|
|
|
|
|
|
|
|
# Rename the user.
|
|
|
|
json = self.putJsonResponse(User, data=dict(username='someotherusername'))
|
|
|
|
|
|
|
|
# Ensure the username was changed.
|
|
|
|
self.assertEquals('someotherusername', json['username'])
|
|
|
|
self.assertFalse(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
# Ensure the robot was changed.
|
|
|
|
self.assertIsNone(model.user.get_user(READ_ACCESS_USER + '+somebot'))
|
|
|
|
self.assertIsNotNone(model.user.get_user('someotherusername+somebot'))
|
|
|
|
|
|
|
|
def test_changeusername_temp_samename(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
user = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
model.user.create_user_prompt(user, 'confirm_username')
|
|
|
|
self.assertTrue(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
json = self.putJsonResponse(User, data=dict(username=READ_ACCESS_USER))
|
|
|
|
|
|
|
|
# Ensure the username was not changed but they are no longer temporarily named.
|
|
|
|
self.assertEquals(READ_ACCESS_USER, json['username'])
|
|
|
|
self.assertFalse(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
def test_changeusername_notallowed(self):
|
|
|
|
with self.toggleFeature('USER_RENAME', False):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
self.assertFalse(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
json = self.putJsonResponse(User, data=dict(username='someotherusername'))
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['username'])
|
|
|
|
self.assertTrue('prompts' in json)
|
|
|
|
|
|
|
|
self.assertIsNone(model.user.get_user('someotherusername'))
|
|
|
|
self.assertIsNotNone(model.user.get_user(ADMIN_ACCESS_USER))
|
|
|
|
|
|
|
|
def test_changeusername_allowed(self):
|
|
|
|
with self.toggleFeature('USER_RENAME', True):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
self.assertFalse(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
json = self.putJsonResponse(User, data=dict(username='someotherusername'))
|
|
|
|
self.assertEquals('someotherusername', json['username'])
|
|
|
|
self.assertTrue('prompts' in json)
|
|
|
|
|
|
|
|
self.assertIsNotNone(model.user.get_user('someotherusername'))
|
|
|
|
self.assertIsNone(model.user.get_user(ADMIN_ACCESS_USER))
|
|
|
|
|
|
|
|
def test_changeusername_already_used(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
user = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
model.user.create_user_prompt(user, 'confirm_username')
|
|
|
|
self.assertTrue(model.user.has_user_prompt(user, 'confirm_username'))
|
|
|
|
|
|
|
|
# Try to change to a used username.
|
|
|
|
self.putJsonResponse(User, data=dict(username=ADMIN_ACCESS_USER), expected_code=400)
|
|
|
|
|
|
|
|
# Change to a new username.
|
|
|
|
self.putJsonResponse(User, data=dict(username='unusedusername'))
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
class TestCreateNewUser(ApiTestCase):
|
|
|
|
def test_existingusername(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(User, data=dict(username=READ_ACCESS_USER, password='password',
|
|
|
|
email='test@example.com'), expected_code=400)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2016-04-11 18:51:58 +00:00
|
|
|
self.assertEquals('The username already exists', json['detail'])
|
2014-04-08 00:37:02 +00:00
|
|
|
|
|
|
|
def test_trycreatetooshort(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(User, data=dict(username='a', password='password',
|
|
|
|
email='test@example.com'), expected_code=400)
|
2014-04-08 00:37:02 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEquals(
|
|
|
|
'Invalid namespace a: Namespace must be between 2 and 255 characters in length',
|
|
|
|
json['detail'])
|
2014-04-08 00:37:02 +00:00
|
|
|
|
|
|
|
def test_trycreateregexmismatch(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(User, data=dict(username='auserName', password='password',
|
|
|
|
email='test@example.com'), expected_code=400)
|
2014-04-08 00:37:02 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEquals(
|
|
|
|
'Invalid namespace auserName: Namespace must match expression ^([a-z0-9]+(?:[._-][a-z0-9]+)*)$',
|
|
|
|
json['detail'])
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
def test_createuser(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
data = self.postJsonResponse(User, data=NEW_USER_DETAILS, expected_code=200)
|
2014-09-23 15:33:52 +00:00
|
|
|
self.assertEquals(True, data['awaiting_verification'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2016-12-17 07:13:36 +00:00
|
|
|
def test_createuser_captcha(self):
|
|
|
|
@urlmatch(netloc=r'(.*\.)?google.com', path='/recaptcha/api/siteverify')
|
|
|
|
def captcha_endpoint(url, request):
|
|
|
|
if url.query.find('response=somecode') > 0:
|
|
|
|
return {'status_code': 200, 'content': py_json.dumps({'success': True})}
|
|
|
|
else:
|
|
|
|
return {'status_code': 400, 'content': py_json.dumps({'success': False})}
|
|
|
|
|
|
|
|
with HTTMock(captcha_endpoint):
|
|
|
|
with self.toggleFeature('RECAPTCHA', True):
|
|
|
|
# Try with a missing captcha.
|
|
|
|
self.postResponse(User, data=NEW_USER_DETAILS, expected_code=400)
|
|
|
|
|
|
|
|
# Try with an invalid captcha.
|
|
|
|
details = dict(NEW_USER_DETAILS)
|
|
|
|
details['recaptcha_response'] = 'someinvalidcode'
|
|
|
|
self.postResponse(User, data=details, expected_code=400)
|
|
|
|
|
|
|
|
# Try with a valid captcha.
|
|
|
|
details = dict(NEW_USER_DETAILS)
|
|
|
|
details['recaptcha_response'] = 'somecode'
|
|
|
|
self.postResponse(User, data=details, expected_code=200)
|
|
|
|
|
2014-09-11 19:45:41 +00:00
|
|
|
def test_createuser_withteaminvite(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
inviter = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
team = model.team.get_organization_team(ORGANIZATION, 'owners')
|
2015-07-16 12:00:51 +00:00
|
|
|
invite = model.team.add_or_invite_to_team(inviter, team, None, NEW_USER_DETAILS['email'])
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
details = {'invite_code': invite.invite_token}
|
2015-07-15 21:25:41 +00:00
|
|
|
details.update(NEW_USER_DETAILS)
|
2014-09-11 19:45:41 +00:00
|
|
|
|
2014-09-23 15:33:52 +00:00
|
|
|
data = self.postJsonResponse(User, data=details, expected_code=200)
|
2014-09-11 19:45:41 +00:00
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
# Make sure the user is verified since the email address of the user matches
|
|
|
|
# that of the team invite.
|
|
|
|
self.assertFalse('awaiting_verification' in data)
|
|
|
|
|
|
|
|
# Make sure the user was not (yet) added to the team.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2015-07-16 12:00:51 +00:00
|
|
|
self.assertNotInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
|
|
|
def test_createuser_withteaminvite_differentemails(self):
|
|
|
|
inviter = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
team = model.team.get_organization_team(ORGANIZATION, 'owners')
|
|
|
|
invite = model.team.add_or_invite_to_team(inviter, team, None, 'differentemail@example.com')
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
details = {'invite_code': invite.invite_token}
|
2015-07-16 12:00:51 +00:00
|
|
|
details.update(NEW_USER_DETAILS)
|
|
|
|
|
|
|
|
data = self.postJsonResponse(User, data=details, expected_code=200)
|
|
|
|
|
|
|
|
# Make sure the user is *not* verified since the email address of the user
|
|
|
|
# does not match that of the team invite.
|
|
|
|
self.assertTrue(data['awaiting_verification'])
|
|
|
|
|
|
|
|
# Make sure the user was not (yet) added to the team.
|
2014-09-11 19:45:41 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2015-07-16 12:00:51 +00:00
|
|
|
self.assertNotInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
2016-11-28 23:39:28 +00:00
|
|
|
def test_createuser_withmultipleteaminvites(self):
|
|
|
|
inviter = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
owners_team = model.team.get_organization_team(ORGANIZATION, 'owners')
|
|
|
|
readers_team = model.team.get_organization_team(ORGANIZATION, 'readers')
|
|
|
|
other_owners_team = model.team.get_organization_team('library', 'owners')
|
|
|
|
|
|
|
|
owners_invite = model.team.add_or_invite_to_team(inviter, owners_team, None,
|
|
|
|
NEW_USER_DETAILS['email'])
|
|
|
|
|
|
|
|
readers_invite = model.team.add_or_invite_to_team(inviter, readers_team, None,
|
|
|
|
NEW_USER_DETAILS['email'])
|
|
|
|
|
|
|
|
other_owners_invite = model.team.add_or_invite_to_team(inviter, other_owners_team, None,
|
|
|
|
NEW_USER_DETAILS['email'])
|
|
|
|
|
|
|
|
# Create the user and ensure they have a verified email address.
|
2017-07-24 15:05:15 +00:00
|
|
|
details = {'invite_code': owners_invite.invite_token}
|
2016-11-28 23:39:28 +00:00
|
|
|
details.update(NEW_USER_DETAILS)
|
|
|
|
|
|
|
|
data = self.postJsonResponse(User, data=details, expected_code=200)
|
|
|
|
|
|
|
|
# Make sure the user is verified since the email address of the user matches
|
|
|
|
# that of the team invite.
|
|
|
|
self.assertFalse('awaiting_verification' in data)
|
|
|
|
|
|
|
|
# Make sure the user was not (yet) added to the teams.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertNotInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertNotInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname='library', teamname='owners'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertNotInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
|
|
|
# Accept the first invitation.
|
|
|
|
self.login(NEW_USER_DETAILS['username'])
|
|
|
|
self.putJsonResponse(TeamMemberInvite, params=dict(code=owners_invite.invite_token))
|
|
|
|
|
|
|
|
# Make sure both codes are now invalid.
|
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=owners_invite.invite_token),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=readers_invite.invite_token),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
# Make sure the user is now in the two invited teams under the organization, but not
|
|
|
|
# in the other org's team.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname='library', teamname='owners'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertNotInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
|
|
|
# Accept the second invitation.
|
|
|
|
self.login(NEW_USER_DETAILS['username'])
|
|
|
|
self.putJsonResponse(TeamMemberInvite, params=dict(code=other_owners_invite.invite_token))
|
|
|
|
|
|
|
|
# Make sure the user was added to the other organization.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname='library', teamname='owners'))
|
2016-11-28 23:39:28 +00:00
|
|
|
self.assertInTeam(json, NEW_USER_DETAILS['username'])
|
|
|
|
|
|
|
|
# Make sure the invitation codes are now invalid.
|
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=other_owners_invite.invite_token),
|
|
|
|
expected_code=400)
|
|
|
|
|
2014-09-11 19:45:41 +00:00
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
class TestDeleteNamespace(ApiTestCase):
|
|
|
|
def test_deletenamespaces(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-04-25 03:08:28 +00:00
|
|
|
# Try to first delete the user. Since they are the sole admin of three orgs, it should fail.
|
2017-03-21 01:06:37 +00:00
|
|
|
with check_transitive_modifications():
|
2016-08-09 21:58:33 +00:00
|
|
|
self.deleteResponse(User, expected_code=400)
|
|
|
|
|
2017-04-25 03:08:28 +00:00
|
|
|
# Delete the three orgs, checking in between.
|
2017-03-21 01:06:37 +00:00
|
|
|
with check_transitive_modifications():
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(Organization, params=dict(orgname=ORGANIZATION), expected_code=204)
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteResponse(User, expected_code=400) # Should still fail.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(Organization, params=dict(orgname='library'), expected_code=204)
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteResponse(User, expected_code=400) # Should still fail.
|
2017-04-25 03:08:28 +00:00
|
|
|
self.deleteEmptyResponse(Organization, params=dict(orgname='titi'), expected_code=204)
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Add some queue items for the user.
|
|
|
|
notification_queue.put([ADMIN_ACCESS_USER, 'somerepo', 'somename'], '{}')
|
|
|
|
dockerfile_build_queue.put([ADMIN_ACCESS_USER, 'anotherrepo'], '{}')
|
|
|
|
|
|
|
|
# Now delete the user.
|
2017-03-21 01:06:37 +00:00
|
|
|
with check_transitive_modifications():
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(User, expected_code=204)
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Ensure the queue items are gone.
|
|
|
|
self.assertIsNone(notification_queue.get())
|
|
|
|
self.assertIsNone(dockerfile_build_queue.get())
|
|
|
|
|
2016-10-21 21:55:22 +00:00
|
|
|
def test_delete_federateduser(self):
|
|
|
|
self.login(PUBLIC_USER)
|
|
|
|
|
|
|
|
# Add some federated logins.
|
|
|
|
user = model.user.get_user(PUBLIC_USER)
|
|
|
|
model.user.attach_federated_login(user, 'github', 'something', {})
|
|
|
|
|
2017-03-21 01:06:37 +00:00
|
|
|
with check_transitive_modifications():
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(User, expected_code=204)
|
2016-10-21 21:55:22 +00:00
|
|
|
|
2016-11-08 23:27:12 +00:00
|
|
|
def test_delete_prompted_user(self):
|
|
|
|
self.login('randomuser')
|
2017-03-21 01:06:37 +00:00
|
|
|
with check_transitive_modifications():
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(User, expected_code=204)
|
2016-11-08 23:27:12 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2015-05-07 22:13:45 +00:00
|
|
|
class TestSignin(ApiTestCase):
|
|
|
|
def test_signin_unicode(self):
|
|
|
|
self.postResponse(Signin, data=dict(username=u'\xe5\x8c\x97\xe4\xba\xac\xe5\xb8\x82',
|
|
|
|
password='password'), expected_code=403)
|
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
def test_signin_invitecode(self):
|
|
|
|
# Create a new user (unverified)
|
|
|
|
data = self.postJsonResponse(User, data=NEW_USER_DETAILS, expected_code=200)
|
|
|
|
self.assertTrue(data['awaiting_verification'])
|
|
|
|
|
|
|
|
# Try to sign in without an invite code.
|
|
|
|
data = self.postJsonResponse(Signin, data=NEW_USER_DETAILS, expected_code=403)
|
|
|
|
self.assertTrue(data['needsEmailVerification'])
|
|
|
|
|
|
|
|
# Try to sign in with an invalid invite code.
|
2017-07-24 15:05:15 +00:00
|
|
|
details = {'invite_code': 'someinvalidcode'}
|
2015-07-16 12:00:51 +00:00
|
|
|
details.update(NEW_USER_DETAILS)
|
|
|
|
|
|
|
|
data = self.postJsonResponse(Signin, data=details, expected_code=403)
|
|
|
|
self.assertTrue(data['needsEmailVerification'])
|
|
|
|
|
|
|
|
# Sign in with an invite code and ensure the user becomes verified.
|
|
|
|
inviter = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
team = model.team.get_organization_team(ORGANIZATION, 'owners')
|
|
|
|
invite = model.team.add_or_invite_to_team(inviter, team, None, NEW_USER_DETAILS['email'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
details = {'invite_code': invite.invite_token}
|
2015-07-16 12:00:51 +00:00
|
|
|
details.update(NEW_USER_DETAILS)
|
|
|
|
|
|
|
|
data = self.postJsonResponse(Signin, data=details, expected_code=200)
|
|
|
|
self.assertFalse('needsEmailVerification' in data)
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
class TestSignout(ApiTestCase):
|
|
|
|
def test_signout(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-12-07 16:39:27 +00:00
|
|
|
read_user = model.user.get_user(READ_ACCESS_USER)
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(User)
|
2014-01-31 22:54:01 +00:00
|
|
|
assert json['username'] == READ_ACCESS_USER
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
self.postResponse(Signout)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2014-03-19 00:32:37 +00:00
|
|
|
# Make sure we're now signed out.
|
|
|
|
self.getJsonResponse(User, expected_code=401)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2017-12-07 16:39:27 +00:00
|
|
|
# Make sure the user's UUID has rotated, to ensure sessions are no longer valid.
|
|
|
|
read_user_again = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
self.assertNotEquals(read_user.uuid, read_user_again.uuid)
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2015-04-07 18:05:12 +00:00
|
|
|
class TestConductSearch(ApiTestCase):
|
|
|
|
def test_noaccess(self):
|
|
|
|
self.login(NO_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='read'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
2015-04-09 16:57:20 +00:00
|
|
|
self.assertEquals(0, len(json['results']))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='owners'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(0, len(json['results']))
|
|
|
|
|
|
|
|
def test_nouser(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='read'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
2015-04-09 16:57:20 +00:00
|
|
|
self.assertEquals(0, len(json['results']))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='public'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(2, len(json['results']))
|
Optimize repository search by changing our lookup strategy
Previous to this change, repositories were looked up unfiltered in six different queries, and then filtered using the permissions model, which issued a query per repository found, making search incredibly slow. Instead, we now lookup a chunk of repositories unfiltered and then filter them via a single query to the database. By layering the filtering on top of the lookup, each as queries, we can minimize the number of queries necessary, without (at the same time) using a super expensive join.
Other changes:
- Remove the 5 page pre-lookup on V1 search and simply return that there is one more page available, until there isn't. While technically not correct, it is much more efficient, and no one should be using pagination with V1 search anyway.
- Remove the lookup for repos without entries in the RAC table. Instead, we now add a new RAC entry when the repository is created for *the day before*, with count 0, so that it is immediately searchable
- Remove lookup of results with a matching namespace; these aren't very relevant anyway, and it overly complicates sorting
2017-02-27 22:56:44 +00:00
|
|
|
self.assertEquals(json['results'][0]['kind'], 'repository')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'publicrepo')
|
2015-04-07 18:05:12 +00:00
|
|
|
|
Optimize repository search by changing our lookup strategy
Previous to this change, repositories were looked up unfiltered in six different queries, and then filtered using the permissions model, which issued a query per repository found, making search incredibly slow. Instead, we now lookup a chunk of repositories unfiltered and then filter them via a single query to the database. By layering the filtering on top of the lookup, each as queries, we can minimize the number of queries necessary, without (at the same time) using a super expensive join.
Other changes:
- Remove the 5 page pre-lookup on V1 search and simply return that there is one more page available, until there isn't. While technically not correct, it is much more efficient, and no one should be using pagination with V1 search anyway.
- Remove the lookup for repos without entries in the RAC table. Instead, we now add a new RAC entry when the repository is created for *the day before*, with count 0, so that it is immediately searchable
- Remove lookup of results with a matching namespace; these aren't very relevant anyway, and it overly complicates sorting
2017-02-27 22:56:44 +00:00
|
|
|
self.assertEquals(json['results'][1]['kind'], 'user')
|
|
|
|
self.assertEquals(json['results'][1]['name'], 'public')
|
2015-04-07 18:05:12 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='owners'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(0, len(json['results']))
|
|
|
|
|
|
|
|
def test_orgmember(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='owners'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(0, len(json['results']))
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='readers'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
self.assertEquals(json['results'][0]['kind'], 'team')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'readers')
|
|
|
|
|
|
|
|
def test_orgadmin(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='owners'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
2017-04-25 03:08:28 +00:00
|
|
|
self.assertEquals(4, len(json['results']))
|
2015-04-07 18:05:12 +00:00
|
|
|
self.assertEquals(json['results'][0]['kind'], 'team')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'owners')
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='readers'))
|
2015-04-07 18:05:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
self.assertEquals(json['results'][0]['kind'], 'team')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'readers')
|
|
|
|
|
2016-03-16 20:08:53 +00:00
|
|
|
def test_explicit_permission(self):
|
|
|
|
self.login('reader')
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='shared'))
|
2016-03-16 20:08:53 +00:00
|
|
|
|
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
self.assertEquals(json['results'][0]['kind'], 'repository')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'shared')
|
|
|
|
|
2017-01-11 20:03:14 +00:00
|
|
|
def test_full_text(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Make sure the repository is found via `full` and `text search`.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='full'))
|
2017-01-11 20:03:14 +00:00
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
self.assertEquals(json['results'][0]['kind'], 'repository')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'text-full-repo')
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ConductSearch, params=dict(query='text search'))
|
2017-01-11 20:03:14 +00:00
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
self.assertEquals(json['results'][0]['kind'], 'repository')
|
|
|
|
self.assertEquals(json['results'][0]['name'], 'text-full-repo')
|
|
|
|
|
|
|
|
|
2016-03-07 15:07:41 +00:00
|
|
|
class TestGetMatchingEntities(ApiTestCase):
|
2016-11-02 20:22:35 +00:00
|
|
|
def test_simple_lookup(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(
|
|
|
|
prefix=ADMIN_ACCESS_USER, namespace=ORGANIZATION, includeTeams='true'))
|
2016-11-02 20:22:35 +00:00
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
|
|
|
|
def test_simple_lookup_noorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(prefix=ADMIN_ACCESS_USER))
|
2016-11-02 20:22:35 +00:00
|
|
|
self.assertEquals(1, len(json['results']))
|
|
|
|
|
2016-10-04 09:35:04 +00:00
|
|
|
def test_unicode_search(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(prefix='北京市', namespace=ORGANIZATION,
|
|
|
|
includeTeams='true'))
|
2016-10-04 09:35:04 +00:00
|
|
|
self.assertEquals(0, len(json['results']))
|
|
|
|
|
2016-03-07 15:07:41 +00:00
|
|
|
def test_notinorg(self):
|
|
|
|
self.login(NO_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(prefix='o', namespace=ORGANIZATION,
|
|
|
|
includeTeams='true'))
|
2016-03-07 15:07:41 +00:00
|
|
|
|
|
|
|
names = set([r['name'] for r in json['results']])
|
|
|
|
assert 'outsideorg' in names
|
|
|
|
assert not 'owners' in names
|
|
|
|
|
2017-09-12 19:55:37 +00:00
|
|
|
def test_prefix_disabled(self):
|
|
|
|
with patch('features.PARTIAL_USER_AUTOCOMPLETE', False):
|
|
|
|
self.login(NO_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(prefix='o', namespace=ORGANIZATION,
|
|
|
|
includeTeams='true'))
|
|
|
|
|
|
|
|
names = set([r['name'] for r in json['results']])
|
|
|
|
assert not 'outsideorg' in names
|
|
|
|
assert not 'owners' in names
|
|
|
|
|
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(prefix='outsideorg', namespace=ORGANIZATION,
|
|
|
|
includeTeams='true'))
|
|
|
|
names = set([r['name'] for r in json['results']])
|
|
|
|
assert 'outsideorg' in names
|
|
|
|
assert not 'owners' in names
|
|
|
|
|
2016-03-07 15:07:41 +00:00
|
|
|
def test_inorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(prefix='o', namespace=ORGANIZATION,
|
|
|
|
includeTeams='true'))
|
2016-03-07 15:07:41 +00:00
|
|
|
|
|
|
|
names = set([r['name'] for r in json['results']])
|
|
|
|
assert 'outsideorg' in names
|
|
|
|
assert 'owners' in names
|
|
|
|
|
|
|
|
def test_inorg_withorgs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(EntitySearch, params=dict(
|
|
|
|
prefix=ORGANIZATION[0], namespace=ORGANIZATION, includeOrgs='true'))
|
2016-03-07 15:07:41 +00:00
|
|
|
|
|
|
|
names = set([r['name'] for r in json['results']])
|
|
|
|
assert ORGANIZATION in names
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
class TestCreateOrganization(ApiTestCase):
|
|
|
|
def test_existinguser(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.postJsonResponse(OrganizationList,
|
2014-03-19 00:32:37 +00:00
|
|
|
data=dict(name=ADMIN_ACCESS_USER, email='testorg@example.com'),
|
2014-01-31 22:54:01 +00:00
|
|
|
expected_code=400)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEquals('A user or organization with this name already exists', json['detail'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_existingorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.postJsonResponse(OrganizationList,
|
2014-03-19 00:32:37 +00:00
|
|
|
data=dict(name=ORGANIZATION, email='testorg@example.com'),
|
2014-01-31 22:54:01 +00:00
|
|
|
expected_code=400)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEquals('A user or organization with this name already exists', json['detail'])
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_createorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
data = self.postResponse(OrganizationList, data=dict(
|
|
|
|
name='neworg', email='testorg@example.com'), expected_code=201)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2017-01-27 16:22:40 +00:00
|
|
|
self.assertEquals('"Created"', data.strip())
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
# Ensure the org was created.
|
2015-07-15 21:25:41 +00:00
|
|
|
organization = model.organization.get_organization('neworg')
|
2014-01-31 22:54:01 +00:00
|
|
|
assert organization is not None
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
# Verify the admin user is the org's admin.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname='neworg'))
|
2014-01-31 23:54:31 +00:00
|
|
|
self.assertEquals('neworg', json['name'])
|
|
|
|
self.assertEquals(True, json['is_admin'])
|
|
|
|
|
2016-05-09 09:16:01 +00:00
|
|
|
def test_createorg_viaoauth(self):
|
|
|
|
# Attempt with no auth.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(OrganizationList, data=dict(name='neworg', email='testorg@example.com'),
|
2016-05-09 09:16:01 +00:00
|
|
|
expected_code=401)
|
|
|
|
|
|
|
|
# Attempt with auth with invalid scope.
|
|
|
|
dt_user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
token = model.oauth.create_access_token_for_testing(dt_user, 'deadbeef', 'repo:read',
|
|
|
|
access_token='foo')
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(OrganizationList, data=dict(name='neworg', email='testorg@example.com'),
|
2016-05-09 09:16:01 +00:00
|
|
|
headers=dict(Authorization='Bearer ' + token.access_token),
|
|
|
|
expected_code=403)
|
|
|
|
|
|
|
|
# Create OAuth token with user:admin scope.
|
|
|
|
token = model.oauth.create_access_token_for_testing(dt_user, 'deadbeef', 'user:admin',
|
|
|
|
access_token='bar')
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
data = self.postResponse(
|
|
|
|
OrganizationList, data=dict(name='neworg', email='testorg@example.com'),
|
|
|
|
headers=dict(Authorization='Bearer ' + token.access_token), expected_code=201)
|
2016-05-09 09:16:01 +00:00
|
|
|
|
2017-01-27 16:22:40 +00:00
|
|
|
self.assertEquals('"Created"', data.strip())
|
2016-05-09 09:16:01 +00:00
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
class TestGetOrganization(ApiTestCase):
|
|
|
|
def test_unknownorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2015-07-15 21:25:41 +00:00
|
|
|
self.getResponse(Organization, params=dict(orgname='notvalid'), expected_code=404)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_cannotaccess(self):
|
|
|
|
self.login(NO_ACCESS_USER)
|
2015-07-15 21:25:41 +00:00
|
|
|
self.getResponse(Organization, params=dict(orgname=ORGANIZATION), expected_code=200)
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
def test_getorganization(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname=ORGANIZATION))
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
self.assertEquals(ORGANIZATION, json['name'])
|
|
|
|
self.assertEquals(False, json['is_admin'])
|
|
|
|
|
|
|
|
def test_getorganization_asadmin(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname=ORGANIZATION))
|
2014-01-31 22:54:01 +00:00
|
|
|
|
|
|
|
self.assertEquals(ORGANIZATION, json['name'])
|
|
|
|
self.assertEquals(True, json['is_admin'])
|
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
class TestChangeOrganizationDetails(ApiTestCase):
|
|
|
|
def test_changeinvoiceemail(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(Organization, params=dict(orgname=ORGANIZATION),
|
2014-01-31 22:54:01 +00:00
|
|
|
data=dict(invoice_email=True))
|
|
|
|
|
|
|
|
self.assertEquals(True, json['invoice_email'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(Organization, params=dict(orgname=ORGANIZATION),
|
2014-01-31 22:54:01 +00:00
|
|
|
data=dict(invoice_email=False))
|
|
|
|
self.assertEquals(False, json['invoice_email'])
|
|
|
|
|
|
|
|
def test_changemail(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(Organization, params=dict(orgname=ORGANIZATION),
|
2014-01-31 22:54:01 +00:00
|
|
|
data=dict(email='newemail@example.com'))
|
|
|
|
|
|
|
|
self.assertEquals('newemail@example.com', json['email'])
|
|
|
|
|
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
class TestGetOrganizationPrototypes(ApiTestCase):
|
|
|
|
def test_getprototypes(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
assert len(json['prototypes']) > 0
|
|
|
|
|
|
|
|
|
|
|
|
class TestCreateOrganizationPrototypes(ApiTestCase):
|
|
|
|
def test_invaliduser(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION),
|
|
|
|
data=dict(activating_user={'name': 'unknownuser'}, role='read',
|
|
|
|
delegate={'kind': 'team',
|
|
|
|
'name': 'owners'}), expected_code=400)
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2016-04-11 18:51:58 +00:00
|
|
|
self.assertEquals('Unknown activating user', json['detail'])
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
def test_missingdelegate(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION),
|
|
|
|
data=dict(role='read'), expected_code=400)
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
def test_createprototype(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION),
|
2014-02-06 19:40:36 +00:00
|
|
|
data=dict(role='read',
|
|
|
|
delegate={'kind': 'team',
|
|
|
|
'name': 'readers'}))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals('read', json['role'])
|
|
|
|
pid = json['id']
|
|
|
|
|
|
|
|
# Verify the prototype exists.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
ids = set([p['id'] for p in json['prototypes']])
|
|
|
|
assert pid in ids
|
|
|
|
|
|
|
|
|
|
|
|
class TestDeleteOrganizationPrototypes(ApiTestCase):
|
|
|
|
def test_deleteprototype(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Get the existing prototypes
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
ids = [p['id'] for p in json['prototypes']]
|
|
|
|
pid = ids[0]
|
|
|
|
|
|
|
|
# Delete a prototype.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(PermissionPrototype,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, prototypeid=pid))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
# Verify the prototype no longer exists.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
newids = [p['id'] for p in json['prototypes']]
|
|
|
|
assert not pid in newids
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateOrganizationPrototypes(ApiTestCase):
|
|
|
|
def test_updateprototype(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Get the existing prototypes
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
ids = [p['id'] for p in json['prototypes']]
|
|
|
|
pid = ids[0]
|
|
|
|
|
|
|
|
# Update a prototype.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.putJsonResponse(PermissionPrototype,
|
2015-07-15 21:25:41 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, prototypeid=pid),
|
|
|
|
data=dict(role='admin'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals('admin', json['role'])
|
|
|
|
|
|
|
|
|
2015-07-02 14:04:12 +00:00
|
|
|
class TestGetOrganizationMembers(ApiTestCase):
|
2014-01-31 23:54:31 +00:00
|
|
|
def test_getmembers(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationMemberList, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2015-07-02 14:04:12 +00:00
|
|
|
membernames = [member['name'] for member in json['members']]
|
|
|
|
assert ADMIN_ACCESS_USER in membernames
|
|
|
|
assert READ_ACCESS_USER in membernames
|
|
|
|
assert not NO_ACCESS_USER in membernames
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2015-08-31 20:27:32 +00:00
|
|
|
for member in json['members']:
|
|
|
|
membername = member['name']
|
|
|
|
response = self.getJsonResponse(OrganizationMember,
|
|
|
|
params=dict(orgname=ORGANIZATION, membername=membername))
|
|
|
|
|
|
|
|
self.assertEquals(member, response)
|
|
|
|
|
2015-07-02 14:04:12 +00:00
|
|
|
|
|
|
|
class TestRemoveOrganizationMember(ApiTestCase):
|
|
|
|
def test_try_remove_only_admin(self):
|
2014-01-31 23:54:31 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2015-07-02 14:04:12 +00:00
|
|
|
self.deleteResponse(OrganizationMember,
|
|
|
|
params=dict(orgname=ORGANIZATION, membername=ADMIN_ACCESS_USER),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
def test_remove_member(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationMemberList, params=dict(orgname=ORGANIZATION))
|
2015-07-02 14:04:12 +00:00
|
|
|
|
|
|
|
membernames = [member['name'] for member in json['members']]
|
|
|
|
assert ADMIN_ACCESS_USER in membernames
|
|
|
|
assert READ_ACCESS_USER in membernames
|
|
|
|
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(OrganizationMember,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, membername=READ_ACCESS_USER))
|
2015-07-02 14:04:12 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationMemberList, params=dict(orgname=ORGANIZATION))
|
2015-07-02 14:04:12 +00:00
|
|
|
|
|
|
|
membernames = [member['name'] for member in json['members']]
|
|
|
|
assert ADMIN_ACCESS_USER in membernames
|
|
|
|
assert not READ_ACCESS_USER in membernames
|
|
|
|
|
|
|
|
def test_remove_member_repo_permission(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Add read user as a direct permission on the admin user's repo.
|
2017-07-24 15:05:15 +00:00
|
|
|
model.permission.set_user_repo_permission(READ_ACCESS_USER, ADMIN_ACCESS_USER, 'simple',
|
|
|
|
'read')
|
2015-07-02 14:04:12 +00:00
|
|
|
|
|
|
|
# Verify the user has a permission on the admin user's repo.
|
2017-07-24 15:05:15 +00:00
|
|
|
admin_perms = [
|
|
|
|
p.user.username for p in model.user.get_all_repo_users(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
]
|
2015-07-02 14:04:12 +00:00
|
|
|
assert READ_ACCESS_USER in admin_perms
|
|
|
|
|
|
|
|
# Add read user as a direct permission on the org repo.
|
2015-07-15 21:25:41 +00:00
|
|
|
model.permission.set_user_repo_permission(READ_ACCESS_USER, ORGANIZATION, ORG_REPO, 'read')
|
2015-07-02 14:04:12 +00:00
|
|
|
|
|
|
|
# Verify the user has a permission on the org repo.
|
2015-07-15 21:25:41 +00:00
|
|
|
org_perms = [p.user.username for p in model.user.get_all_repo_users(ORGANIZATION, ORG_REPO)]
|
2015-07-02 14:04:12 +00:00
|
|
|
assert READ_ACCESS_USER in org_perms
|
|
|
|
|
|
|
|
# Remove the user from the org.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(OrganizationMember,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, membername=READ_ACCESS_USER))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2015-07-02 14:04:12 +00:00
|
|
|
# Verify that the user's permission on the org repo is gone, but it is still
|
|
|
|
# present on the other repo.
|
2015-07-15 21:25:41 +00:00
|
|
|
org_perms = [p.user.username for p in model.user.get_all_repo_users(ORGANIZATION, ORG_REPO)]
|
2015-07-02 14:04:12 +00:00
|
|
|
assert not READ_ACCESS_USER in org_perms
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
admin_perms = [
|
|
|
|
p.user.username for p in model.user.get_all_repo_users(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
]
|
2015-07-02 14:04:12 +00:00
|
|
|
assert READ_ACCESS_USER in admin_perms
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestGetOrganizationPrivateAllowed(ApiTestCase):
|
|
|
|
def test_existingorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrgPrivateRepositories, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, json['privateAllowed'])
|
|
|
|
assert not 'reposAllowed' in json
|
|
|
|
|
|
|
|
def test_neworg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
data = self.postResponse(OrganizationList, data=dict(name='neworg', email='test@example.com'),
|
2014-01-31 23:54:31 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrgPrivateRepositories, params=dict(orgname='neworg'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals(False, json['privateAllowed'])
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateOrganizationTeam(ApiTestCase):
|
|
|
|
def test_updateexisting(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
data = self.putJsonResponse(OrganizationTeam, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'),
|
|
|
|
data=dict(description='My cool team', role='creator'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals('My cool team', data['description'])
|
|
|
|
self.assertEquals('creator', data['role'])
|
|
|
|
|
|
|
|
def test_attemptchangeroleonowners(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(OrganizationTeam, params=dict(orgname=ORGANIZATION, teamname='owners'),
|
|
|
|
data=dict(role='creator'), expected_code=400)
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
def test_createnewteam(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
data = self.putJsonResponse(OrganizationTeam, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='newteam'),
|
|
|
|
data=dict(description='My cool team', role='member'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
self.assertEquals('My cool team', data['description'])
|
|
|
|
self.assertEquals('member', data['role'])
|
|
|
|
|
|
|
|
# Verify the team was created.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
assert 'newteam' in json['teams']
|
|
|
|
|
|
|
|
|
|
|
|
class TestDeleteOrganizationTeam(ApiTestCase):
|
|
|
|
def test_deleteteam(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(OrganizationTeam,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, teamname='readers'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
# Make sure the team was deleted
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Organization, params=dict(orgname=ORGANIZATION))
|
2014-01-31 23:54:31 +00:00
|
|
|
assert not 'readers' in json['teams']
|
|
|
|
|
|
|
|
def test_attemptdeleteowners(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
resp = self.deleteResponse(
|
|
|
|
OrganizationTeam, params=dict(orgname=ORGANIZATION, teamname='owners'), expected_code=400)
|
2016-07-11 19:47:05 +00:00
|
|
|
data = py_json.loads(resp)
|
|
|
|
msg = ("Deleting team 'owners' would remove admin ability for user " +
|
|
|
|
"'devtable' in organization 'buynlarge'")
|
|
|
|
self.assertEquals(msg, data['message'])
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
|
2016-08-22 18:42:35 +00:00
|
|
|
class TestTeamPermissions(ApiTestCase):
|
|
|
|
def test_team_permissions(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
resp = self.getJsonResponse(TeamPermissions,
|
|
|
|
params=dict(orgname=ORGANIZATION, teamname='readers'))
|
|
|
|
|
|
|
|
self.assertEquals(1, len(resp['permissions']))
|
|
|
|
|
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
class TestGetOrganizationTeamMembers(ApiTestCase):
|
|
|
|
def test_invalidteam(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(TeamMemberList, params=dict(orgname=ORGANIZATION, teamname='notvalid'),
|
2014-01-31 23:54:31 +00:00
|
|
|
expected_code=404)
|
|
|
|
|
|
|
|
def test_getmembers(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2018-03-28 18:31:02 +00:00
|
|
|
self.assertInTeam(json, READ_ACCESS_USER)
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateOrganizationTeamMember(ApiTestCase):
|
2014-08-18 21:24:00 +00:00
|
|
|
def test_addmember_alreadyteammember(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
membername = READ_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=membername), expected_code=400)
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
def test_addmember_orgmember(self):
|
2014-01-31 23:54:31 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
membername = READ_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='owners',
|
|
|
|
membername=membername))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
# Verify the user was added to the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
self.assertInTeam(json, membername)
|
|
|
|
|
|
|
|
def test_addmember_robot(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
membername = ORGANIZATION + '+coolrobot'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=membername))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
# Verify the user was added to the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertInTeam(json, membername)
|
|
|
|
|
|
|
|
def test_addmember_invalidrobot(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
membername = 'freshuser+anotherrobot'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=membername), expected_code=400)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
def test_addmember_nonorgmember(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
membername = NO_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(TeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', membername=membername))
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Make sure the user is not (yet) part of the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'))
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
for member in json['members']:
|
|
|
|
self.assertNotEqual(membername, member['name'])
|
|
|
|
|
2017-02-17 17:01:41 +00:00
|
|
|
def test_updatemembers_syncedteam(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
with patch('endpoints.api.team.authentication', AttrDict({'federated_service': 'foobar'})):
|
|
|
|
# Add the user to a non-synced team, which should succeed.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='owners',
|
|
|
|
membername=READ_ACCESS_USER))
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Remove the user from the non-synced team, which should succeed.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='owners',
|
|
|
|
membername=READ_ACCESS_USER))
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Attempt to add the user to a synced team, which should fail.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='synced',
|
|
|
|
membername=READ_ACCESS_USER), expected_code=400)
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Attempt to remove the user from the synced team, which should fail.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='synced',
|
|
|
|
membername=READ_ACCESS_USER), expected_code=400)
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Add a robot to the synced team, which should succeed.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='synced',
|
|
|
|
membername=ORGANIZATION + '+coolrobot'))
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Remove the robot from the non-synced team, which should succeed.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='synced',
|
|
|
|
membername=ORGANIZATION + '+coolrobot'))
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Invite a team member to a non-synced team, which should succeed.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(InviteTeamMember, params=dict(orgname=ORGANIZATION, teamname='owners',
|
|
|
|
email='someguy+new@devtable.com'))
|
2017-02-17 17:01:41 +00:00
|
|
|
|
|
|
|
# Attempt to invite a team member to a synced team, which should fail.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putResponse(InviteTeamMember, params=dict(orgname=ORGANIZATION, teamname='synced',
|
|
|
|
email='someguy+new@devtable.com'),
|
2017-02-17 17:01:41 +00:00
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
class TestAcceptTeamMemberInvite(ApiTestCase):
|
|
|
|
def test_accept(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the invite.
|
|
|
|
membername = NO_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(TeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', membername=membername))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Login as the user.
|
|
|
|
self.login(membername)
|
|
|
|
|
|
|
|
# Accept the invite.
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_user(membername)
|
|
|
|
invites = list(model.team.lookup_team_invites(user))
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
self.putJsonResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token))
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
# Verify the user is now on the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
self.assertInTeam(json, membername)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
# Verify the accept now fails.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token),
|
2014-09-12 18:29:01 +00:00
|
|
|
expected_code=400)
|
2014-08-18 21:24:00 +00:00
|
|
|
|
2016-12-15 22:15:11 +00:00
|
|
|
def test_accept_via_email(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the invite.
|
|
|
|
member = model.user.get_user(NO_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(InviteTeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', email=member.email))
|
2016-12-15 22:15:11 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Login as the user.
|
|
|
|
self.login(member.username)
|
|
|
|
|
|
|
|
# Accept the invite.
|
|
|
|
invites = list(model.team.lookup_team_invites_by_email(member.email))
|
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
|
|
|
self.putJsonResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token))
|
|
|
|
|
|
|
|
# Verify the user is now on the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2016-12-15 22:15:11 +00:00
|
|
|
|
|
|
|
self.assertInTeam(json, member.username)
|
2014-08-18 21:24:00 +00:00
|
|
|
|
2016-12-15 22:15:11 +00:00
|
|
|
# Verify the accept now fails.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token),
|
2016-12-15 22:15:11 +00:00
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
def test_accept_invite_different_user(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the invite.
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(TeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', membername=NO_ACCESS_USER))
|
2016-12-15 22:15:11 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Login as a different user.
|
|
|
|
self.login(PUBLIC_USER)
|
|
|
|
|
|
|
|
# Try to accept the invite.
|
|
|
|
user = model.user.get_user(NO_ACCESS_USER)
|
|
|
|
invites = list(model.team.lookup_team_invites(user))
|
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
# Ensure the invite is still valid.
|
|
|
|
user = model.user.get_user(NO_ACCESS_USER)
|
|
|
|
invites = list(model.team.lookup_team_invites(user))
|
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
|
|
|
# Ensure the user is *not* a member of the team.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2016-12-15 22:15:11 +00:00
|
|
|
self.assertNotInTeam(json, PUBLIC_USER)
|
|
|
|
|
|
|
|
def test_accept_invite_different_email(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the invite.
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(InviteTeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', email='someemail@example.com'))
|
2016-12-15 22:15:11 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Login as a different user.
|
|
|
|
self.login(PUBLIC_USER)
|
|
|
|
|
|
|
|
# Try to accept the invite.
|
|
|
|
invites = list(model.team.lookup_team_invites_by_email('someemail@example.com'))
|
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
|
|
|
self.putResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
# Ensure the invite is still valid.
|
|
|
|
invites = list(model.team.lookup_team_invites_by_email('someemail@example.com'))
|
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
|
|
|
# Ensure the user is *not* a member of the team.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='owners'))
|
2016-12-15 22:15:11 +00:00
|
|
|
self.assertNotInTeam(json, PUBLIC_USER)
|
2014-08-18 21:24:00 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
class TestDeclineTeamMemberInvite(ApiTestCase):
|
|
|
|
def test_decline_wronguser(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the invite.
|
|
|
|
membername = NO_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(TeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', membername=membername))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Try to decline the invite.
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_user(membername)
|
|
|
|
invites = list(model.team.lookup_team_invites(user))
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token),
|
2014-09-12 18:29:01 +00:00
|
|
|
expected_code=400)
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
def test_decline(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the invite.
|
|
|
|
membername = NO_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(TeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='owners', membername=membername))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Login as the user.
|
|
|
|
self.login(membername)
|
|
|
|
|
|
|
|
# Decline the invite.
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_user(membername)
|
|
|
|
invites = list(model.team.lookup_team_invites(user))
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals(1, len(invites))
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token))
|
2014-08-18 21:24:00 +00:00
|
|
|
|
|
|
|
# Make sure the invite was deleted.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteResponse(TeamMemberInvite, params=dict(code=invites[0].invite_token),
|
2014-09-12 18:29:01 +00:00
|
|
|
expected_code=400)
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDeleteOrganizationTeamMember(ApiTestCase):
|
2014-09-08 21:20:01 +00:00
|
|
|
def test_deletememberinvite(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2016-01-12 20:57:03 +00:00
|
|
|
# Verify the initial member count
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='readers', includePending=True))
|
2016-01-12 20:57:03 +00:00
|
|
|
|
|
|
|
self.assertEquals(len(json['members']), 3)
|
|
|
|
|
2014-09-08 21:20:01 +00:00
|
|
|
membername = NO_ACCESS_USER
|
2017-07-24 15:05:15 +00:00
|
|
|
response = self.putJsonResponse(TeamMember, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='readers', membername=membername))
|
2014-09-08 21:20:01 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, response['invited'])
|
|
|
|
|
|
|
|
# Verify the invite was added.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='readers', includePending=True))
|
2014-09-08 21:20:01 +00:00
|
|
|
|
2016-01-12 20:57:03 +00:00
|
|
|
self.assertEquals(len(json['members']), 4)
|
2014-09-08 21:20:01 +00:00
|
|
|
|
|
|
|
# Delete the invite.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=membername))
|
2014-09-08 21:20:01 +00:00
|
|
|
|
|
|
|
# Verify the user was removed from the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(
|
|
|
|
orgname=ORGANIZATION, teamname='readers', includePending=True))
|
2014-09-08 21:20:01 +00:00
|
|
|
|
2016-01-12 20:57:03 +00:00
|
|
|
self.assertEquals(len(json['members']), 3)
|
2014-09-08 21:20:01 +00:00
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
def test_deletemember(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=READ_ACCESS_USER))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
# Verify the user was removed from the team.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(TeamMemberList, params=dict(orgname=ORGANIZATION,
|
|
|
|
teamname='readers'))
|
2014-01-31 23:54:31 +00:00
|
|
|
|
2016-01-12 20:57:03 +00:00
|
|
|
self.assertEquals(len(json['members']), 1)
|
2014-01-31 23:54:31 +00:00
|
|
|
|
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
class TestCreateRepo(ApiTestCase):
|
2015-09-24 15:42:56 +00:00
|
|
|
def test_invalidreponame(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryList, data=dict(
|
|
|
|
repository='some/repo', visibility='public', description=''), expected_code=400)
|
2015-09-24 15:42:56 +00:00
|
|
|
|
2016-04-11 18:51:58 +00:00
|
|
|
self.assertEquals('Invalid repository name', json['detail'])
|
2015-09-24 15:42:56 +00:00
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
def test_duplicaterepo(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryList, data=dict(
|
|
|
|
repository='simple', visibility='public', description=''), expected_code=400)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-04-11 18:51:58 +00:00
|
|
|
self.assertEquals('Repository already exists', json['detail'])
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
def test_createrepo(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryList, data=dict(
|
|
|
|
repository='newrepo', visibility='public', description=''), expected_code=201)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals('newrepo', json['name'])
|
|
|
|
|
2017-03-23 21:16:19 +00:00
|
|
|
def test_create_app_repo(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryList, data=dict(
|
|
|
|
repository='newrepo', visibility='public', description='', repo_kind='application'),
|
2017-03-23 21:16:19 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals('newrepo', json['name'])
|
|
|
|
self.assertEquals('application', json['kind'])
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
def test_createrepo_underorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryList, data=dict(
|
|
|
|
namespace=ORGANIZATION, repository='newrepo', visibility='private', description=''),
|
2014-03-19 00:32:37 +00:00
|
|
|
expected_code=201)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
self.assertEquals(ORGANIZATION, json['namespace'])
|
|
|
|
self.assertEquals('newrepo', json['name'])
|
|
|
|
|
|
|
|
|
|
|
|
class TestListRepos(ApiTestCase):
|
2017-03-23 21:16:19 +00:00
|
|
|
def test_list_app_repos(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create an app repo.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryList, data=dict(repository='newrepo', visibility='public',
|
|
|
|
description='', repo_kind='application'),
|
2017-03-23 21:16:19 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(namespace=ADMIN_ACCESS_USER,
|
|
|
|
public=False, repo_kind='application'))
|
2017-03-23 21:16:19 +00:00
|
|
|
|
|
|
|
self.assertEquals(1, len(json['repositories']))
|
|
|
|
self.assertEquals('application', json['repositories'][0]['kind'])
|
|
|
|
|
2014-02-19 00:15:14 +00:00
|
|
|
def test_listrepos_asguest(self):
|
2015-05-08 02:25:23 +00:00
|
|
|
# Queries: Base + the list query
|
2016-08-15 20:11:45 +00:00
|
|
|
with assert_query_count(BASE_QUERY_COUNT + 1):
|
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(public=True))
|
|
|
|
self.assertEquals(len(json['repositories']), 1)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-08-15 20:11:45 +00:00
|
|
|
def assertPublicRepos(self, has_extras=False):
|
2016-06-30 21:31:35 +00:00
|
|
|
public_user = model.user.get_user('public')
|
|
|
|
|
2016-08-10 19:08:06 +00:00
|
|
|
# Delete all existing repos under the namespace.
|
2017-07-24 15:05:15 +00:00
|
|
|
for repo in list(
|
|
|
|
RepositoryTable.select().where(RepositoryTable.namespace_user == public_user)):
|
2016-08-10 19:08:06 +00:00
|
|
|
model.repository.purge_repository(public_user.username, repo.name)
|
|
|
|
|
|
|
|
# Add public repos until we have enough for a few pages.
|
|
|
|
required = set()
|
2016-08-15 20:11:45 +00:00
|
|
|
for i in range(0, REPOS_PER_PAGE * 3):
|
2016-08-10 19:08:06 +00:00
|
|
|
name = 'publicrepo%s' % i
|
2017-07-24 15:05:15 +00:00
|
|
|
model.repository.create_repository('public', name, public_user, visibility='public')
|
2016-08-10 19:08:06 +00:00
|
|
|
required.add(name)
|
|
|
|
|
|
|
|
# Request results until we no longer have any.
|
|
|
|
next_page = None
|
|
|
|
while True:
|
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(public=True, next_page=next_page))
|
|
|
|
for repo in json['repositories']:
|
|
|
|
name = repo['name']
|
2016-08-15 20:11:45 +00:00
|
|
|
if name in required:
|
|
|
|
required.remove(name)
|
|
|
|
else:
|
|
|
|
self.assertTrue(has_extras, "Could not find name %s in repos created" % name)
|
2016-08-10 19:08:06 +00:00
|
|
|
|
|
|
|
if 'next_page' in json:
|
|
|
|
self.assertEquals(len(json['repositories']), REPOS_PER_PAGE)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
next_page = json['next_page']
|
|
|
|
|
2016-08-15 20:11:45 +00:00
|
|
|
def test_listrepos_asguest_withpages(self):
|
|
|
|
self.assertPublicRepos()
|
2016-06-30 21:31:35 +00:00
|
|
|
|
2016-08-15 20:11:45 +00:00
|
|
|
def test_listrepos_asorgmember_withpages(self):
|
2014-02-01 00:45:44 +00:00
|
|
|
self.login(READ_ACCESS_USER)
|
2016-08-15 20:11:45 +00:00
|
|
|
self.assertPublicRepos(has_extras=True)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2014-02-19 00:15:14 +00:00
|
|
|
def test_listrepos_filter(self):
|
2014-02-01 00:45:44 +00:00
|
|
|
self.login(READ_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(namespace=ORGANIZATION, public=False))
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-06-30 21:31:35 +00:00
|
|
|
self.assertGreater(len(json['repositories']), 0)
|
2015-07-21 21:20:24 +00:00
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
for repo in json['repositories']:
|
|
|
|
self.assertEquals(ORGANIZATION, repo['namespace'])
|
|
|
|
|
2015-07-21 21:20:24 +00:00
|
|
|
def test_listrepos_allparams(self):
|
2016-07-06 20:15:54 +00:00
|
|
|
# Add a repository action count entry for one of the org repos.
|
|
|
|
repo = model.repository.get_repository(ORGANIZATION, ORG_REPO)
|
|
|
|
RepositoryActionCount.create(repository=repo, count=10, date=datetime.datetime.utcnow())
|
|
|
|
|
2015-07-21 21:20:24 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-03-16 20:08:53 +00:00
|
|
|
# Queries: Base + the list query + the popularity and last modified queries + full perms load
|
2018-06-19 14:51:30 +00:00
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 5):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(namespace=ORGANIZATION, public=False,
|
|
|
|
last_modified=True, popularity=True))
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-06-30 21:31:35 +00:00
|
|
|
self.assertGreater(len(json['repositories']), 0)
|
2015-06-09 21:58:57 +00:00
|
|
|
|
2016-07-06 20:15:54 +00:00
|
|
|
for repository in json['repositories']:
|
|
|
|
self.assertEquals(ORGANIZATION, repository['namespace'])
|
|
|
|
if repository['name'] == ORG_REPO:
|
|
|
|
self.assertGreater(repository['popularity'], 0)
|
|
|
|
|
2015-07-28 19:47:04 +00:00
|
|
|
def test_listrepos_starred_nouser(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(RepositoryList, params=dict(last_modified=True, popularity=True,
|
|
|
|
starred=True), expected_code=400)
|
2015-07-28 19:47:04 +00:00
|
|
|
|
2015-07-21 21:20:24 +00:00
|
|
|
def test_listrepos_starred(self):
|
2015-07-15 22:25:19 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2015-07-21 21:20:24 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(last_modified=True, popularity=True,
|
|
|
|
starred=True))
|
2015-07-21 21:20:24 +00:00
|
|
|
|
|
|
|
self.assertTrue(len(json['repositories']) > 0)
|
2015-07-15 22:25:19 +00:00
|
|
|
|
|
|
|
for repo in json['repositories']:
|
2015-07-21 21:20:24 +00:00
|
|
|
self.assertTrue(repo['is_starred'])
|
2015-07-20 18:17:26 +00:00
|
|
|
|
|
|
|
def test_listrepos_asguest_allparams(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(namespace=ORGANIZATION, public=False,
|
|
|
|
last_modified=True))
|
2015-07-20 18:17:26 +00:00
|
|
|
|
|
|
|
for repo in json['repositories']:
|
|
|
|
self.assertEquals(ORGANIZATION, repo['namespace'])
|
2015-07-15 22:25:19 +00:00
|
|
|
|
2016-08-31 17:51:53 +00:00
|
|
|
def assertRepositoryVisible(self, namespace, name):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(namespace=namespace, public=False))
|
2016-08-31 17:51:53 +00:00
|
|
|
self.assertEquals(1, len(json['repositories']))
|
|
|
|
self.assertEquals(name, json['repositories'][0]['name'])
|
|
|
|
|
|
|
|
def assertRepositoryNotVisible(self, namespace, name):
|
2016-08-31 18:07:05 +00:00
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(namespace=namespace, public=False))
|
|
|
|
for repo in json['repositories']:
|
|
|
|
self.assertNotEquals(name, repo['name'])
|
|
|
|
|
|
|
|
json = self.getJsonResponse(RepositoryList, params=dict(starred=True))
|
2016-08-31 17:51:53 +00:00
|
|
|
for repo in json['repositories']:
|
|
|
|
self.assertNotEquals(name, repo['name'])
|
|
|
|
|
2016-08-31 18:07:05 +00:00
|
|
|
def test_listrepos_starred_filtered(self):
|
|
|
|
admin_user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
reader_user = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a new organization.
|
|
|
|
new_org = model.organization.create_organization('neworg', 'neworg@devtable.com', admin_user)
|
|
|
|
admin_team = model.team.create_team('admin', new_org, 'admin')
|
|
|
|
|
|
|
|
# Add a repository to the organization.
|
|
|
|
repo = model.repository.create_repository('neworg', 'somerepo', admin_user)
|
|
|
|
|
|
|
|
with self.add_to_team_temporarily(reader_user, admin_team):
|
|
|
|
# Star the repository for the user.
|
|
|
|
model.repository.star_repository(reader_user, repo)
|
|
|
|
|
|
|
|
# Verify that the user cannot see the repo, since they are no longer allowed to do so.
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
self.assertRepositoryNotVisible('neworg', 'somerepo')
|
|
|
|
|
2016-08-31 17:51:53 +00:00
|
|
|
@contextmanager
|
|
|
|
def add_to_team_temporarily(self, user, team):
|
|
|
|
model.team.add_user_to_team(user, team)
|
|
|
|
yield
|
|
|
|
model.team.remove_user_from_team(team.organization.username, team.name, user.username,
|
|
|
|
ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
def test_listrepos_org_filtered(self):
|
|
|
|
admin_user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
reader_user = model.user.get_user(READ_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a new organization.
|
|
|
|
new_org = model.organization.create_organization('neworg', 'neworg@devtable.com', admin_user)
|
|
|
|
|
|
|
|
admin_team = model.team.create_team('admin', new_org, 'admin')
|
|
|
|
creator_team = model.team.create_team('creators', new_org, 'creator')
|
|
|
|
member_team = model.team.create_team('members', new_org, 'member')
|
|
|
|
|
|
|
|
# Add a repository to the organization.
|
|
|
|
model.repository.create_repository('neworg', 'somerepo', admin_user)
|
|
|
|
|
|
|
|
# Verify that the admin user can view it.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
self.assertRepositoryVisible('neworg', 'somerepo')
|
|
|
|
|
|
|
|
# Add reader to a creator team under the org and verify they *cannot* see the repository.
|
|
|
|
with self.add_to_team_temporarily(reader_user, creator_team):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
self.assertRepositoryNotVisible('neworg', 'somerepo')
|
|
|
|
|
|
|
|
# Add reader to a member team under the org and verify they *cannot* see the repository.
|
|
|
|
with self.add_to_team_temporarily(reader_user, member_team):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
self.assertRepositoryNotVisible('neworg', 'somerepo')
|
|
|
|
|
|
|
|
# Add reader to an admin team under the org and verify they *can* see the repository.
|
|
|
|
with self.add_to_team_temporarily(reader_user, admin_team):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
self.assertRepositoryVisible('neworg', 'somerepo')
|
|
|
|
|
|
|
|
# Verify that the public user cannot see the repository.
|
|
|
|
self.login(PUBLIC_USER)
|
|
|
|
self.assertRepositoryNotVisible('neworg', 'somerepo')
|
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2015-05-19 21:52:44 +00:00
|
|
|
class TestViewPublicRepository(ApiTestCase):
|
|
|
|
def test_normalview(self):
|
2016-06-03 19:00:01 +00:00
|
|
|
resp = self.getJsonResponse(Repository, params=dict(repository='public/publicrepo'))
|
|
|
|
self.assertFalse('stats' in resp)
|
|
|
|
|
|
|
|
def test_normalview_withstats(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
resp = self.getJsonResponse(Repository, params=dict(repository='public/publicrepo',
|
|
|
|
includeStats=True))
|
2016-06-03 19:00:01 +00:00
|
|
|
self.assertTrue('stats' in resp)
|
2015-05-19 21:52:44 +00:00
|
|
|
|
|
|
|
def test_anon_access_disabled(self):
|
|
|
|
import features
|
|
|
|
features.ANONYMOUS_ACCESS = False
|
|
|
|
try:
|
|
|
|
self.getResponse(Repository, params=dict(repository='public/publicrepo'), expected_code=401)
|
|
|
|
finally:
|
|
|
|
features.ANONYMOUS_ACCESS = True
|
|
|
|
|
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
class TestUpdateRepo(ApiTestCase):
|
2014-02-06 19:40:36 +00:00
|
|
|
SIMPLE_REPO = ADMIN_ACCESS_USER + '/simple'
|
2017-07-24 15:05:15 +00:00
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
def test_updatedescription(self):
|
2014-02-01 00:45:44 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(Repository, params=dict(repository=self.SIMPLE_REPO),
|
2014-02-01 00:45:44 +00:00
|
|
|
data=dict(description='Some cool repo'))
|
|
|
|
|
|
|
|
# Verify the repo description was updated.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
self.assertEquals('Some cool repo', json['description'])
|
|
|
|
|
|
|
|
|
|
|
|
class TestChangeRepoVisibility(ApiTestCase):
|
2014-02-06 19:40:36 +00:00
|
|
|
SIMPLE_REPO = ADMIN_ACCESS_USER + '/simple'
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
def test_trychangevisibility(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Make public.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryVisibility, params=dict(repository=self.SIMPLE_REPO),
|
2015-09-15 18:33:35 +00:00
|
|
|
data=dict(visibility='public'))
|
|
|
|
|
|
|
|
# Verify the visibility.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, json['is_public'])
|
|
|
|
|
|
|
|
# Change the subscription of the namespace.
|
2017-12-15 19:28:57 +00:00
|
|
|
self.putJsonResponse(UserPlan, data=dict(plan='personal-2018'))
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
# Try to make private.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryVisibility, params=dict(repository=self.SIMPLE_REPO),
|
|
|
|
data=dict(visibility='private'), expected_code=402)
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
# Verify the visibility.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2015-09-15 18:33:35 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, json['is_public'])
|
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
def test_changevisibility(self):
|
2014-02-01 00:45:44 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Make public.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryVisibility, params=dict(repository=self.SIMPLE_REPO),
|
2014-02-01 00:45:44 +00:00
|
|
|
data=dict(visibility='public'))
|
|
|
|
|
|
|
|
# Verify the visibility.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, json['is_public'])
|
|
|
|
|
|
|
|
# Make private.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryVisibility, params=dict(repository=self.SIMPLE_REPO),
|
2014-02-01 00:45:44 +00:00
|
|
|
data=dict(visibility='private'))
|
|
|
|
|
|
|
|
# Verify the visibility.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2014-02-01 00:45:44 +00:00
|
|
|
|
|
|
|
self.assertEquals(False, json['is_public'])
|
|
|
|
|
|
|
|
|
2015-12-18 19:18:52 +00:00
|
|
|
class log_queries(object):
|
2017-03-21 01:06:37 +00:00
|
|
|
def __init__(self, query_filters=None):
|
|
|
|
self.filters = query_filters
|
2015-12-18 19:18:52 +00:00
|
|
|
|
|
|
|
def get_queries(self):
|
|
|
|
queries = [q.msg[0] for q in self._handler.queries]
|
2017-03-21 01:06:37 +00:00
|
|
|
if not self.filters:
|
|
|
|
return queries
|
2015-12-18 19:18:52 +00:00
|
|
|
|
2017-03-21 01:06:37 +00:00
|
|
|
filtered_queries = []
|
|
|
|
for query_filter in self.filters:
|
|
|
|
filtered_queries.extend([q for q in queries if re.match(query_filter, q)])
|
|
|
|
|
|
|
|
return filtered_queries
|
2015-12-18 19:18:52 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
logger = logging.getLogger('peewee')
|
|
|
|
self._handler = _QueryLogHandler()
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(self._handler)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
logger = logging.getLogger('peewee')
|
|
|
|
logger.removeHandler(self._handler)
|
|
|
|
|
|
|
|
|
2017-03-21 01:06:37 +00:00
|
|
|
class check_transitive_modifications(log_queries):
|
2016-08-05 20:39:01 +00:00
|
|
|
def __init__(self):
|
2017-03-21 01:06:37 +00:00
|
|
|
filters = [r'^DELETE.+IN \(SELECT.+$', r'^UPDATE.+IN \(SELECT.+$']
|
|
|
|
super(check_transitive_modifications, self).__init__(query_filters=filters)
|
2015-12-18 19:18:52 +00:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
2017-03-21 01:06:37 +00:00
|
|
|
super(check_transitive_modifications, self).__exit__(exc_type, exc_val, exc_tb)
|
2015-12-18 19:18:52 +00:00
|
|
|
queries = self.get_queries()
|
2016-08-05 20:39:01 +00:00
|
|
|
if queries:
|
2017-03-21 01:06:37 +00:00
|
|
|
raise Exception('Detected transitive deletion or update in queries: %s' % queries)
|
2015-12-18 19:18:52 +00:00
|
|
|
|
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
class TestDeleteRepository(ApiTestCase):
|
2014-02-06 19:40:36 +00:00
|
|
|
SIMPLE_REPO = ADMIN_ACCESS_USER + '/simple'
|
2014-11-11 04:05:20 +00:00
|
|
|
COMPLEX_REPO = ADMIN_ACCESS_USER + '/complex'
|
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
def test_deleterepo(self):
|
2014-02-01 00:45:44 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2015-09-16 19:34:20 +00:00
|
|
|
# Verify the repo exists.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2015-09-16 19:34:20 +00:00
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
# Add a build queue item for the repo.
|
|
|
|
dockerfile_build_queue.put([ADMIN_ACCESS_USER, 'simple'], '{}')
|
|
|
|
|
|
|
|
# Delete the repository.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
# Ensure the queue item is gone.
|
|
|
|
self.assertIsNone(dockerfile_build_queue.get())
|
|
|
|
|
2014-02-01 00:45:44 +00:00
|
|
|
# Verify the repo was deleted.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.SIMPLE_REPO), expected_code=404)
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
def test_verify_queue_removal(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Verify the repo exists.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Add a build queue item for the repo and another repo.
|
2016-11-30 21:54:04 +00:00
|
|
|
dockerfile_build_queue.put([ADMIN_ACCESS_USER, 'simple'], '{}', available_after=-1)
|
|
|
|
dockerfile_build_queue.put([ADMIN_ACCESS_USER, 'anotherrepo'], '{}', available_after=-1)
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Delete the repository.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(Repository, params=dict(repository=self.SIMPLE_REPO))
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Ensure the other queue item is still present.
|
|
|
|
self.assertIsNotNone(dockerfile_build_queue.get())
|
|
|
|
|
2014-11-11 04:05:20 +00:00
|
|
|
def test_deleterepo2(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2015-09-16 19:34:20 +00:00
|
|
|
# Verify the repo exists.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.COMPLEX_REPO))
|
2015-09-16 19:34:20 +00:00
|
|
|
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(Repository, params=dict(repository=self.COMPLEX_REPO))
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Verify the repo was deleted.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.COMPLEX_REPO), expected_code=404)
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
def test_populate_and_delete_repo(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2015-09-16 19:34:20 +00:00
|
|
|
# Verify the repo exists.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.COMPLEX_REPO))
|
2015-09-16 19:34:20 +00:00
|
|
|
|
|
|
|
# Make sure the repository has some images and tags.
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, 'complex')
|
|
|
|
self.assertTrue(len(list(registry_model.get_legacy_images(repo_ref))) > 0)
|
|
|
|
self.assertTrue(len(list(registry_model.list_repository_tags(repo_ref))) > 0)
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Add some data for the repository, in addition to is already existing images and tags.
|
2015-07-15 21:25:41 +00:00
|
|
|
repository = model.repository.get_repository(ADMIN_ACCESS_USER, 'complex')
|
2014-11-11 04:05:20 +00:00
|
|
|
|
2018-05-23 21:08:00 +00:00
|
|
|
# Add some CNR tags and linked tags.
|
2018-07-08 07:15:43 +00:00
|
|
|
base_tag = appr_model.tag.create_or_update_tag(repository, 'somebasetag', NEW_MODELS)
|
|
|
|
base_tag2 = appr_model.tag.create_or_update_tag(repository, 'somebasetag2', NEW_MODELS)
|
|
|
|
appr_model.tag.create_or_update_tag(repository, 'somelinkedtag', NEW_MODELS, linked_tag=base_tag)
|
|
|
|
appr_model.tag.create_or_update_tag(repository, 'somelinkedtag2', NEW_MODELS, linked_tag=base_tag2)
|
2017-10-18 21:03:27 +00:00
|
|
|
|
2014-11-11 04:05:20 +00:00
|
|
|
# Create some access tokens.
|
2015-07-15 21:25:41 +00:00
|
|
|
access_token = model.token.create_access_token(repository, 'read')
|
|
|
|
model.token.create_access_token(repository, 'write')
|
2014-11-11 04:05:20 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
delegate_token = model.token.create_delegate_token(ADMIN_ACCESS_USER, 'complex', 'sometoken',
|
|
|
|
'read')
|
|
|
|
model.token.create_delegate_token(ADMIN_ACCESS_USER, 'complex', 'sometoken', 'write')
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Create some repository builds.
|
2015-07-15 21:25:41 +00:00
|
|
|
model.build.create_repository_build(repository, access_token, {}, 'someid', 'foobar')
|
|
|
|
model.build.create_repository_build(repository, delegate_token, {}, 'someid2', 'foobar2')
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Create some notifications.
|
2015-10-26 19:13:58 +00:00
|
|
|
model.notification.create_repo_notification(repository, 'repo_push', 'hipchat', {}, {})
|
|
|
|
model.notification.create_repo_notification(repository, 'build_queued', 'slack', {}, {})
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Create some logs.
|
2019-01-08 19:03:28 +00:00
|
|
|
logs_model.log_action('push_repo', ADMIN_ACCESS_USER, repository=repository)
|
|
|
|
logs_model.log_action('push_repo', ADMIN_ACCESS_USER, repository=repository)
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Create some build triggers.
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
model.build.create_build_trigger(repository, 'github', 'sometoken', user)
|
|
|
|
model.build.create_build_trigger(repository, 'github', 'anothertoken', user)
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Create some email authorizations.
|
2015-07-15 21:25:41 +00:00
|
|
|
model.repository.create_email_authorization_for_repo(ADMIN_ACCESS_USER, 'complex', 'a@b.com')
|
|
|
|
model.repository.create_email_authorization_for_repo(ADMIN_ACCESS_USER, 'complex', 'b@c.com')
|
2014-11-11 04:05:20 +00:00
|
|
|
|
2015-09-16 19:34:20 +00:00
|
|
|
# Create some repository action count entries.
|
|
|
|
RepositoryActionCount.create(repository=repository, date=datetime.datetime.now(), count=1)
|
2017-07-24 15:05:15 +00:00
|
|
|
RepositoryActionCount.create(
|
|
|
|
repository=repository, date=datetime.datetime.now() - datetime.timedelta(days=2), count=2)
|
|
|
|
RepositoryActionCount.create(
|
|
|
|
repository=repository, date=datetime.datetime.now() - datetime.timedelta(days=5), count=6)
|
2015-09-16 19:34:20 +00:00
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, 'complex')
|
|
|
|
tag = registry_model.get_repo_tag(repo_ref, 'prod')
|
|
|
|
manifest = registry_model.get_manifest_for_tag(tag)
|
2016-08-26 20:07:49 +00:00
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
# Create some labels.
|
|
|
|
registry_model.create_manifest_label(manifest, 'foo', 'bar', 'manifest')
|
|
|
|
registry_model.create_manifest_label(manifest, 'foo', 'baz', 'manifest')
|
|
|
|
registry_model.create_manifest_label(manifest, 'something', '{}', 'api',
|
|
|
|
media_type_name='application/json')
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
registry_model.create_manifest_label(manifest, 'something', '{"some": "json"}', 'manifest')
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2014-11-11 04:05:20 +00:00
|
|
|
# Delete the repository.
|
2017-03-21 01:06:37 +00:00
|
|
|
with check_transitive_modifications():
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(Repository, params=dict(repository=self.COMPLEX_REPO))
|
2014-11-11 04:05:20 +00:00
|
|
|
|
|
|
|
# Verify the repo was deleted.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=self.COMPLEX_REPO), expected_code=404)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
class TestGetRepository(ApiTestCase):
|
2014-02-06 19:40:36 +00:00
|
|
|
PUBLIC_REPO = PUBLIC_USER + '/publicrepo'
|
2014-03-11 17:38:44 +00:00
|
|
|
|
2017-04-13 20:34:14 +00:00
|
|
|
def test_get_largerepo(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# base + repo + is_starred + tags
|
2018-12-03 19:19:53 +00:00
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 6 + 2):
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getJsonResponse(Repository, params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2017-04-13 20:34:14 +00:00
|
|
|
|
|
|
|
# base + repo + is_starred + tags
|
2018-12-03 19:19:53 +00:00
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 6 + 2):
|
2017-04-13 20:34:14 +00:00
|
|
|
json = self.getJsonResponse(Repository,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/gargantuan'))
|
|
|
|
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals('gargantuan', json['name'])
|
|
|
|
|
|
|
|
self.assertEquals(False, json['is_public'])
|
|
|
|
|
2014-03-11 17:38:44 +00:00
|
|
|
def test_getrepo_badnames(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2018-01-29 18:30:49 +00:00
|
|
|
bad_names = ['logs', 'build', 'tokens', 'foo.bar', 'foo-bar', 'foo_bar']
|
2014-03-11 17:38:44 +00:00
|
|
|
|
|
|
|
# For each bad name, create the repo.
|
|
|
|
for bad_name in bad_names:
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryList, expected_code=201, data=dict(
|
|
|
|
repository=bad_name, visibility='public', description=''))
|
2014-03-11 17:38:44 +00:00
|
|
|
|
|
|
|
# Make sure we can retrieve its information.
|
2014-03-19 19:39:44 +00:00
|
|
|
json = self.getJsonResponse(Repository,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/' + bad_name))
|
2014-03-11 17:38:44 +00:00
|
|
|
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals(bad_name, json['name'])
|
|
|
|
self.assertEquals(True, json['is_public'])
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_getrepo_public_asguest(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.PUBLIC_REPO))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals(PUBLIC_USER, json['namespace'])
|
|
|
|
self.assertEquals('publicrepo', json['name'])
|
|
|
|
|
|
|
|
self.assertEquals(True, json['is_public'])
|
|
|
|
self.assertEquals(False, json['is_organization'])
|
|
|
|
|
|
|
|
self.assertEquals(False, json['can_write'])
|
|
|
|
self.assertEquals(False, json['can_admin'])
|
|
|
|
|
|
|
|
assert 'latest' in json['tags']
|
|
|
|
|
|
|
|
def test_getrepo_public_asowner(self):
|
|
|
|
self.login(PUBLIC_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=self.PUBLIC_REPO))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals(False, json['is_organization'])
|
|
|
|
self.assertEquals(True, json['can_write'])
|
|
|
|
self.assertEquals(True, json['can_admin'])
|
|
|
|
|
|
|
|
def test_getrepo_building(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(Repository,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/building'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals(True, json['can_write'])
|
|
|
|
self.assertEquals(True, json['can_admin'])
|
|
|
|
self.assertEquals(False, json['is_organization'])
|
|
|
|
|
|
|
|
def test_getrepo_org_asnonmember(self):
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(Repository, params=dict(repository=ORGANIZATION + '/' + ORG_REPO),
|
2014-03-19 17:57:36 +00:00
|
|
|
expected_code=401)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
def test_getrepo_org_asreader(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=ORGANIZATION + '/' + ORG_REPO))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals(ORGANIZATION, json['namespace'])
|
|
|
|
self.assertEquals(ORG_REPO, json['name'])
|
|
|
|
|
|
|
|
self.assertEquals(False, json['can_write'])
|
|
|
|
self.assertEquals(False, json['can_admin'])
|
|
|
|
|
|
|
|
self.assertEquals(True, json['is_organization'])
|
|
|
|
|
|
|
|
def test_getrepo_org_asadmin(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(Repository, params=dict(repository=ORGANIZATION + '/' + ORG_REPO))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals(True, json['can_write'])
|
|
|
|
self.assertEquals(True, json['can_admin'])
|
|
|
|
|
|
|
|
self.assertEquals(True, json['is_organization'])
|
|
|
|
|
|
|
|
|
2015-02-13 20:54:01 +00:00
|
|
|
class TestRepositoryBuildResource(ApiTestCase):
|
2016-06-02 15:59:16 +00:00
|
|
|
def test_repo_build_invalid_url(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
self.postJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(archive_url='hppt://quay.io'), expected_code=400)
|
2016-06-02 15:59:16 +00:00
|
|
|
|
2015-02-13 20:54:01 +00:00
|
|
|
def test_cancel_invalidbuild(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteResponse(
|
|
|
|
RepositoryBuildResource,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', build_uuid='invalid'),
|
|
|
|
expected_code=404)
|
2015-02-13 20:54:01 +00:00
|
|
|
|
|
|
|
def test_cancel_waitingbuild(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
|
|
|
json = self.postJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(file_id='foobarbaz'), expected_code=201)
|
2015-02-13 20:54:01 +00:00
|
|
|
|
|
|
|
uuid = json['id']
|
|
|
|
|
|
|
|
# Check for the build.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
self.assertEquals(1, len(json['builds']))
|
|
|
|
self.assertEquals(uuid, json['builds'][0]['id'])
|
|
|
|
|
2015-02-23 18:38:01 +00:00
|
|
|
# Find the build's queue item.
|
|
|
|
build_ref = database.RepositoryBuild.get(uuid=uuid)
|
|
|
|
queue_item = database.QueueItem.get(id=build_ref.queue_id)
|
|
|
|
|
|
|
|
self.assertTrue(queue_item.available)
|
|
|
|
self.assertTrue(queue_item.retries_remaining > 0)
|
|
|
|
|
2015-02-13 20:54:01 +00:00
|
|
|
# Cancel the build.
|
|
|
|
self.deleteResponse(RepositoryBuildResource,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', build_uuid=uuid),
|
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
# Check for the build.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
2016-11-16 18:51:07 +00:00
|
|
|
self.assertEquals(1, len(json['builds']))
|
|
|
|
self.assertEquals('cancelled', json['builds'][0]['phase'])
|
2015-02-13 20:54:01 +00:00
|
|
|
|
2015-02-23 18:38:01 +00:00
|
|
|
# Check for the build's queue item.
|
|
|
|
try:
|
|
|
|
database.QueueItem.get(id=build_ref.queue_id)
|
|
|
|
self.fail('QueueItem still exists for build')
|
|
|
|
except database.QueueItem.DoesNotExist:
|
|
|
|
pass
|
2015-02-13 20:54:01 +00:00
|
|
|
|
|
|
|
def test_attemptcancel_scheduledbuild(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
|
|
|
json = self.postJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(file_id='foobarbaz'), expected_code=201)
|
2015-02-13 20:54:01 +00:00
|
|
|
|
|
|
|
uuid = json['id']
|
|
|
|
|
|
|
|
# Check for the build.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
self.assertEquals(1, len(json['builds']))
|
|
|
|
self.assertEquals(uuid, json['builds'][0]['id'])
|
|
|
|
|
|
|
|
# Set queue item to be picked up.
|
2015-02-23 18:47:21 +00:00
|
|
|
build_ref = database.RepositoryBuild.get(uuid=uuid)
|
|
|
|
qi = database.QueueItem.get(id=build_ref.queue_id)
|
2015-02-13 20:54:01 +00:00
|
|
|
qi.available = False
|
|
|
|
qi.save()
|
|
|
|
|
|
|
|
# Try to cancel the build.
|
|
|
|
self.deleteResponse(RepositoryBuildResource,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', build_uuid=uuid),
|
2016-12-02 20:07:03 +00:00
|
|
|
expected_code=201)
|
2015-02-13 20:54:01 +00:00
|
|
|
|
|
|
|
def test_attemptcancel_workingbuild(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
|
|
|
json = self.postJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(file_id='foobarbaz'), expected_code=201)
|
2015-02-13 20:54:01 +00:00
|
|
|
|
|
|
|
uuid = json['id']
|
|
|
|
|
|
|
|
# Check for the build.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
self.assertEquals(1, len(json['builds']))
|
|
|
|
self.assertEquals(uuid, json['builds'][0]['id'])
|
|
|
|
|
|
|
|
# Set the build to a different phase.
|
|
|
|
rb = database.RepositoryBuild.get(uuid=uuid)
|
|
|
|
rb.phase = database.BUILD_PHASE.BUILDING
|
|
|
|
rb.save()
|
|
|
|
|
|
|
|
# Try to cancel the build.
|
|
|
|
self.deleteResponse(RepositoryBuildResource,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', build_uuid=uuid),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
class TestRepoBuilds(ApiTestCase):
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_getrepo_nobuilds(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-03-22 18:30:13 +00:00
|
|
|
# Queries: Permission + the list query + app check
|
|
|
|
with assert_query_count(3):
|
2015-05-08 01:11:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
assert len(json['builds']) == 0
|
|
|
|
|
|
|
|
def test_getrepobuilds(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-03-22 18:30:13 +00:00
|
|
|
# Queries: Permission + the list query + app check
|
|
|
|
with assert_query_count(3):
|
2015-05-08 01:11:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/building'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
assert len(json['builds']) > 0
|
2014-05-20 22:53:00 +00:00
|
|
|
build = json['builds'][-1]
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
assert 'id' in build
|
|
|
|
assert 'status' in build
|
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
# Check the status endpoint.
|
2017-07-24 15:05:15 +00:00
|
|
|
status_json = self.getJsonResponse(RepositoryBuildStatus, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/building', build_uuid=build['id']))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-11-13 17:51:37 +00:00
|
|
|
self.assertEquals(status_json['id'], build['id'])
|
|
|
|
self.assertEquals(status_json['resource_key'], build['resource_key'])
|
|
|
|
self.assertEquals(status_json['trigger'], build['trigger'])
|
2014-02-26 20:19:07 +00:00
|
|
|
|
2015-08-14 21:22:19 +00:00
|
|
|
|
2014-02-25 23:22:02 +00:00
|
|
|
class TestRequestRepoBuild(ApiTestCase):
|
2015-08-14 21:22:19 +00:00
|
|
|
def test_requestbuild_noidurl(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build without a file ID or URL.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(), expected_code=400)
|
2015-08-14 21:22:19 +00:00
|
|
|
|
|
|
|
def test_requestbuild_invalidurls(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build with and invalid URL.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(archive_url='foobarbaz'), expected_code=400)
|
2015-08-14 21:22:19 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(archive_url='file://foobarbaz'), expected_code=400)
|
2015-08-14 21:22:19 +00:00
|
|
|
|
|
|
|
def test_requestrepobuild_withurl(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Ensure we are not yet building.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
assert len(json['builds']) == 0
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(archive_url='http://quay.io/robots.txt'), expected_code=201)
|
2015-08-14 21:22:19 +00:00
|
|
|
|
|
|
|
# Check for the build.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
assert len(json['builds']) > 0
|
|
|
|
self.assertEquals('http://quay.io/robots.txt', json['builds'][0]['archive_url'])
|
|
|
|
|
|
|
|
def test_requestrepobuild_withfile(self):
|
2014-02-03 23:18:33 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-04-02 01:49:06 +00:00
|
|
|
# Ensure we are not yet building.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
assert len(json['builds']) == 0
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(file_id='foobarbaz'), expected_code=201)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Check for the build.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
2015-08-14 21:22:19 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
assert len(json['builds']) > 0
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-04-02 01:49:06 +00:00
|
|
|
def test_requestrepobuild_with_robot(self):
|
2014-03-27 22:33:13 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-04-02 01:49:06 +00:00
|
|
|
# Ensure we are not yet building.
|
2014-03-27 22:33:13 +00:00
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-27 22:33:13 +00:00
|
|
|
assert len(json['builds']) == 0
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
2014-04-02 01:49:06 +00:00
|
|
|
pull_robot = ADMIN_ACCESS_USER + '+dtrobot'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(file_id='foobarbaz', pull_robot=pull_robot), expected_code=201)
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
# Check for the build.
|
|
|
|
json = self.getJsonResponse(RepositoryBuildList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/building'))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-27 22:33:13 +00:00
|
|
|
assert len(json['builds']) > 0
|
2014-04-02 01:49:06 +00:00
|
|
|
|
|
|
|
def test_requestrepobuild_with_invalid_robot(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
|
|
|
pull_robot = ADMIN_ACCESS_USER + '+invalidrobot'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(file_id='foobarbaz', pull_robot=pull_robot), expected_code=404)
|
2014-04-02 01:49:06 +00:00
|
|
|
|
|
|
|
def test_requestrepobuild_with_unauthorized_robot(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Request a (fake) build.
|
|
|
|
pull_robot = 'freshuser+anotherrobot'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryBuildList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(file_id='foobarbaz', pull_robot=pull_robot), expected_code=403)
|
2014-04-02 01:49:06 +00:00
|
|
|
|
2014-03-27 22:33:13 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
class TestRepositoryEmail(ApiTestCase):
|
|
|
|
def test_emailnotauthorized(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
# Verify the e-mail address is not authorized.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getResponse(RepositoryAuthorizedEmail, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', email='test@example.com'), expected_code=404)
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
def test_emailnotauthorized_butsent(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
# Verify the e-mail address is not authorized.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryAuthorizedEmail, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', email='jschorr+other@devtable.com'))
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(False, json['confirmed'])
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals('simple', json['repository'])
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
def test_emailauthorized(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
# Verify the e-mail address is authorized.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryAuthorizedEmail, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', email='jschorr@devtable.com'))
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, json['confirmed'])
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals('simple', json['repository'])
|
|
|
|
|
|
|
|
def test_send_email_authorization(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
# Send the email.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(RepositoryAuthorizedEmail, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', email='jschorr+foo@devtable.com'))
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
self.assertEquals(False, json['confirmed'])
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
|
|
|
|
self.assertEquals('simple', json['repository'])
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
class TestRepositoryNotifications(ApiTestCase):
|
2016-10-04 07:57:32 +00:00
|
|
|
def test_testnotification(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Add a notification.
|
|
|
|
json = self.postJsonResponse(RepositoryNotificationList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(config={'url': 'http://example.com'}, event='repo_push',
|
2017-07-24 15:05:15 +00:00
|
|
|
method='webhook', eventConfig={}), expected_code=201)
|
2016-10-04 07:57:32 +00:00
|
|
|
uuid = json['uuid']
|
|
|
|
|
|
|
|
self.assertIsNone(notification_queue.get())
|
|
|
|
|
|
|
|
# Issue a test notification.
|
|
|
|
self.postJsonResponse(TestRepositoryNotification,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', uuid=uuid))
|
|
|
|
|
|
|
|
# Ensure the item is in the queue.
|
2017-07-24 15:05:15 +00:00
|
|
|
time.sleep(1) # Makes sure the queue get works on MySQL with its second-level precision.
|
2016-10-04 07:57:32 +00:00
|
|
|
found = notification_queue.get()
|
|
|
|
self.assertIsNotNone(found)
|
|
|
|
self.assertTrue('notification_uuid' in found['body'])
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_webhooks(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
# Add a notification.
|
|
|
|
json = self.postJsonResponse(RepositoryNotificationList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
2015-07-15 21:25:41 +00:00
|
|
|
data=dict(config={'url': 'http://example.com'}, event='repo_push',
|
2017-07-24 15:05:15 +00:00
|
|
|
method='webhook', eventConfig={}), expected_code=201)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
self.assertEquals('repo_push', json['event'])
|
|
|
|
self.assertEquals('webhook', json['method'])
|
|
|
|
self.assertEquals('http://example.com', json['config']['url'])
|
2015-08-17 20:30:15 +00:00
|
|
|
self.assertIsNone(json['title'])
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
wid = json['uuid']
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
# Get the notification.
|
|
|
|
json = self.getJsonResponse(RepositoryNotification,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', uuid=wid))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
self.assertEquals(wid, json['uuid'])
|
|
|
|
self.assertEquals('repo_push', json['event'])
|
|
|
|
self.assertEquals('webhook', json['method'])
|
2015-08-17 20:30:15 +00:00
|
|
|
self.assertIsNone(json['title'])
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
# Verify the notification is listed.
|
|
|
|
json = self.getJsonResponse(RepositoryNotificationList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
ids = [w['uuid'] for w in json['notifications']]
|
2014-02-03 23:18:33 +00:00
|
|
|
assert wid in ids
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
# Delete the notification.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(RepositoryNotification,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', uuid=wid),
|
|
|
|
expected_code=204)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
# Verify the notification is gone.
|
|
|
|
self.getResponse(RepositoryNotification,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', uuid=wid),
|
2014-02-03 23:18:33 +00:00
|
|
|
expected_code=404)
|
|
|
|
|
2015-08-17 20:30:15 +00:00
|
|
|
# Add another notification.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(
|
|
|
|
RepositoryNotificationList, params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
|
|
|
data=dict(config={'url': 'http://example.com'}, event='repo_push', method='webhook',
|
|
|
|
title='Some Notification', eventConfig={}), expected_code=201)
|
2015-08-17 20:30:15 +00:00
|
|
|
|
|
|
|
self.assertEquals('repo_push', json['event'])
|
|
|
|
self.assertEquals('webhook', json['method'])
|
|
|
|
self.assertEquals('http://example.com', json['config']['url'])
|
|
|
|
self.assertEquals('Some Notification', json['title'])
|
|
|
|
|
|
|
|
wid = json['uuid']
|
|
|
|
|
|
|
|
# Get the notification.
|
|
|
|
json = self.getJsonResponse(RepositoryNotification,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', uuid=wid))
|
|
|
|
|
|
|
|
self.assertEquals(wid, json['uuid'])
|
|
|
|
self.assertEquals('repo_push', json['event'])
|
|
|
|
self.assertEquals('webhook', json['method'])
|
|
|
|
self.assertEquals('Some Notification', json['title'])
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
class TestListAndGetImage(ApiTestCase):
|
|
|
|
def test_listandgetimages(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryImageList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
assert len(json['images']) > 0
|
2014-09-18 21:16:10 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
for image in json['images']:
|
|
|
|
assert 'id' in image
|
|
|
|
assert 'tags' in image
|
|
|
|
assert 'created' in image
|
|
|
|
assert 'comment' in image
|
|
|
|
assert 'command' in image
|
|
|
|
assert 'ancestors' in image
|
|
|
|
assert 'size' in image
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
ijson = self.getJsonResponse(RepositoryImage, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', image_id=image['id']))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
self.assertEquals(image['id'], ijson['id'])
|
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
class TestGetImageChanges(ApiTestCase):
|
|
|
|
def test_getimagechanges(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Find an image to check.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryImageList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
|
|
|
|
|
|
|
image_id = json['images'][0]['id']
|
|
|
|
|
|
|
|
# Lookup the image's changes.
|
|
|
|
# TODO: Fix me once we can get fake changes into the test data
|
2014-03-18 23:34:26 +00:00
|
|
|
#self.getJsonResponse(RepositoryImageChanges,
|
2014-02-03 23:18:33 +00:00
|
|
|
# params=dict(repository=ADMIN_ACCESS_USER + '/simple',
|
|
|
|
# image_id=image_id))
|
|
|
|
|
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
class TestRestoreTag(ApiTestCase):
|
|
|
|
def test_restoretag_invalidtag(self):
|
2015-04-19 19:43:16 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
self.postResponse(RestoreTag,
|
2015-04-19 19:43:16 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/history', tag='invalidtag'),
|
2018-08-22 22:03:26 +00:00
|
|
|
data=dict(image='invalid_image'), expected_code=404)
|
2015-04-19 19:43:16 +00:00
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
def test_restoretag_invalidimage(self):
|
2015-04-19 19:43:16 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
self.postResponse(RestoreTag,
|
2015-04-19 19:43:16 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/history', tag='latest'),
|
2018-08-22 22:03:26 +00:00
|
|
|
data=dict(image='invalid_image'), expected_code=404)
|
2015-04-19 19:43:16 +00:00
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
def test_restoretag_invalidmanifest(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
self.postResponse(RestoreTag,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/history', tag='latest'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(manifest_digest='invalid_digest'), expected_code=400)
|
2017-03-03 22:23:23 +00:00
|
|
|
|
|
|
|
def test_restoretag(self):
|
2015-04-19 19:43:16 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/history', tag='latest'))
|
2015-04-19 19:43:16 +00:00
|
|
|
|
|
|
|
self.assertEquals(2, len(json['tags']))
|
|
|
|
self.assertFalse('end_ts' in json['tags'][0])
|
|
|
|
|
|
|
|
previous_image_id = json['tags'][1]['docker_image_id']
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RestoreTag, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/history', tag='latest'), data=dict(image=previous_image_id))
|
2015-04-19 19:43:16 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/history', tag='latest'))
|
2015-04-19 19:43:16 +00:00
|
|
|
self.assertEquals(3, len(json['tags']))
|
|
|
|
self.assertFalse('end_ts' in json['tags'][0])
|
|
|
|
self.assertEquals(previous_image_id, json['tags'][0]['docker_image_id'])
|
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
def test_restoretag_to_digest(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/history', tag='latest'))
|
2017-03-03 22:23:23 +00:00
|
|
|
|
|
|
|
self.assertEquals(2, len(json['tags']))
|
|
|
|
self.assertFalse('end_ts' in json['tags'][0])
|
|
|
|
|
|
|
|
previous_manifest = json['tags'][1]['manifest_digest']
|
|
|
|
|
|
|
|
self.postJsonResponse(RestoreTag, params=dict(repository=ADMIN_ACCESS_USER + '/history',
|
2017-07-24 15:05:15 +00:00
|
|
|
tag='latest'),
|
2017-03-03 22:23:23 +00:00
|
|
|
data=dict(image='foo', manifest_digest=previous_manifest))
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/history', tag='latest'))
|
2017-03-03 22:23:23 +00:00
|
|
|
self.assertEquals(3, len(json['tags']))
|
|
|
|
self.assertFalse('end_ts' in json['tags'][0])
|
|
|
|
self.assertEquals(previous_manifest, json['tags'][0]['manifest_digest'])
|
|
|
|
|
2015-04-19 19:43:16 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
class TestListAndDeleteTag(ApiTestCase):
|
2016-08-11 22:21:01 +00:00
|
|
|
def test_invalid_tags(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# List the images for staging.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/complex', tag='staging'))
|
2016-08-11 22:21:01 +00:00
|
|
|
|
|
|
|
staging_images = json['images']
|
|
|
|
|
|
|
|
# Try to add some invalid tags.
|
|
|
|
self.putResponse(RepositoryTag,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='-fail'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(image=staging_images[0]['id']), expected_code=400)
|
2016-08-11 22:21:01 +00:00
|
|
|
|
|
|
|
self.putResponse(RepositoryTag,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='北京'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(image=staging_images[0]['id']), expected_code=400)
|
2016-08-11 22:21:01 +00:00
|
|
|
|
2014-02-28 05:12:09 +00:00
|
|
|
def test_listdeletecreateandmovetag(self):
|
2014-02-03 23:18:33 +00:00
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# List the images for prod.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='prod'))
|
|
|
|
|
|
|
|
prod_images = json['images']
|
|
|
|
assert len(prod_images) > 0
|
|
|
|
|
|
|
|
# List the images for staging.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/complex', tag='staging'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
staging_images = json['images']
|
2018-04-02 18:16:43 +00:00
|
|
|
assert len(prod_images) == len(staging_images) + 2
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Delete prod.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(RepositoryTag,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='prod'),
|
|
|
|
expected_code=204)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Make sure the tag is gone.
|
2014-03-18 23:34:26 +00:00
|
|
|
self.getResponse(RepositoryTagImages,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='prod'),
|
|
|
|
expected_code=404)
|
|
|
|
|
|
|
|
# Make the sure the staging images are still there.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/complex', tag='staging'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
self.assertEquals(staging_images, json['images'])
|
|
|
|
|
2015-10-05 20:36:33 +00:00
|
|
|
# Require a valid tag name.
|
|
|
|
self.putResponse(RepositoryTag,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='-fail'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(image=staging_images[0]['id']), expected_code=400)
|
2015-10-05 20:36:33 +00:00
|
|
|
|
2014-02-28 05:12:09 +00:00
|
|
|
# Add a new tag to the staging image.
|
2014-03-26 23:42:29 +00:00
|
|
|
self.putResponse(RepositoryTag,
|
2014-02-28 05:12:09 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='sometag'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(image=staging_images[0]['id']), expected_code=201)
|
2014-02-28 05:12:09 +00:00
|
|
|
|
|
|
|
# Make sure the tag is present.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/complex', tag='sometag'))
|
2014-02-28 05:12:09 +00:00
|
|
|
|
|
|
|
sometag_images = json['images']
|
|
|
|
self.assertEquals(sometag_images, staging_images)
|
|
|
|
|
|
|
|
# Move the tag.
|
2014-03-26 23:42:29 +00:00
|
|
|
self.putResponse(RepositoryTag,
|
2014-02-28 05:12:09 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='sometag'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(image=staging_images[-1]['id']), expected_code=201)
|
2014-02-28 05:12:09 +00:00
|
|
|
|
|
|
|
# Make sure the tag has moved.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/complex', tag='sometag'))
|
2014-02-28 05:12:09 +00:00
|
|
|
|
|
|
|
sometag_new_images = json['images']
|
|
|
|
self.assertEquals(1, len(sometag_new_images))
|
|
|
|
self.assertEquals(staging_images[-1], sometag_new_images[0])
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_deletesubtag(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# List the images for prod.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='prod'))
|
|
|
|
|
|
|
|
prod_images = json['images']
|
|
|
|
assert len(prod_images) > 0
|
|
|
|
|
|
|
|
# Delete staging.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(RepositoryTag,
|
2017-07-24 15:05:15 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='staging'),
|
|
|
|
expected_code=204)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Make sure the prod images are still around.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTagImages,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/complex', tag='prod'))
|
|
|
|
|
|
|
|
self.assertEquals(prod_images, json['images'])
|
|
|
|
|
2017-03-01 23:22:40 +00:00
|
|
|
def test_listtag_digest(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', page=1, limit=1))
|
2017-03-01 23:22:40 +00:00
|
|
|
self.assertTrue('manifest_digest' in json['tags'][0])
|
|
|
|
|
2015-09-09 19:29:50 +00:00
|
|
|
def test_listtagpagination(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, "simple")
|
|
|
|
latest_tag = registry_model.get_repo_tag(repo_ref, 'latest', include_legacy_image=True)
|
2016-08-02 18:17:33 +00:00
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
# Create 8 tags in the simple repo.
|
|
|
|
remaining_tags = {'latest', 'prod'}
|
|
|
|
for i in xrange(1, 9):
|
2016-08-02 18:17:33 +00:00
|
|
|
tag_name = "tag" + str(i)
|
|
|
|
remaining_tags.add(tag_name)
|
2018-11-12 21:27:49 +00:00
|
|
|
assert registry_model.retarget_tag(repo_ref, tag_name, latest_tag.legacy_image, storage)
|
2016-08-02 18:17:33 +00:00
|
|
|
|
|
|
|
# Make sure we can iterate over all of them.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', page=1, limit=5))
|
2016-08-02 18:17:33 +00:00
|
|
|
self.assertEquals(1, json['page'])
|
|
|
|
self.assertEquals(5, len(json['tags']))
|
|
|
|
self.assertTrue(json['has_additional'])
|
|
|
|
|
|
|
|
names = {tag['name'] for tag in json['tags']}
|
|
|
|
remaining_tags = remaining_tags - names
|
|
|
|
self.assertEquals(5, len(remaining_tags))
|
2015-09-09 19:29:50 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', page=2, limit=5))
|
2016-08-02 18:17:33 +00:00
|
|
|
|
|
|
|
self.assertEquals(2, json['page'])
|
|
|
|
self.assertEquals(5, len(json['tags']))
|
|
|
|
self.assertFalse(json['has_additional'])
|
|
|
|
|
|
|
|
names = {tag['name'] for tag in json['tags']}
|
|
|
|
remaining_tags = remaining_tags - names
|
|
|
|
self.assertEquals(0, len(remaining_tags))
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(ListRepositoryTags, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', page=3, limit=5))
|
2016-08-02 18:17:33 +00:00
|
|
|
|
|
|
|
self.assertEquals(3, json['page'])
|
|
|
|
self.assertEquals(0, len(json['tags']))
|
|
|
|
self.assertFalse(json['has_additional'])
|
2015-09-09 19:29:50 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
class TestRepoPermissions(ApiTestCase):
|
2014-11-20 16:33:42 +00:00
|
|
|
def listUserPermissions(self, namespace=ADMIN_ACCESS_USER, repo='simple'):
|
2014-03-18 23:34:26 +00:00
|
|
|
return self.getJsonResponse(RepositoryUserPermissionList,
|
2014-11-20 16:33:42 +00:00
|
|
|
params=dict(repository=namespace + '/' + repo))['permissions']
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
def listTeamPermissions(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
response = self.getJsonResponse(RepositoryTeamPermissionList,
|
|
|
|
params=dict(repository=ORGANIZATION + '/' + ORG_REPO))
|
|
|
|
return response['permissions']
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2014-11-20 16:33:42 +00:00
|
|
|
def test_userpermissions_underorg(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
permissions = self.listUserPermissions(namespace=ORGANIZATION, repo=ORG_REPO)
|
|
|
|
|
|
|
|
self.assertEquals(1, len(permissions))
|
|
|
|
assert 'outsideorg' in permissions
|
|
|
|
self.assertEquals('read', permissions['outsideorg']['role'])
|
|
|
|
self.assertEquals(False, permissions['outsideorg']['is_org_member'])
|
|
|
|
|
|
|
|
# Add another user.
|
|
|
|
self.putJsonResponse(RepositoryUserPermission,
|
2015-07-15 21:25:41 +00:00
|
|
|
params=dict(repository=ORGANIZATION + '/' + ORG_REPO,
|
2017-07-24 15:05:15 +00:00
|
|
|
username=ADMIN_ACCESS_USER), data=dict(role='admin'))
|
2014-11-20 16:33:42 +00:00
|
|
|
|
|
|
|
# Verify the user is present.
|
|
|
|
permissions = self.listUserPermissions(namespace=ORGANIZATION, repo=ORG_REPO)
|
|
|
|
|
|
|
|
self.assertEquals(2, len(permissions))
|
|
|
|
assert ADMIN_ACCESS_USER in permissions
|
|
|
|
self.assertEquals('admin', permissions[ADMIN_ACCESS_USER]['role'])
|
|
|
|
self.assertEquals(True, permissions[ADMIN_ACCESS_USER]['is_org_member'])
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_userpermissions(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# The repo should start with just the admin as a user perm.
|
|
|
|
permissions = self.listUserPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(1, len(permissions))
|
|
|
|
assert ADMIN_ACCESS_USER in permissions
|
|
|
|
self.assertEquals('admin', permissions[ADMIN_ACCESS_USER]['role'])
|
2014-11-20 16:33:42 +00:00
|
|
|
self.assertFalse('is_org_member' in permissions[ADMIN_ACCESS_USER])
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Add another user.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(RepositoryUserPermission, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', username=NO_ACCESS_USER), data=dict(role='read'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify the user is present.
|
|
|
|
permissions = self.listUserPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(2, len(permissions))
|
|
|
|
assert NO_ACCESS_USER in permissions
|
|
|
|
self.assertEquals('read', permissions[NO_ACCESS_USER]['role'])
|
2014-11-20 16:33:42 +00:00
|
|
|
self.assertFalse('is_org_member' in permissions[NO_ACCESS_USER])
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryUserPermission, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', username=NO_ACCESS_USER))
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals('read', json['role'])
|
|
|
|
|
|
|
|
# Change the user's permissions.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(RepositoryUserPermission, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', username=NO_ACCESS_USER), data=dict(role='admin'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify.
|
|
|
|
permissions = self.listUserPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(2, len(permissions))
|
|
|
|
assert NO_ACCESS_USER in permissions
|
|
|
|
self.assertEquals('admin', permissions[NO_ACCESS_USER]['role'])
|
|
|
|
|
|
|
|
# Delete the user's permission.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(RepositoryUserPermission, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', username=NO_ACCESS_USER))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify.
|
|
|
|
permissions = self.listUserPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(1, len(permissions))
|
|
|
|
assert not NO_ACCESS_USER in permissions
|
|
|
|
|
|
|
|
def test_teampermissions(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# The repo should start with just the readers as a team perm.
|
|
|
|
permissions = self.listTeamPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(1, len(permissions))
|
|
|
|
assert 'readers' in permissions
|
|
|
|
self.assertEquals('read', permissions['readers']['role'])
|
|
|
|
|
|
|
|
# Add another team.
|
2014-03-18 23:34:26 +00:00
|
|
|
self.putJsonResponse(RepositoryTeamPermission,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ORGANIZATION + '/' + ORG_REPO, teamname='owners'),
|
|
|
|
data=dict(role='write'))
|
|
|
|
|
|
|
|
# Verify the team is present.
|
|
|
|
permissions = self.listTeamPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(2, len(permissions))
|
|
|
|
assert 'owners' in permissions
|
|
|
|
self.assertEquals('write', permissions['owners']['role'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryTeamPermission, params=dict(
|
|
|
|
repository=ORGANIZATION + '/' + ORG_REPO, teamname='owners'))
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals('write', json['role'])
|
|
|
|
|
|
|
|
# Change the team's permissions.
|
2014-03-18 23:34:26 +00:00
|
|
|
self.putJsonResponse(RepositoryTeamPermission,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ORGANIZATION + '/' + ORG_REPO, teamname='owners'),
|
|
|
|
data=dict(role='admin'))
|
|
|
|
|
|
|
|
# Verify.
|
|
|
|
permissions = self.listTeamPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(2, len(permissions))
|
|
|
|
assert 'owners' in permissions
|
|
|
|
self.assertEquals('admin', permissions['owners']['role'])
|
|
|
|
|
|
|
|
# Delete the team's permission.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(
|
|
|
|
RepositoryTeamPermission,
|
|
|
|
params=dict(repository=ORGANIZATION + '/' + ORG_REPO, teamname='owners'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify.
|
|
|
|
permissions = self.listTeamPermissions()
|
|
|
|
|
|
|
|
self.assertEquals(1, len(permissions))
|
|
|
|
assert not 'owners' in permissions
|
|
|
|
|
2014-02-06 19:40:36 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
class TestApiTokens(ApiTestCase):
|
|
|
|
def listTokens(self):
|
2014-03-18 23:34:26 +00:00
|
|
|
return self.getJsonResponse(RepositoryTokenList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))['tokens']
|
|
|
|
|
|
|
|
def test_tokens(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a new token.
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.postJsonResponse(RepositoryTokenList,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(role='read', friendlyName='mytoken'), expected_code=201)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
self.assertEquals('mytoken', json['friendlyName'])
|
|
|
|
self.assertEquals('read', json['role'])
|
|
|
|
token_code = json['code']
|
|
|
|
|
|
|
|
# Verify.
|
|
|
|
tokens = self.listTokens()
|
|
|
|
assert token_code in tokens
|
|
|
|
self.assertEquals('mytoken', tokens[token_code]['friendlyName'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryToken, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', code=token_code))
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals(tokens[token_code], json)
|
|
|
|
|
|
|
|
# Change the token's permission.
|
2014-03-18 23:34:26 +00:00
|
|
|
self.putJsonResponse(RepositoryToken,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', code=token_code),
|
|
|
|
data=dict(role='write'))
|
|
|
|
|
|
|
|
# Verify.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryToken, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', code=token_code))
|
2014-02-03 23:18:33 +00:00
|
|
|
self.assertEquals('write', json['role'])
|
|
|
|
|
|
|
|
# Delete the token.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(RepositoryToken, params=dict(repository=ADMIN_ACCESS_USER + '/simple',
|
|
|
|
code=token_code))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify.
|
2014-03-18 23:34:26 +00:00
|
|
|
self.getResponse(RepositoryToken,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple', code=token_code),
|
|
|
|
expected_code=404)
|
|
|
|
|
|
|
|
|
|
|
|
class TestUserCard(ApiTestCase):
|
|
|
|
def test_getusercard(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(UserCard)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
self.assertEquals('4242', json['card']['last4'])
|
|
|
|
self.assertEquals('Visa', json['card']['type'])
|
|
|
|
|
|
|
|
def test_setusercard_error(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.postJsonResponse(UserCard, data=dict(token='sometoken'), expected_code=402)
|
2014-02-03 23:18:33 +00:00
|
|
|
assert 'carderror' in json
|
|
|
|
|
|
|
|
|
|
|
|
class TestOrgCard(ApiTestCase):
|
|
|
|
def test_getorgcard(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationCard, params=dict(orgname=ORGANIZATION))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
self.assertEquals('4242', json['card']['last4'])
|
|
|
|
self.assertEquals('Visa', json['card']['type'])
|
|
|
|
|
|
|
|
|
|
|
|
class TestUserSubscription(ApiTestCase):
|
|
|
|
def getSubscription(self):
|
2014-03-18 23:34:26 +00:00
|
|
|
return self.getJsonResponse(UserPlan)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
def test_updateplan(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Change the plan.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(UserPlan, data=dict(plan='free'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify
|
|
|
|
sub = self.getSubscription()
|
|
|
|
self.assertEquals('free', sub['plan'])
|
|
|
|
|
|
|
|
# Change the plan.
|
2017-12-15 19:28:57 +00:00
|
|
|
self.putJsonResponse(UserPlan, data=dict(plan='bus-large-2018'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify
|
|
|
|
sub = self.getSubscription()
|
2017-12-15 19:28:57 +00:00
|
|
|
self.assertEquals('bus-large-2018', sub['plan'])
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestOrgSubscription(ApiTestCase):
|
|
|
|
def getSubscription(self):
|
2014-03-18 23:34:26 +00:00
|
|
|
return self.getJsonResponse(OrganizationPlan, params=dict(orgname=ORGANIZATION))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
def test_updateplan(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Change the plan.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(OrganizationPlan, params=dict(orgname=ORGANIZATION),
|
2014-02-03 23:18:33 +00:00
|
|
|
data=dict(plan='free'))
|
|
|
|
|
|
|
|
# Verify
|
|
|
|
sub = self.getSubscription()
|
|
|
|
self.assertEquals('free', sub['plan'])
|
|
|
|
|
|
|
|
# Change the plan.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(OrganizationPlan, params=dict(orgname=ORGANIZATION),
|
2017-12-15 19:28:57 +00:00
|
|
|
data=dict(plan='bus-large-2018'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify
|
|
|
|
sub = self.getSubscription()
|
2017-12-15 19:28:57 +00:00
|
|
|
self.assertEquals('bus-large-2018', sub['plan'])
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestUserRobots(ApiTestCase):
|
|
|
|
def getRobotNames(self):
|
2014-11-24 21:07:38 +00:00
|
|
|
return [r['name'] for r in self.getJsonResponse(UserRobotList)['robots']]
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
def test_robot_list(self):
|
|
|
|
self.login(NO_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create some robots.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(UserRobot, params=dict(robot_shortname='bender'), expected_code=201)
|
2015-05-08 20:43:07 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(UserRobot, params=dict(robot_shortname='goldy'), expected_code=201)
|
2015-05-08 20:43:07 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(UserRobot, params=dict(robot_shortname='coolbot'), expected_code=201)
|
2015-05-08 20:43:07 +00:00
|
|
|
|
2016-03-16 20:08:53 +00:00
|
|
|
# Queries: Base + the lookup query
|
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 1):
|
2015-05-08 20:43:07 +00:00
|
|
|
self.getJsonResponse(UserRobotList)
|
|
|
|
|
2016-03-16 20:08:53 +00:00
|
|
|
# Queries: Base + the lookup query
|
|
|
|
with assert_query_count(BASE_LOGGEDIN_QUERY_COUNT + 1):
|
2015-05-08 20:43:07 +00:00
|
|
|
self.getJsonResponse(UserRobotList, params=dict(permissions=True))
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_robots(self):
|
|
|
|
self.login(NO_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(UserRobot, params=dict(robot_shortname='bender'),
|
2014-02-03 23:18:33 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
self.assertEquals(NO_ACCESS_USER + '+bender', json['name'])
|
|
|
|
|
|
|
|
# Verify.
|
|
|
|
robots = self.getRobotNames()
|
|
|
|
assert NO_ACCESS_USER + '+bender' in robots
|
|
|
|
|
|
|
|
# Delete the robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(UserRobot, params=dict(robot_shortname='bender'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify.
|
|
|
|
robots = self.getRobotNames()
|
|
|
|
assert not NO_ACCESS_USER + '+bender' in robots
|
|
|
|
|
2014-08-25 21:19:23 +00:00
|
|
|
def test_regenerate(self):
|
|
|
|
self.login(NO_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(UserRobot, params=dict(robot_shortname='bender'),
|
2014-08-25 21:19:23 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
token = json['token']
|
|
|
|
|
|
|
|
# Regenerate the robot.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.postJsonResponse(RegenerateUserRobot, params=dict(robot_shortname='bender'),
|
|
|
|
expected_code=200)
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
# Verify the token changed.
|
|
|
|
self.assertNotEquals(token, json['token'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json2 = self.getJsonResponse(UserRobot, params=dict(robot_shortname='bender'),
|
2014-08-25 21:19:23 +00:00
|
|
|
expected_code=200)
|
|
|
|
|
|
|
|
self.assertEquals(json['token'], json2['token'])
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
class TestOrgRobots(ApiTestCase):
|
2017-08-10 00:33:14 +00:00
|
|
|
def getRobotNames(self, include_permissions=False):
|
|
|
|
params = dict(orgname=ORGANIZATION, permissions=include_permissions)
|
2017-07-24 15:05:15 +00:00
|
|
|
return [
|
|
|
|
r['name']
|
2017-08-10 00:33:14 +00:00
|
|
|
for r in self.getJsonResponse(OrgRobotList, params=params)['robots']
|
2017-07-24 15:05:15 +00:00
|
|
|
]
|
2014-02-03 23:18:33 +00:00
|
|
|
|
2015-09-08 14:10:00 +00:00
|
|
|
def test_create_robot_with_underscores(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='mr_bender'),
|
2015-09-08 14:10:00 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
# Add the robot to a team.
|
|
|
|
membername = ORGANIZATION + '+mr_bender'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=membername))
|
2015-09-08 14:10:00 +00:00
|
|
|
|
|
|
|
# Retrieve the robot's details.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.getJsonResponse(OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='mr_bender'),
|
2015-09-08 14:10:00 +00:00
|
|
|
expected_code=200)
|
|
|
|
|
2017-08-10 00:33:14 +00:00
|
|
|
# Make sure the robot shows up in the org robots list.
|
|
|
|
self.assertTrue(membername in self.getRobotNames(include_permissions=True))
|
|
|
|
|
2014-11-07 17:05:21 +00:00
|
|
|
def test_delete_robot_after_use(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create the robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='bender'),
|
2015-07-15 21:25:41 +00:00
|
|
|
expected_code=201)
|
2014-11-07 17:05:21 +00:00
|
|
|
|
|
|
|
# Add the robot to a team.
|
|
|
|
membername = ORGANIZATION + '+bender'
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(TeamMember, params=dict(orgname=ORGANIZATION, teamname='readers',
|
|
|
|
membername=membername))
|
2014-11-07 17:05:21 +00:00
|
|
|
|
|
|
|
# Add a repository permission.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.putJsonResponse(
|
|
|
|
RepositoryUserPermission,
|
|
|
|
params=dict(repository=ORGANIZATION + '/' + ORG_REPO, username=membername),
|
|
|
|
data=dict(role='read'))
|
2014-11-07 17:05:21 +00:00
|
|
|
|
|
|
|
# Add a permission prototype with the robot as the activating user.
|
2015-07-15 21:25:41 +00:00
|
|
|
self.postJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(role='read', activating_user={'name': membername},
|
2015-07-15 21:25:41 +00:00
|
|
|
delegate={'kind': 'user',
|
|
|
|
'name': membername}))
|
2014-11-07 17:05:21 +00:00
|
|
|
|
|
|
|
# Add a permission prototype with the robot as the delegating user.
|
2015-07-15 21:25:41 +00:00
|
|
|
self.postJsonResponse(PermissionPrototypeList, params=dict(orgname=ORGANIZATION),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(role='read', delegate={'kind': 'user',
|
|
|
|
'name': membername}))
|
2014-11-07 17:05:21 +00:00
|
|
|
|
|
|
|
# Add a build trigger with the robot as the pull robot.
|
|
|
|
database.BuildTriggerService.create(name='fakeservice')
|
|
|
|
|
|
|
|
# Add a new fake trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(ORGANIZATION, ORG_REPO)
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
pull_robot = model.user.get_user(membername)
|
|
|
|
trigger = model.build.create_build_trigger(repo, 'fakeservice', 'sometoken', user,
|
|
|
|
pull_robot=pull_robot)
|
2015-06-30 19:35:28 +00:00
|
|
|
|
|
|
|
# Add a fake build of the fake build trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
token = model.token.create_access_token(repo, 'write', kind='build-worker',
|
|
|
|
friendly_name='Repository Build Token')
|
2015-06-30 19:35:28 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
build = model.build.create_repository_build(repo, token, {}, 'fake-dockerfile', 'fake-name',
|
|
|
|
trigger, pull_robot_name=membername)
|
2014-11-07 17:05:21 +00:00
|
|
|
|
2015-01-14 17:56:06 +00:00
|
|
|
# Add some log entries for the robot.
|
2019-01-08 19:03:28 +00:00
|
|
|
logs_model.log_action('pull_repo', ORGANIZATION, performer=pull_robot, repository=repo)
|
2015-01-14 17:56:06 +00:00
|
|
|
|
2014-11-07 17:05:21 +00:00
|
|
|
# Delete the robot and verify it works.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='bender'))
|
2014-11-07 17:05:21 +00:00
|
|
|
|
2015-06-30 19:35:28 +00:00
|
|
|
# Verify the build is still present.
|
2015-07-15 21:25:41 +00:00
|
|
|
self.assertIsNotNone(model.build.get_repository_build(build.uuid))
|
2015-06-30 19:35:28 +00:00
|
|
|
|
2014-11-07 17:05:21 +00:00
|
|
|
# All the above records should now be deleted, along with the robot. We verify a few of the
|
|
|
|
# critical ones below.
|
|
|
|
|
|
|
|
# Check the team.
|
2015-07-15 21:25:41 +00:00
|
|
|
team = model.team.get_organization_team(ORGANIZATION, 'readers')
|
2017-07-24 15:05:15 +00:00
|
|
|
members = [
|
|
|
|
member.username for member in model.organization.get_organization_team_members(team.id)
|
|
|
|
]
|
2014-11-24 21:07:38 +00:00
|
|
|
self.assertFalse(membername in members)
|
2014-11-07 17:05:21 +00:00
|
|
|
|
|
|
|
# Check the robot itself.
|
2015-07-15 21:25:41 +00:00
|
|
|
self.assertIsNone(model.user.get_user(membername))
|
2014-11-07 17:05:21 +00:00
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_robots(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(
|
|
|
|
OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='bender'), expected_code=201)
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
self.assertEquals(ORGANIZATION + '+bender', json['name'])
|
|
|
|
|
|
|
|
# Verify.
|
|
|
|
robots = self.getRobotNames()
|
|
|
|
assert ORGANIZATION + '+bender' in robots
|
|
|
|
|
|
|
|
# Delete the robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='bender'))
|
2014-02-03 23:18:33 +00:00
|
|
|
|
|
|
|
# Verify.
|
|
|
|
robots = self.getRobotNames()
|
|
|
|
assert not ORGANIZATION + '+bender' in robots
|
|
|
|
|
2014-08-25 21:19:23 +00:00
|
|
|
def test_regenerate(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a robot.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.putJsonResponse(
|
|
|
|
OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='bender'), expected_code=201)
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
token = json['token']
|
|
|
|
|
|
|
|
# Regenerate the robot.
|
|
|
|
json = self.postJsonResponse(RegenerateOrgRobot,
|
2015-07-15 21:25:41 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, robot_shortname='bender'),
|
|
|
|
expected_code=200)
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
# Verify the token changed.
|
|
|
|
self.assertNotEquals(token, json['token'])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json2 = self.getJsonResponse(
|
|
|
|
OrgRobot, params=dict(orgname=ORGANIZATION, robot_shortname='bender'), expected_code=200)
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
self.assertEquals(json['token'], json2['token'])
|
|
|
|
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
class TestLogs(ApiTestCase):
|
2017-02-09 00:52:17 +00:00
|
|
|
def test_repo_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(RepositoryLogs, params=dict(repository='devtable/simple'))
|
|
|
|
assert 'logs' in json
|
|
|
|
assert 'start_time' in json
|
|
|
|
assert 'end_time' in json
|
|
|
|
|
|
|
|
def test_repo_logs_crossyear(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryLogs, params=dict(
|
|
|
|
repository='devtable/simple', starttime='12/01/2016', endtime='1/09/2017'))
|
2017-02-09 00:52:17 +00:00
|
|
|
self.assertEquals('Thu, 01 Dec 2016 00:00:00 -0000', json['start_time'])
|
|
|
|
self.assertEquals('Tue, 10 Jan 2017 00:00:00 -0000', json['end_time'])
|
|
|
|
|
|
|
|
def test_repo_aggregate_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(RepositoryAggregateLogs, params=dict(repository='devtable/simple'))
|
|
|
|
assert 'aggregated' in json
|
|
|
|
assert len(json['aggregated']) > 0
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_user_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(UserLogs)
|
2014-02-03 23:18:33 +00:00
|
|
|
assert 'logs' in json
|
|
|
|
assert 'start_time' in json
|
|
|
|
assert 'end_time' in json
|
|
|
|
|
|
|
|
def test_org_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(OrgLogs, params=dict(orgname=ORGANIZATION))
|
2014-02-03 23:18:33 +00:00
|
|
|
assert 'logs' in json
|
|
|
|
assert 'start_time' in json
|
|
|
|
assert 'end_time' in json
|
|
|
|
|
2015-08-05 21:36:17 +00:00
|
|
|
def test_user_aggregate_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(UserAggregateLogs)
|
|
|
|
assert 'aggregated' in json
|
|
|
|
|
|
|
|
def test_org_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(OrgAggregateLogs, params=dict(orgname=ORGANIZATION))
|
|
|
|
assert 'aggregated' in json
|
|
|
|
|
2014-02-03 23:18:33 +00:00
|
|
|
def test_performer(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(OrgLogs, params=dict(orgname=ORGANIZATION))
|
2014-02-03 23:18:33 +00:00
|
|
|
all_logs = json['logs']
|
|
|
|
|
2014-03-18 23:34:26 +00:00
|
|
|
json = self.getJsonResponse(OrgLogs,
|
2014-02-03 23:18:33 +00:00
|
|
|
params=dict(performer=READ_ACCESS_USER, orgname=ORGANIZATION))
|
|
|
|
|
|
|
|
assert len(json['logs']) < len(all_logs)
|
|
|
|
for log in json['logs']:
|
|
|
|
self.assertEquals(READ_ACCESS_USER, log['performer']['name'])
|
2014-02-01 00:45:44 +00:00
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
|
2014-03-20 19:46:13 +00:00
|
|
|
class TestApplicationInformation(ApiTestCase):
|
|
|
|
def test_get_info(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(ApplicationInformation,
|
|
|
|
params=dict(client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-03-20 19:46:13 +00:00
|
|
|
assert 'name' in json
|
|
|
|
assert 'uri' in json
|
|
|
|
assert 'organization' in json
|
|
|
|
|
|
|
|
def test_get_invalid_info(self):
|
|
|
|
self.getJsonResponse(ApplicationInformation, params=dict(client_id='invalid-code'),
|
|
|
|
expected_code=404)
|
|
|
|
|
|
|
|
|
|
|
|
class TestOrganizationApplications(ApiTestCase):
|
|
|
|
def test_list_create_applications(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(OrganizationApplications, params=dict(orgname=ORGANIZATION))
|
|
|
|
|
|
|
|
self.assertEquals(2, len(json['applications']))
|
2014-11-13 17:51:37 +00:00
|
|
|
|
|
|
|
found = False
|
|
|
|
for application in json['applications']:
|
|
|
|
if application['client_id'] == FAKE_APPLICATION_CLIENT_ID:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
|
|
|
|
self.assertTrue(found)
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
# Add a new application.
|
|
|
|
json = self.postJsonResponse(OrganizationApplications, params=dict(orgname=ORGANIZATION),
|
|
|
|
data=dict(name="Some cool app", description="foo"))
|
|
|
|
|
|
|
|
self.assertEquals("Some cool app", json['name'])
|
|
|
|
self.assertEquals("foo", json['description'])
|
|
|
|
|
|
|
|
# Retrieve the apps list again
|
|
|
|
list_json = self.getJsonResponse(OrganizationApplications, params=dict(orgname=ORGANIZATION))
|
|
|
|
self.assertEquals(3, len(list_json['applications']))
|
|
|
|
|
|
|
|
|
|
|
|
class TestOrganizationApplicationResource(ApiTestCase):
|
|
|
|
def test_get_edit_delete_application(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Retrieve the application.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationApplicationResource, params=dict(
|
|
|
|
orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
self.assertEquals(FAKE_APPLICATION_CLIENT_ID, json['client_id'])
|
|
|
|
|
|
|
|
# Edit the application.
|
2017-07-24 15:05:15 +00:00
|
|
|
edit_json = self.putJsonResponse(OrganizationApplicationResource, params=dict(
|
|
|
|
orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID), data=dict(
|
|
|
|
name="Some App", description="foo", application_uri="bar", redirect_uri="baz",
|
|
|
|
avatar_email="meh"))
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
self.assertEquals(FAKE_APPLICATION_CLIENT_ID, edit_json['client_id'])
|
|
|
|
self.assertEquals("Some App", edit_json['name'])
|
|
|
|
self.assertEquals("foo", edit_json['description'])
|
|
|
|
self.assertEquals("bar", edit_json['application_uri'])
|
|
|
|
self.assertEquals("baz", edit_json['redirect_uri'])
|
2014-11-25 00:25:13 +00:00
|
|
|
self.assertEquals("meh", edit_json['avatar_email'])
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
# Retrieve the application again.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationApplicationResource, params=dict(
|
|
|
|
orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-20 19:46:13 +00:00
|
|
|
self.assertEquals(json, edit_json)
|
|
|
|
|
|
|
|
# Delete the application.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(
|
|
|
|
OrganizationApplicationResource,
|
|
|
|
params=dict(orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
# Make sure the application is gone.
|
|
|
|
self.getJsonResponse(OrganizationApplicationResource,
|
2015-07-15 21:25:41 +00:00
|
|
|
params=dict(orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID),
|
|
|
|
expected_code=404)
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
|
2016-02-16 16:52:57 +00:00
|
|
|
class TestOrganization(ApiTestCase):
|
|
|
|
def test_change_send_billing_invoice(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
self.putJsonResponse(Organization, params=dict(orgname=ORGANIZATION),
|
|
|
|
data=dict(invoice_email=False, invoice_email_address=None))
|
|
|
|
|
|
|
|
|
2014-03-20 19:46:13 +00:00
|
|
|
class TestOrganizationApplicationResetClientSecret(ApiTestCase):
|
|
|
|
def test_reset_client_secret(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Retrieve the application.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationApplicationResource, params=dict(
|
|
|
|
orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
self.assertEquals(FAKE_APPLICATION_CLIENT_ID, json['client_id'])
|
|
|
|
|
|
|
|
# Reset the client secret.
|
2017-07-24 15:05:15 +00:00
|
|
|
reset_json = self.postJsonResponse(OrganizationApplicationResetClientSecret, params=dict(
|
|
|
|
orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-03-20 19:46:13 +00:00
|
|
|
|
|
|
|
self.assertEquals(FAKE_APPLICATION_CLIENT_ID, reset_json['client_id'])
|
|
|
|
self.assertNotEquals(reset_json['client_secret'], json['client_secret'])
|
|
|
|
|
|
|
|
# Verify it was changed in the DB.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(OrganizationApplicationResource, params=dict(
|
|
|
|
orgname=ORGANIZATION, client_id=FAKE_APPLICATION_CLIENT_ID))
|
2014-03-20 19:46:13 +00:00
|
|
|
self.assertEquals(reset_json['client_secret'], json['client_secret'])
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-20 19:46:13 +00:00
|
|
|
|
2015-04-29 21:30:24 +00:00
|
|
|
class FakeBuildTrigger(BuildTriggerHandler):
|
2014-02-26 20:19:07 +00:00
|
|
|
@classmethod
|
|
|
|
def service_name(cls):
|
|
|
|
return 'fakeservice'
|
|
|
|
|
2016-09-27 14:52:34 +00:00
|
|
|
def list_build_source_namespaces(self):
|
|
|
|
return [
|
2017-07-24 15:05:15 +00:00
|
|
|
{
|
|
|
|
'name': 'first',
|
|
|
|
'id': 'first'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'second',
|
|
|
|
'id': 'second'
|
|
|
|
},
|
2016-09-27 14:52:34 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def list_build_sources_for_namespace(self, namespace):
|
|
|
|
if namespace == "first":
|
2017-07-24 15:05:15 +00:00
|
|
|
return [{'name': 'source',}]
|
2016-09-27 14:52:34 +00:00
|
|
|
elif namespace == "second":
|
2017-07-24 15:05:15 +00:00
|
|
|
return [{'name': self.auth_token,}]
|
2016-09-27 14:52:34 +00:00
|
|
|
else:
|
|
|
|
return []
|
2015-04-29 21:30:24 +00:00
|
|
|
|
|
|
|
def list_build_subdirs(self):
|
|
|
|
return [self.auth_token, 'foo', 'bar', self.config['somevalue']]
|
|
|
|
|
|
|
|
def handle_trigger_request(self, request):
|
|
|
|
prepared = PreparedBuild(self.trigger)
|
|
|
|
prepared.build_name = 'build-name'
|
|
|
|
prepared.tags = ['bar']
|
|
|
|
prepared.dockerfile_id = 'foo'
|
|
|
|
prepared.subdirectory = 'subdir'
|
|
|
|
prepared.metadata = {'foo': 'bar'}
|
|
|
|
prepared.is_manual = False
|
|
|
|
return prepared
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return 'active' in self.config and self.config['active']
|
|
|
|
|
|
|
|
def activate(self, standard_webhook_url):
|
|
|
|
self.config['active'] = True
|
|
|
|
return self.config, {}
|
|
|
|
|
|
|
|
def deactivate(self):
|
|
|
|
self.config['active'] = False
|
|
|
|
return self.config
|
|
|
|
|
|
|
|
def manual_start(self, run_parameters=None):
|
|
|
|
prepared = PreparedBuild(self.trigger)
|
|
|
|
prepared.build_name = 'build-name'
|
|
|
|
prepared.tags = ['bar']
|
|
|
|
prepared.dockerfile_id = 'foo'
|
|
|
|
prepared.subdirectory = 'subdir'
|
|
|
|
prepared.metadata = {'foo': 'bar'}
|
|
|
|
prepared.is_manual = True
|
2017-03-21 21:24:11 +00:00
|
|
|
prepared.context = '/'
|
2015-04-29 21:30:24 +00:00
|
|
|
return prepared
|
|
|
|
|
2015-05-03 17:38:11 +00:00
|
|
|
def get_repository_url(self):
|
|
|
|
return 'http://foo/' + self.config['build_source']
|
2014-04-03 03:33:58 +00:00
|
|
|
|
2015-04-29 21:30:24 +00:00
|
|
|
def load_dockerfile_contents(self):
|
|
|
|
if not 'dockerfile' in self.config:
|
2014-04-03 03:33:58 +00:00
|
|
|
return None
|
|
|
|
|
2015-04-29 21:30:24 +00:00
|
|
|
return self.config['dockerfile']
|
2014-04-03 03:33:58 +00:00
|
|
|
|
2015-08-21 19:07:26 +00:00
|
|
|
def list_field_values(self, field_name, limit=None):
|
2014-09-30 20:29:32 +00:00
|
|
|
if field_name == 'test_field':
|
|
|
|
return [1, 2, 3]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
class TestBuildTriggers(ApiTestCase):
|
|
|
|
def test_list_build_triggers(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Check a repo with no known triggers.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(BuildTriggerList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2014-02-26 20:19:07 +00:00
|
|
|
self.assertEquals(0, len(json['triggers']))
|
|
|
|
|
|
|
|
# Check a repo with one known trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(BuildTriggerList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/building'))
|
2014-02-26 20:19:07 +00:00
|
|
|
self.assertEquals(1, len(json['triggers']))
|
|
|
|
|
|
|
|
trigger = json['triggers'][0]
|
|
|
|
|
|
|
|
assert 'id' in trigger
|
|
|
|
assert 'is_active' in trigger
|
|
|
|
assert 'config' in trigger
|
|
|
|
assert 'service' in trigger
|
|
|
|
|
|
|
|
# Verify the get trigger method.
|
2017-07-24 15:05:15 +00:00
|
|
|
trigger_json = self.getJsonResponse(BuildTrigger, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/building', trigger_uuid=trigger['id']))
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
self.assertEquals(trigger, trigger_json)
|
|
|
|
|
|
|
|
# Check the recent builds for the trigger.
|
2017-07-24 15:05:15 +00:00
|
|
|
builds_json = self.getJsonResponse(TriggerBuildList, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/building', trigger_uuid=trigger['id']))
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
assert 'builds' in builds_json
|
|
|
|
|
|
|
|
def test_delete_build_trigger(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(BuildTriggerList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/building'))
|
2014-02-26 20:19:07 +00:00
|
|
|
self.assertEquals(1, len(json['triggers']))
|
|
|
|
trigger = json['triggers'][0]
|
|
|
|
|
|
|
|
# Delete the trigger.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(BuildTrigger, params=dict(repository=ADMIN_ACCESS_USER + '/building',
|
|
|
|
trigger_uuid=trigger['id']))
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
# Verify it was deleted.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(BuildTriggerList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/building'))
|
2014-02-26 20:19:07 +00:00
|
|
|
self.assertEquals(0, len(json['triggers']))
|
|
|
|
|
2014-04-03 03:33:58 +00:00
|
|
|
def test_analyze_fake_trigger(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
database.BuildTriggerService.create(name='fakeservice')
|
|
|
|
|
|
|
|
# Add a new fake trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
trigger = model.build.create_build_trigger(repo, 'fakeservice', 'sometoken', user)
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
# Analyze the trigger's dockerfile: First, no dockerfile.
|
|
|
|
trigger_config = {}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
2016-09-27 14:52:34 +00:00
|
|
|
self.assertEquals('warning', analyze_json['status'])
|
|
|
|
self.assertEquals('Specified Dockerfile path for the trigger was not ' +
|
|
|
|
'found on the main branch. This trigger may fail.', analyze_json['message'])
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
# Analyze the trigger's dockerfile: Second, missing FROM in dockerfile.
|
|
|
|
trigger_config = {'dockerfile': 'MAINTAINER me'}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
|
|
|
self.assertEquals('warning', analyze_json['status'])
|
|
|
|
self.assertEquals('No FROM line found in the Dockerfile', analyze_json['message'])
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-04-03 03:33:58 +00:00
|
|
|
# Analyze the trigger's dockerfile: Third, dockerfile with public repo.
|
|
|
|
trigger_config = {'dockerfile': 'FROM somerepo'}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
|
|
|
self.assertEquals('publicbase', analyze_json['status'])
|
|
|
|
|
|
|
|
# Analyze the trigger's dockerfile: Fourth, dockerfile with private repo with an invalid path.
|
|
|
|
trigger_config = {'dockerfile': 'FROM localhost:5000/somepath'}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
|
|
|
self.assertEquals('warning', analyze_json['status'])
|
2015-07-15 21:25:41 +00:00
|
|
|
self.assertEquals('"localhost:5000/somepath" is not a valid Quay repository path',
|
|
|
|
analyze_json['message'])
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
# Analyze the trigger's dockerfile: Fifth, dockerfile with private repo that does not exist.
|
|
|
|
trigger_config = {'dockerfile': 'FROM localhost:5000/nothere/randomrepo'}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
|
|
|
self.assertEquals('error', analyze_json['status'])
|
2015-07-15 21:25:41 +00:00
|
|
|
nofound = 'Repository "localhost:5000/%s/randomrepo" referenced by the Dockerfile was not found'
|
|
|
|
self.assertEquals(nofound % 'nothere', analyze_json['message'])
|
2014-04-03 03:33:58 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
# Analyze the trigger's dockerfile: Sixth, dockerfile with private repo that the user cannot see
|
2014-04-03 03:33:58 +00:00
|
|
|
trigger_config = {'dockerfile': 'FROM localhost:5000/randomuser/randomrepo'}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
|
|
|
self.assertEquals('error', analyze_json['status'])
|
2015-07-15 21:25:41 +00:00
|
|
|
self.assertEquals(nofound % 'randomuser', analyze_json['message'])
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
# Analyze the trigger's dockerfile: Seventh, dockerfile with private repo that the user see.
|
|
|
|
trigger_config = {'dockerfile': 'FROM localhost:5000/devtable/complex'}
|
2017-07-24 15:05:15 +00:00
|
|
|
analyze_json = self.postJsonResponse(BuildTriggerAnalyze, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-04-03 03:33:58 +00:00
|
|
|
data={'config': trigger_config})
|
|
|
|
|
2016-09-27 14:52:34 +00:00
|
|
|
self.assertEquals('requiresrobot', analyze_json['status'])
|
2014-04-03 03:33:58 +00:00
|
|
|
self.assertEquals('devtable', analyze_json['namespace'])
|
|
|
|
self.assertEquals('complex', analyze_json['name'])
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER + '+dtrobot', analyze_json['robots'][0]['name'])
|
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
def test_fake_trigger(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
database.BuildTriggerService.create(name='fakeservice')
|
|
|
|
|
|
|
|
# Add a new fake trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
trigger = model.build.create_build_trigger(repo, 'fakeservice', 'sometoken', user)
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
# Verify the trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(BuildTriggerList,
|
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
|
2014-02-26 20:19:07 +00:00
|
|
|
self.assertEquals(1, len(json['triggers']))
|
|
|
|
self.assertEquals(trigger.uuid, json['triggers'][0]['id'])
|
|
|
|
self.assertEquals(trigger.service.name, json['triggers'][0]['service'])
|
|
|
|
self.assertEquals(False, json['triggers'][0]['is_active'])
|
|
|
|
|
2016-09-27 14:52:34 +00:00
|
|
|
# List the trigger's source namespaces.
|
2017-07-24 15:05:15 +00:00
|
|
|
namespace_json = self.getJsonResponse(BuildTriggerSourceNamespaces, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid))
|
|
|
|
self.assertEquals([{
|
|
|
|
'id': 'first',
|
|
|
|
'name': 'first'
|
|
|
|
}, {
|
|
|
|
'id': 'second',
|
|
|
|
'name': 'second'
|
|
|
|
}], namespace_json['namespaces'])
|
|
|
|
|
|
|
|
source_json = self.postJsonResponse(BuildTriggerSources, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
|
|
|
data=dict(namespace='first'))
|
2016-09-27 14:52:34 +00:00
|
|
|
self.assertEquals([{'name': 'source'}], source_json['sources'])
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
# List the trigger's subdirs.
|
2017-07-24 15:05:15 +00:00
|
|
|
subdir_json = self.postJsonResponse(BuildTriggerSubdirs, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-02-26 20:19:07 +00:00
|
|
|
data={'somevalue': 'meh'})
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertEquals({
|
|
|
|
'status': 'success',
|
|
|
|
'dockerfile_paths': ['/sometoken', '/foo', '/bar', '/meh'],
|
|
|
|
'contextMap': {
|
|
|
|
'/bar': ['/'],
|
|
|
|
'/foo': ['/'],
|
|
|
|
'/meh': ['/'],
|
|
|
|
'/sometoken': ['/']
|
|
|
|
}
|
|
|
|
}, subdir_json)
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
# Activate the trigger.
|
2017-07-24 15:05:15 +00:00
|
|
|
trigger_config = {'build_source': 'somesource'}
|
|
|
|
activate_json = self.postJsonResponse(BuildTriggerActivate, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2014-03-27 22:33:13 +00:00
|
|
|
data={'config': trigger_config})
|
2014-02-26 20:19:07 +00:00
|
|
|
|
|
|
|
self.assertEquals(True, activate_json['is_active'])
|
|
|
|
|
|
|
|
# Make sure the trigger has a write token.
|
2015-07-15 21:25:41 +00:00
|
|
|
trigger = model.build.get_build_trigger(trigger.uuid)
|
2014-02-26 20:19:07 +00:00
|
|
|
self.assertNotEquals(None, trigger.write_token)
|
|
|
|
self.assertEquals(True, py_json.loads(trigger.config)['active'])
|
|
|
|
|
|
|
|
# Make sure we cannot activate again.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(BuildTriggerActivate, params=dict(repository=ADMIN_ACCESS_USER + '/simple',
|
|
|
|
trigger_uuid=trigger.uuid),
|
|
|
|
data={'config': trigger_config}, expected_code=400)
|
2014-02-26 20:19:07 +00:00
|
|
|
|
2014-09-30 20:29:32 +00:00
|
|
|
# Retrieve values for a field.
|
2014-10-14 20:23:01 +00:00
|
|
|
result = self.postJsonResponse(BuildTriggerFieldValues,
|
2014-11-24 21:07:38 +00:00
|
|
|
params=dict(repository=ADMIN_ACCESS_USER + '/simple',
|
2015-07-15 21:25:41 +00:00
|
|
|
trigger_uuid=trigger.uuid, field_name="test_field"))
|
2014-09-30 20:29:32 +00:00
|
|
|
|
|
|
|
self.assertEquals(result['values'], [1, 2, 3])
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(BuildTriggerFieldValues, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid,
|
|
|
|
field_name="another_field"), expected_code=404)
|
2014-09-30 20:29:32 +00:00
|
|
|
|
2014-02-26 20:19:07 +00:00
|
|
|
# Start a manual build.
|
2017-07-24 15:05:15 +00:00
|
|
|
start_json = self.postJsonResponse(ActivateBuildTrigger, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid), data=dict(),
|
2014-02-26 20:19:07 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
assert 'id' in start_json
|
|
|
|
self.assertEquals("build-name", start_json['display_name'])
|
2015-04-30 19:47:40 +00:00
|
|
|
self.assertEquals(['bar'], start_json['tags'])
|
|
|
|
self.assertEquals('subdir', start_json['subdirectory'])
|
|
|
|
self.assertEquals('somesource', start_json['trigger']['build_source'])
|
2014-02-26 20:19:07 +00:00
|
|
|
|
2015-02-18 19:12:59 +00:00
|
|
|
# Verify the metadata was added.
|
|
|
|
build_obj = database.RepositoryBuild.get(database.RepositoryBuild.uuid == start_json['id'])
|
|
|
|
self.assertEquals('bar', py_json.loads(build_obj.job_config)['trigger_metadata']['foo'])
|
2014-03-25 00:57:02 +00:00
|
|
|
|
2015-08-07 17:01:49 +00:00
|
|
|
# Start another manual build, with a ref.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(ActivateBuildTrigger, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
|
|
|
data=dict(refs={'kind': 'branch',
|
|
|
|
'name': 'foobar'}), expected_code=201)
|
2017-03-08 01:39:42 +00:00
|
|
|
|
|
|
|
# Start another manual build with a null ref.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(ActivateBuildTrigger, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid), data=dict(refs=None),
|
2017-03-08 01:39:42 +00:00
|
|
|
expected_code=201)
|
2015-08-07 17:01:49 +00:00
|
|
|
|
2014-03-27 22:33:13 +00:00
|
|
|
def test_invalid_robot_account(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
database.BuildTriggerService.create(name='fakeservice')
|
|
|
|
|
|
|
|
# Add a new fake trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
trigger = model.build.create_build_trigger(repo, 'fakeservice', 'sometoken', user)
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
# Try to activate it with an invalid robot account.
|
|
|
|
trigger_config = {}
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(BuildTriggerActivate, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
|
|
|
data={'config': trigger_config,
|
|
|
|
'pull_robot': 'someinvalidrobot'}, expected_code=404)
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
def test_unauthorized_robot_account(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
database.BuildTriggerService.create(name='fakeservice')
|
|
|
|
|
|
|
|
# Add a new fake trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
trigger = model.build.create_build_trigger(repo, 'fakeservice', 'sometoken', user)
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
# Try to activate it with a robot account in the wrong namespace.
|
|
|
|
trigger_config = {}
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(BuildTriggerActivate, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
|
|
|
data={'config': trigger_config,
|
|
|
|
'pull_robot': 'freshuser+anotherrobot'}, expected_code=403)
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
def test_robot_account(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
database.BuildTriggerService.create(name='fakeservice')
|
|
|
|
|
|
|
|
# Add a new fake trigger.
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(ADMIN_ACCESS_USER, 'simple')
|
|
|
|
user = model.user.get_user(ADMIN_ACCESS_USER)
|
|
|
|
trigger = model.build.create_build_trigger(repo, 'fakeservice', 'sometoken', user)
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
# Try to activate it with a robot account.
|
|
|
|
trigger_config = {}
|
2017-07-24 15:05:15 +00:00
|
|
|
activate_json = self.postJsonResponse(
|
|
|
|
BuildTriggerActivate, params=dict(repository=ADMIN_ACCESS_USER + '/simple',
|
|
|
|
trigger_uuid=trigger.uuid),
|
|
|
|
data={'config': trigger_config,
|
|
|
|
'pull_robot': ADMIN_ACCESS_USER + '+dtrobot'})
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
# Verify that the robot was saved.
|
|
|
|
self.assertEquals(True, activate_json['is_active'])
|
2014-04-02 01:49:06 +00:00
|
|
|
self.assertEquals(ADMIN_ACCESS_USER + '+dtrobot', activate_json['pull_robot']['name'])
|
2014-03-27 22:33:13 +00:00
|
|
|
|
|
|
|
# Start a manual build.
|
2017-07-24 15:05:15 +00:00
|
|
|
start_json = self.postJsonResponse(ActivateBuildTrigger, params=dict(
|
|
|
|
repository=ADMIN_ACCESS_USER + '/simple', trigger_uuid=trigger.uuid),
|
2015-08-11 21:17:18 +00:00
|
|
|
data=dict(refs=dict(kind='branch', name='foobar')),
|
2014-03-27 22:33:13 +00:00
|
|
|
expected_code=201)
|
|
|
|
|
|
|
|
assert 'id' in start_json
|
|
|
|
self.assertEquals("build-name", start_json['display_name'])
|
2015-04-30 19:47:40 +00:00
|
|
|
self.assertEquals(['bar'], start_json['tags'])
|
2014-03-27 22:33:13 +00:00
|
|
|
|
2014-03-25 00:57:02 +00:00
|
|
|
|
|
|
|
class TestUserAuthorizations(ApiTestCase):
|
|
|
|
def test_list_get_delete_user_authorizations(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(UserAuthorizationList)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-25 00:57:02 +00:00
|
|
|
self.assertEquals(1, len(json['authorizations']))
|
|
|
|
|
|
|
|
authorization = json['authorizations'][0]
|
|
|
|
|
|
|
|
assert 'uuid' in authorization
|
|
|
|
assert 'scopes' in authorization
|
|
|
|
assert 'application' in authorization
|
|
|
|
|
|
|
|
# Retrieve the authorization.
|
2015-07-15 21:25:41 +00:00
|
|
|
get_json = self.getJsonResponse(UserAuthorization,
|
|
|
|
params=dict(access_token_uuid=authorization['uuid']))
|
2014-03-25 00:57:02 +00:00
|
|
|
self.assertEquals(authorization, get_json)
|
|
|
|
|
|
|
|
# Delete the authorization.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(UserAuthorization, params=dict(
|
|
|
|
access_token_uuid=authorization['uuid']))
|
2014-03-25 00:57:02 +00:00
|
|
|
|
|
|
|
# Verify it has been deleted.
|
2015-07-15 21:25:41 +00:00
|
|
|
self.getJsonResponse(UserAuthorization, params=dict(access_token_uuid=authorization['uuid']),
|
2014-03-25 00:57:02 +00:00
|
|
|
expected_code=404)
|
|
|
|
|
2014-05-12 19:22:58 +00:00
|
|
|
|
|
|
|
class TestSuperUserLogs(ApiTestCase):
|
|
|
|
def test_get_logs(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(SuperUserLogs)
|
|
|
|
|
|
|
|
assert 'logs' in json
|
|
|
|
assert len(json['logs']) > 0
|
|
|
|
|
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
class TestSuperUserTakeOwnership(ApiTestCase):
|
|
|
|
def test_take_ownership_superuser(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Should fail to take ownership of a superuser.
|
|
|
|
self.postResponse(SuperUserTakeOwnership, params=dict(namespace=ADMIN_ACCESS_USER),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
def test_take_ownership_invalid_namespace(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(SuperUserTakeOwnership, params=dict(namespace='invalid'), expected_code=404)
|
2016-06-07 22:12:11 +00:00
|
|
|
|
|
|
|
def test_take_ownership_non_superuser(self):
|
|
|
|
self.login(READ_ACCESS_USER)
|
|
|
|
self.postResponse(SuperUserTakeOwnership, params=dict(namespace='freshuser'),
|
|
|
|
expected_code=403)
|
|
|
|
|
|
|
|
def test_take_ownership_user(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
with assert_action_logged('take_ownership'):
|
|
|
|
# Take ownership of the read user.
|
|
|
|
self.postResponse(SuperUserTakeOwnership, params=dict(namespace=READ_ACCESS_USER))
|
|
|
|
|
|
|
|
# Ensure that the read access user is now an org, with the superuser as the owner.
|
|
|
|
reader = model.user.get_user_or_org(READ_ACCESS_USER)
|
|
|
|
self.assertTrue(reader.organization)
|
|
|
|
|
|
|
|
usernames = [admin.username for admin in model.organization.get_admin_users(reader)]
|
|
|
|
self.assertIn(ADMIN_ACCESS_USER, usernames)
|
|
|
|
|
|
|
|
def test_take_ownership_org(self):
|
|
|
|
# Create a new org with another user as owner.
|
|
|
|
public_user = model.user.get_user(PUBLIC_USER)
|
|
|
|
org = model.organization.create_organization('someorg', 'some@example.com', public_user)
|
|
|
|
|
|
|
|
# Ensure that the admin is not yet owner of the org.
|
|
|
|
usernames = [admin.username for admin in model.organization.get_admin_users(org)]
|
|
|
|
self.assertNotIn(ADMIN_ACCESS_USER, usernames)
|
|
|
|
|
|
|
|
with assert_action_logged('take_ownership'):
|
|
|
|
# Take ownership.
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
self.postResponse(SuperUserTakeOwnership, params=dict(namespace='someorg'))
|
|
|
|
|
|
|
|
# Ensure now in the admin users.
|
|
|
|
usernames = [admin.username for admin in model.organization.get_admin_users(org)]
|
|
|
|
self.assertIn(ADMIN_ACCESS_USER, usernames)
|
|
|
|
|
|
|
|
|
2016-04-05 19:27:45 +00:00
|
|
|
class TestSuperUserKeyManagement(ApiTestCase):
|
|
|
|
def test_get_update_keys(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKeyManagement)
|
2016-04-12 23:17:19 +00:00
|
|
|
key_count = len(json['keys'])
|
2016-04-05 19:27:45 +00:00
|
|
|
|
|
|
|
key = json['keys'][0]
|
|
|
|
self.assertTrue('name' in key)
|
|
|
|
self.assertTrue('service' in key)
|
|
|
|
self.assertTrue('kid' in key)
|
|
|
|
self.assertTrue('created_date' in key)
|
|
|
|
self.assertTrue('expiration_date' in key)
|
|
|
|
self.assertTrue('jwk' in key)
|
|
|
|
self.assertTrue('approval' in key)
|
|
|
|
self.assertTrue('metadata' in key)
|
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
with assert_action_logged('service_key_modify'):
|
|
|
|
# Update the key's name.
|
|
|
|
self.putJsonResponse(SuperUserServiceKey, params=dict(kid=key['kid']),
|
|
|
|
data=dict(name='somenewname'))
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
# Ensure the key's name has been changed.
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKey, params=dict(kid=key['kid']))
|
|
|
|
self.assertEquals('somenewname', json['name'])
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
with assert_action_logged('service_key_modify'):
|
|
|
|
# Update the key's metadata.
|
|
|
|
self.putJsonResponse(SuperUserServiceKey, params=dict(kid=key['kid']),
|
|
|
|
data=dict(metadata=dict(foo='bar')))
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
# Ensure the key's metadata has been changed.
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKey, params=dict(kid=key['kid']))
|
|
|
|
self.assertEquals('bar', json['metadata']['foo'])
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
with assert_action_logged('service_key_extend'):
|
|
|
|
# Change the key's expiration.
|
|
|
|
self.putJsonResponse(SuperUserServiceKey, params=dict(kid=key['kid']),
|
|
|
|
data=dict(expiration=None))
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
# Ensure the key's expiration has been changed.
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKey, params=dict(kid=key['kid']))
|
|
|
|
self.assertIsNone(json['expiration_date'])
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
with assert_action_logged('service_key_delete'):
|
|
|
|
# Delete the key.
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(SuperUserServiceKey, params=dict(kid=key['kid']))
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
# Ensure the key no longer exists.
|
|
|
|
self.getResponse(SuperUserServiceKey, params=dict(kid=key['kid']), expected_code=404)
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
json = self.getJsonResponse(SuperUserServiceKeyManagement)
|
|
|
|
self.assertEquals(key_count - 1, len(json['keys']))
|
2016-04-05 19:27:45 +00:00
|
|
|
|
2016-04-05 19:57:44 +00:00
|
|
|
def test_approve_key(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Ensure the key is not yet approved.
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKey, params=dict(kid='kid3'))
|
|
|
|
self.assertEquals('unapprovedkey', json['name'])
|
|
|
|
self.assertIsNone(json['approval'])
|
|
|
|
|
|
|
|
# Approve the key.
|
2016-06-07 22:12:11 +00:00
|
|
|
with assert_action_logged('service_key_approve'):
|
|
|
|
self.postResponse(SuperUserServiceKeyApproval, params=dict(kid='kid3'),
|
|
|
|
data=dict(notes='testapprove'), expected_code=201)
|
2016-04-05 19:57:44 +00:00
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
# Ensure the key is approved.
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKey, params=dict(kid='kid3'))
|
|
|
|
self.assertEquals('unapprovedkey', json['name'])
|
|
|
|
self.assertIsNotNone(json['approval'])
|
|
|
|
self.assertEquals('ServiceKeyApprovalType.SUPERUSER', json['approval']['approval_type'])
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['approval']['approver']['username'])
|
|
|
|
self.assertEquals('testapprove', json['approval']['notes'])
|
2016-04-05 19:57:44 +00:00
|
|
|
|
|
|
|
def test_approve_preapproved(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
new_key = {
|
|
|
|
'service': 'coolservice',
|
|
|
|
'name': 'mynewkey',
|
|
|
|
'metadata': dict(foo='baz'),
|
|
|
|
'notes': 'whazzup!?',
|
|
|
|
'expiration': timegm((datetime.datetime.now() + datetime.timedelta(days=1)).utctimetuple()),
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create the key (preapproved automatically)
|
|
|
|
json = self.postJsonResponse(SuperUserServiceKeyManagement, data=new_key)
|
|
|
|
|
|
|
|
# Try to approve again.
|
|
|
|
self.postResponse(SuperUserServiceKeyApproval, params=dict(kid=json['kid']), expected_code=201)
|
2016-04-05 19:27:45 +00:00
|
|
|
|
|
|
|
def test_create_key(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
new_key = {
|
|
|
|
'service': 'coolservice',
|
|
|
|
'name': 'mynewkey',
|
|
|
|
'metadata': dict(foo='baz'),
|
|
|
|
'notes': 'whazzup!?',
|
|
|
|
'expiration': timegm((datetime.datetime.now() + datetime.timedelta(days=1)).utctimetuple()),
|
|
|
|
}
|
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
with assert_action_logged('service_key_create'):
|
|
|
|
# Create the key.
|
|
|
|
json = self.postJsonResponse(SuperUserServiceKeyManagement, data=new_key)
|
|
|
|
self.assertEquals('mynewkey', json['name'])
|
|
|
|
self.assertTrue('kid' in json)
|
|
|
|
self.assertTrue('public_key' in json)
|
|
|
|
self.assertTrue('private_key' in json)
|
|
|
|
|
|
|
|
# Verify the private key is a valid PEM.
|
2017-07-24 15:05:15 +00:00
|
|
|
serialization.load_pem_private_key(json['private_key'].encode('utf-8'), None,
|
|
|
|
default_backend())
|
2016-06-07 22:12:11 +00:00
|
|
|
|
|
|
|
# Verify the key.
|
|
|
|
kid = json['kid']
|
|
|
|
|
|
|
|
json = self.getJsonResponse(SuperUserServiceKey, params=dict(kid=kid))
|
|
|
|
self.assertEquals('mynewkey', json['name'])
|
|
|
|
self.assertEquals('coolservice', json['service'])
|
|
|
|
self.assertEquals('baz', json['metadata']['foo'])
|
|
|
|
self.assertEquals(kid, json['kid'])
|
|
|
|
|
|
|
|
self.assertIsNotNone(json['approval'])
|
|
|
|
self.assertEquals('ServiceKeyApprovalType.SUPERUSER', json['approval']['approval_type'])
|
|
|
|
self.assertEquals(ADMIN_ACCESS_USER, json['approval']['approver']['username'])
|
|
|
|
self.assertEquals('whazzup!?', json['approval']['notes'])
|
2016-04-05 19:27:45 +00:00
|
|
|
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
class TestRepositoryManifestLabels(ApiTestCase):
|
|
|
|
def test_basic_labels(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, 'complex')
|
|
|
|
tag = registry_model.get_repo_tag(repo_ref, 'prod')
|
2016-07-18 22:20:00 +00:00
|
|
|
repository = ADMIN_ACCESS_USER + '/complex'
|
|
|
|
|
|
|
|
# Check the existing labels on the complex repo, which should be empty
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(
|
|
|
|
RepositoryManifestLabels,
|
2018-08-24 19:04:22 +00:00
|
|
|
params=dict(repository=repository, manifestref=tag.manifest_digest))
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
self.assertEquals(0, len(json['labels']))
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2017-06-20 19:50:46 +00:00
|
|
|
data=dict(key='bad_label', value='world',
|
2017-07-24 15:05:15 +00:00
|
|
|
media_type='text/plain'), expected_code=400)
|
2017-06-20 19:50:46 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2017-06-20 19:50:46 +00:00
|
|
|
data=dict(key='hello', value='world',
|
2017-07-24 15:05:15 +00:00
|
|
|
media_type='bad_media_type'), expected_code=400)
|
2017-06-20 19:50:46 +00:00
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
# Add some labels to the manifest.
|
|
|
|
with assert_action_logged('manifest_label_add'):
|
2017-07-24 15:05:15 +00:00
|
|
|
label1 = self.postJsonResponse(RepositoryManifestLabels, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest), data=dict(
|
2017-07-24 15:05:15 +00:00
|
|
|
key='hello', value='world', media_type='text/plain'), expected_code=201)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
with assert_action_logged('manifest_label_add'):
|
2017-07-24 15:05:15 +00:00
|
|
|
label2 = self.postJsonResponse(RepositoryManifestLabels, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest), data=dict(
|
2017-07-24 15:05:15 +00:00
|
|
|
key='hi', value='there', media_type='text/plain'), expected_code=201)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
with assert_action_logged('manifest_label_add'):
|
2017-07-24 15:05:15 +00:00
|
|
|
label3 = self.postJsonResponse(RepositoryManifestLabels, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest), data=dict(
|
2017-07-24 15:05:15 +00:00
|
|
|
key='hello', value='someone', media_type='application/json'), expected_code=201)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
# Ensure we have *3* labels
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryManifestLabels, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest))
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
self.assertEquals(3, len(json['labels']))
|
|
|
|
|
|
|
|
self.assertNotEquals(label2['label']['id'], label1['label']['id'])
|
|
|
|
self.assertNotEquals(label3['label']['id'], label1['label']['id'])
|
|
|
|
self.assertNotEquals(label2['label']['id'], label3['label']['id'])
|
|
|
|
|
|
|
|
self.assertEquals('text/plain', label1['label']['media_type'])
|
|
|
|
self.assertEquals('text/plain', label2['label']['media_type'])
|
|
|
|
self.assertEquals('application/json', label3['label']['media_type'])
|
|
|
|
|
2016-11-17 19:55:14 +00:00
|
|
|
# Ensure we can retrieve each of the labels.
|
|
|
|
for label in json['labels']:
|
2017-07-24 15:05:15 +00:00
|
|
|
label_json = self.getJsonResponse(ManageRepositoryManifestLabel, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest, labelid=label['id']))
|
2016-11-17 19:55:14 +00:00
|
|
|
self.assertEquals(label['id'], label_json['id'])
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
# Delete a label.
|
|
|
|
with assert_action_logged('manifest_label_delete'):
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(ManageRepositoryManifestLabel, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest, labelid=label1['label']['id']))
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
# Ensure the label is gone.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryManifestLabels, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest))
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
self.assertEquals(2, len(json['labels']))
|
|
|
|
|
|
|
|
# Check filtering.
|
2017-07-24 15:05:15 +00:00
|
|
|
json = self.getJsonResponse(RepositoryManifestLabels, params=dict(
|
2018-08-24 19:04:22 +00:00
|
|
|
repository=repository, manifestref=tag.manifest_digest, filter='hello'))
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
self.assertEquals(1, len(json['labels']))
|
|
|
|
|
|
|
|
def test_prefixed_labels(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, 'complex')
|
|
|
|
tag = registry_model.get_repo_tag(repo_ref, 'prod')
|
2016-07-18 22:20:00 +00:00
|
|
|
repository = ADMIN_ACCESS_USER + '/complex'
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2016-07-18 22:20:00 +00:00
|
|
|
data=dict(key='com.dockers.whatever', value='pants',
|
2017-07-24 15:05:15 +00:00
|
|
|
media_type='text/plain'), expected_code=201)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postJsonResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2016-07-18 22:20:00 +00:00
|
|
|
data=dict(key='my.cool.prefix.for.my.label', value='value',
|
2017-07-24 15:05:15 +00:00
|
|
|
media_type='text/plain'), expected_code=201)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
def test_add_invalid_media_type(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, 'complex')
|
|
|
|
tag = registry_model.get_repo_tag(repo_ref, 'prod')
|
2016-07-18 22:20:00 +00:00
|
|
|
repository = ADMIN_ACCESS_USER + '/complex'
|
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2016-07-18 22:20:00 +00:00
|
|
|
data=dict(key='hello', value='world', media_type='some/invalid'),
|
|
|
|
expected_code=400)
|
|
|
|
|
|
|
|
def test_add_invalid_key(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2018-08-24 19:04:22 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(ADMIN_ACCESS_USER, 'complex')
|
|
|
|
tag = registry_model.get_repo_tag(repo_ref, 'prod')
|
2016-07-18 22:20:00 +00:00
|
|
|
repository = ADMIN_ACCESS_USER + '/complex'
|
|
|
|
|
|
|
|
# Try to add an empty label key.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(key='', value='world'), expected_code=400)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
# Try to add an invalid label key.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(key='invalid___key', value='world'), expected_code=400)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
# Try to add a label key in a reserved namespace.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.postResponse(RepositoryManifestLabels, params=dict(repository=repository,
|
2018-08-24 19:04:22 +00:00
|
|
|
manifestref=tag.manifest_digest),
|
2017-07-24 15:05:15 +00:00
|
|
|
data=dict(key='io.docker.whatever', value='world'), expected_code=400)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
|
2014-05-12 19:22:58 +00:00
|
|
|
class TestSuperUserManagement(ApiTestCase):
|
|
|
|
def test_get_user(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(SuperUserManagement, params=dict(username='freshuser'))
|
2014-05-12 19:22:58 +00:00
|
|
|
self.assertEquals('freshuser', json['username'])
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals('jschorr+test@devtable.com', json['email'])
|
2014-11-24 21:07:38 +00:00
|
|
|
self.assertEquals(False, json['super_user'])
|
2014-05-12 19:22:58 +00:00
|
|
|
|
|
|
|
def test_delete_user(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Verify the user exists.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(SuperUserManagement, params=dict(username='freshuser'))
|
2014-05-12 19:22:58 +00:00
|
|
|
self.assertEquals('freshuser', json['username'])
|
|
|
|
|
|
|
|
# Delete the user.
|
2017-07-24 15:05:15 +00:00
|
|
|
self.deleteEmptyResponse(SuperUserManagement, params=dict(username='freshuser'),
|
|
|
|
expected_code=204)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-05-12 19:22:58 +00:00
|
|
|
# Verify the user no longer exists.
|
2015-07-15 21:25:41 +00:00
|
|
|
self.getResponse(SuperUserManagement, params=dict(username='freshuser'), expected_code=404)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2015-06-30 16:42:19 +00:00
|
|
|
def test_change_user_password(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Verify the user exists.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(SuperUserManagement, params=dict(username='freshuser'))
|
2015-06-30 16:42:19 +00:00
|
|
|
self.assertEquals('freshuser', json['username'])
|
|
|
|
self.assertEquals('jschorr+test@devtable.com', json['email'])
|
|
|
|
|
|
|
|
# Update the user.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.putJsonResponse(SuperUserManagement, params=dict(username='freshuser'),
|
|
|
|
data=dict(password='somepassword'))
|
2015-06-30 16:42:19 +00:00
|
|
|
self.assertTrue('encrypted_password' in json)
|
2014-05-12 19:22:58 +00:00
|
|
|
|
|
|
|
def test_update_user(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Verify the user exists.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(SuperUserManagement, params=dict(username='freshuser'))
|
2014-05-12 19:22:58 +00:00
|
|
|
self.assertEquals('freshuser', json['username'])
|
2014-08-18 21:24:00 +00:00
|
|
|
self.assertEquals('jschorr+test@devtable.com', json['email'])
|
2014-05-12 19:22:58 +00:00
|
|
|
|
|
|
|
# Update the user.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.putJsonResponse(SuperUserManagement, params=dict(username='freshuser'),
|
|
|
|
data=dict(email='foo@bar.com'))
|
2015-06-30 16:42:19 +00:00
|
|
|
self.assertFalse('encrypted_password' in json)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-05-12 19:22:58 +00:00
|
|
|
# Verify the user was updated.
|
2015-07-15 21:25:41 +00:00
|
|
|
json = self.getJsonResponse(SuperUserManagement, params=dict(username='freshuser'))
|
2014-05-12 19:22:58 +00:00
|
|
|
self.assertEquals('freshuser', json['username'])
|
|
|
|
self.assertEquals('foo@bar.com', json['email'])
|
|
|
|
|
2016-10-07 14:22:30 +00:00
|
|
|
def test_set_message(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
|
|
|
|
|
|
|
# Create a message
|
2017-01-17 21:41:24 +00:00
|
|
|
message = {"content": "new message", "severity": "info", "media_type": "text/plain"}
|
|
|
|
self.postResponse(GlobalUserMessages, data=dict(message=message), expected_code=201)
|
2016-10-07 19:56:58 +00:00
|
|
|
|
2016-10-17 19:43:03 +00:00
|
|
|
json = self.getJsonResponse(GlobalUserMessages)
|
2017-01-17 21:41:24 +00:00
|
|
|
self.assertEquals(len(json['messages']), 3)
|
2016-10-07 19:56:58 +00:00
|
|
|
|
2017-01-23 18:36:11 +00:00
|
|
|
has_matching_message = False
|
|
|
|
for message in json["messages"]:
|
|
|
|
new_message_match = message["content"] == "new message"
|
|
|
|
severity_match = message["severity"] == "info"
|
|
|
|
media_type_match = message["media_type"] == "text/plain"
|
|
|
|
if new_message_match and severity_match and media_type_match:
|
|
|
|
has_matching_message = True
|
|
|
|
break
|
2017-01-17 21:41:24 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
self.assertTrue(has_matching_message,
|
|
|
|
"Could not find matching message in: " + str(json["messages"]))
|
2017-01-17 21:41:24 +00:00
|
|
|
self.assertNotEqual(json['messages'][0]["content"], json['messages'][2]["content"])
|
|
|
|
self.assertTrue(json['messages'][2]["uuid"])
|
2016-10-11 19:09:38 +00:00
|
|
|
|
|
|
|
def test_delete_message(self):
|
|
|
|
self.login(ADMIN_ACCESS_USER)
|
2016-10-17 19:43:03 +00:00
|
|
|
json = self.getJsonResponse(GlobalUserMessages)
|
2016-12-14 21:24:06 +00:00
|
|
|
self.deleteEmptyResponse(GlobalUserMessage, {"uuid": json['messages'][0]['uuid']}, 204)
|
2016-10-11 19:09:38 +00:00
|
|
|
|
2016-10-17 19:43:03 +00:00
|
|
|
json = self.getJsonResponse(GlobalUserMessages)
|
2016-10-11 19:09:38 +00:00
|
|
|
|
2017-01-17 21:41:24 +00:00
|
|
|
self.assertEquals(len(json['messages']), 1)
|
2014-05-12 19:22:58 +00:00
|
|
|
|
2017-07-24 15:05:15 +00:00
|
|
|
|
2014-01-31 21:19:29 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|