Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mozilla/balrog
Browse files Browse the repository at this point in the history
  • Loading branch information
bhearsum committed Jan 9, 2018
2 parents bdbae4d + 21641b8 commit a67fc36
Show file tree
Hide file tree
Showing 23 changed files with 1,251 additions and 314 deletions.
18 changes: 10 additions & 8 deletions auslib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2295,10 +2295,16 @@ def assertOptionsExist(self, permission, options):
raise ValueError('Unknown option "%s" for permission "%s"' % (opt, permission))

def getAllUsers(self, transaction=None):
res_permissions = self.select(columns=[self.username], distinct=True, transaction=transaction)
res_roles = self.user_roles.select(columns=[self.user_roles.username], transaction=transaction)
res = res_roles + res_permissions
return list(set([r['username'] for r in res]))
res_users = self.select(columns=[self.username], distinct=True, transaction=transaction)
users_list = list([r['username'] for r in res_users])
users = {}
for user in users_list:
res_roles = self.user_roles.select(where=[
self.user_roles.username == user],
columns=[self.user_roles.role, self.user_roles.data_version],
transaction=transaction)
users[user] = {"roles": res_roles}
return users

def getAllPermissions(self, transaction=None):
ret = defaultdict(dict)
Expand Down Expand Up @@ -2423,10 +2429,6 @@ def getUserRoles(self, username, transaction=None):
distinct=True, transaction=transaction)
return [{"role": r["role"], "data_version": r["data_version"]} for r in res]

def getAllRoles(self, transaction=None):
res = self.user_roles.select(columns=[self.user_roles.role], distinct=True, transaction=transaction)
return [r["role"] for r in res]

def isAdmin(self, username, transaction=None):
return bool(self.getPermission(username, "admin", transaction))

Expand Down
73 changes: 54 additions & 19 deletions auslib/test/admin/views/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ def testUsers(self):
ret = self._get('/users')
self.assertEqual(ret.status_code, 200)
data = json.loads(ret.data)
data['users'] = set(data['users'])
self.assertEqual(data, dict(users=set(['bill', 'billy', 'bob', 'ashanti', 'mary', 'julie'])))
self.assertEqual(data, ({
'ashanti': {'roles': []},
'bill': {'roles': [
{'role': 'qa', 'data_version': 1},
{'role': 'releng', 'data_version': 1}]},
'billy': {'roles': []},
'bob': {'roles': [{'role': 'relman', 'data_version': 1}]},
'julie': {'roles': [{'role': 'releng', 'data_version': 1}]},
'mary': {'roles': [{'role': 'relman', 'data_version': 1}]}}))


class TestCurrentUserAPI_JSON(ViewTest):
Expand Down Expand Up @@ -763,6 +770,51 @@ def testGetScheduledChangeHistoryRevisions(self):
}
self.assertEquals(json.loads(ret.data), expected)

def testGetScheduledChangeHistory(self):
ret = self._get("/permissions_scheduled_change/history")
self.assertEquals(ret.status_code, 200, ret.data)
expected = {
"count": 7,
"revisions": [
{
"change_id": 13, "changed_by": "bill", "complete": False, "timestamp": 405, "sc_id": 6, "scheduled_by": "bill",
"change_type": "update", "data_version": 1, "permission": "release", "options": {"products": ["a", "b"]},
"username": "bob", "when": 38000000, "sc_data_version": 1
},
{
"change_id": 11, "changed_by": "bill", "complete": False, "timestamp": 205, "sc_id": 5, "scheduled_by": "bill",
"change_type": "insert", "data_version": None, "permission": "rule", "username": "joe", "options": {"products": ["fake"]},
"when": 98000000, "sc_data_version": 1
},
{
"change_id": 9, "changed_by": "bill", "timestamp": 201, "sc_id": 4, "scheduled_by": "bill", "change_type": "delete", "data_version": None,
"permission": "scheduled_change", "username": "mary", "complete": False, "options": None, "sc_data_version": 1, "when": 76000000
},
{
"change_id": 7, "changed_by": "bill", "timestamp": 100, "sc_id": 3, "scheduled_by": "bill", "change_type": "insert",
"data_version": None, "permission": "permission", "username": "bob", "options": None, "when": 30000000, "complete": True,
"sc_data_version": 2,
},
{
"change_id": 6, "changed_by": "bill", "timestamp": 61, "sc_id": 3, "scheduled_by": "bill", "change_type": "insert",
"data_version": None, "permission": "permission", "username": "bob", "options": None, "when": 30000000, "complete": False,
"sc_data_version": 1,
},
{
"change_id": 4, "changed_by": "bill", "complete": False, "timestamp": 41, "sc_id": 2, "scheduled_by": "bill",
"change_type": "update", "data_version": 1, "permission": "release_locale", "username": "ashanti", "options": None,
"sc_data_version": 1, "when": 20000000
},
{
"change_id": 2, "changed_by": "bill", "complete": False, "timestamp": 21, "sc_id": 1, "scheduled_by": "bill",
"change_type": "insert", "data_version": None, "permission": "rule", "username": "janet", "options": {"products": ["foo"]},
"sc_data_version": 1, "when": 10000000
},
],
}

self.assertEquals(json.loads(ret.data), expected)

@mock.patch("time.time", mock.MagicMock(return_value=100))
def testSignoffWithPermission(self):
ret = self._post("/scheduled_changes/permissions/2/signoffs", data=dict(role="relman"), username="bob")
Expand Down Expand Up @@ -848,23 +900,6 @@ def testAddNewFullAdminPermission(self):

class TestUserRolesAPI_JSON(ViewTest):

def testGetRoles(self):
ret = self._get("/users/bill/roles")
self.assertStatusCode(ret, 200)
got = json.loads(ret.data)["roles"]
self.assertEquals(got, [{"role": "qa", "data_version": 1},
{"role": "releng", "data_version": 1}])

def testGetAllRoles(self):
ret = self._get("/users/roles")
self.assertStatusCode(ret, 200)
got = json.loads(ret.data)["roles"]
self.assertEqual(got, ['releng', 'qa', 'relman'])

def testGetRolesMissingUserReturnsEmptyList(self):
ret = self.client.get("/users/dean/roles")
self.assertStatusCode(ret, 200)

def testGrantRole(self):
ret = self._put("/users/ashanti/roles/dev")
self.assertStatusCode(ret, 201)
Expand Down
101 changes: 101 additions & 0 deletions auslib/test/admin/views/test_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,42 @@ def testGetScheduledChangeHistoryRevisions(self):
}
self.assertEquals(ret, expected)

def testGetScheduledChangeHistory(self):
ret = self._get("/releases_scheduled_change/history")
self.assertEquals(ret.status_code, 200, ret.data)
ret = json.loads(ret.data)
expected = {
'count': 5,
'revisions': [
{
'product': 'a', 'changed_by': 'bill', 'data': {'name': 'a', 'extv': '2.0', 'hashFunction': 'sha512', 'schema_version': 1},
'sc_id': 2, 'sc_data_version': 1, 'scheduled_by': 'bill', 'data_version': 1, 'complete': False, 'when': 6000000000,
'change_type': 'update', 'name': 'a', 'timestamp': 71, 'change_id': 4, 'read_only': False
},
{
'product': 'm', 'changed_by': 'bill', 'data': {'name': 'm', 'hashFunction': 'sha512', 'schema_version': 1},
'sc_id': 1, 'sc_data_version': 1, 'scheduled_by': 'bill', 'data_version': None, 'complete': False,
'when': 4000000000, 'change_type': 'insert', 'name': 'm', 'timestamp': 51, 'change_id': 2, 'read_only': False
},
{
'product': None, 'changed_by': 'bill', 'data': None, 'sc_id': 4, 'sc_data_version': 1, 'scheduled_by': 'bill',
'data_version': 1, 'complete': False, 'when': 230000000, 'change_type': 'delete', 'name': 'ab', 'timestamp': 26,
'change_id': 9, 'read_only': False
},
{
'product': 'b', 'changed_by': 'bill', 'data': {'name': 'b', 'hashFunction': 'sha512', 'schema_version': 1},
'sc_id': 3, 'sc_data_version': 2, 'scheduled_by': 'bill', 'data_version': 1, 'complete': True, 'when': 10000000,
'change_type': 'update', 'name': 'b', 'timestamp': 25, 'change_id': 7, 'read_only': False
},
{
'product': 'b', 'changed_by': 'bill', 'data': {'name': 'b', 'hashFunction': 'sha512', 'schema_version': 1},
'sc_id': 3, 'sc_data_version': 1, 'scheduled_by': 'bill', 'data_version': 1, 'complete': False,
'when': 10000000, 'change_type': 'update', 'name': 'b', 'timestamp': 7, 'change_id': 6, 'read_only': False
},
],
}
self.assertEquals(ret, expected)

@mock.patch("time.time", mock.MagicMock(return_value=100))
def testSignoffWithPermission(self):
ret = self._post("/scheduled_changes/releases/1/signoffs", data=dict(role="qa"), username="bill")
Expand Down Expand Up @@ -1729,6 +1765,71 @@ def testGetRevisions(self):
with self.assertRaises(KeyError):
data['data']

def testGetHistory(self):
url = '/releases/history'
ret = self._get(url)
self.assertEquals(ret.status_code, 200, msg=ret.data)
data = json.loads(ret.data)
self.assertEquals(data["count"], 3)
expected = {
'count': 3,
'revisions': [
{
'product': 'b', 'data_version': 1, 'changed_by': 'bill', '_different': [], 'read_only': 'False',
'change_id': 6, '_time_ago': '48 years and 583 months ago', 'name': 'b', 'timestamp': 16
},
{
'product': 'd', 'data_version': 1, 'changed_by': 'bill', '_different': ['product', 'name', 'data'],
'read_only': 'False', 'change_id': 4, '_time_ago': '48 years and 583 months ago', 'name': 'd',
'timestamp': 10
},
{
'product': 'a', 'data_version': 1, 'changed_by': 'bill', '_different': ['product', 'name', 'data'],
'read_only': 'False', 'change_id': 2, '_time_ago': '48 years and 583 months ago', 'name': 'ab',
'timestamp': 6
},
],
}
revisions = data["revisions"]
expected_revisions = expected["revisions"]
for index in range(len(revisions)):
self.assertEquals(revisions[index]['product'], expected_revisions[index]['product'])
self.assertEquals(revisions[index]['timestamp'], expected_revisions[index]['timestamp'])
self.assertEquals(revisions[index]['read_only'], expected_revisions[index]['read_only'])
self.assertEquals(revisions[index]['data_version'], expected_revisions[index]['data_version'])
self.assertEquals(revisions[index]['changed_by'], expected_revisions[index]['changed_by'])
self.assertEquals(len(data["revisions"]), 3)

def testGetHistoryWithTimeRange(self):
url = '/releases/history'
ret = self._get(url, qs={"timestamp_from": 8})
self.assertEquals(ret.status_code, 200, msg=ret.data)
data = json.loads(ret.data)
self.assertEquals(data["count"], 2)
expected = {
'count': 2,
'revisions': [
{
'product': 'b', 'data_version': 1, 'changed_by': 'bill', '_different': [], 'read_only': 'False',
'change_id': 6, '_time_ago': '48 years and 583 months ago', 'name': 'b', 'timestamp': 16
},
{
'product': 'd', 'data_version': 1, 'changed_by': 'bill', '_different': ['product', 'name', 'data'],
'read_only': 'False', 'change_id': 4, '_time_ago': '48 years and 583 months ago', 'name': 'd',
'timestamp': 10
},
],
}
revisions = data["revisions"]
expected_revisions = expected["revisions"]
for index in range(len(revisions)):
self.assertEquals(revisions[index]['product'], expected_revisions[index]['product'])
self.assertEquals(revisions[index]['timestamp'], expected_revisions[index]['timestamp'])
self.assertEquals(revisions[index]['read_only'], expected_revisions[index]['read_only'])
self.assertEquals(revisions[index]['data_version'], expected_revisions[index]['data_version'])
self.assertEquals(revisions[index]['changed_by'], expected_revisions[index]['changed_by'])
self.assertEquals(len(data["revisions"]), 2)

def testPostRevisionRollback(self):
# Make some changes to a release
data = json.dumps(dict(detailsUrl='beep', fakePartials=True, schema_version=1))
Expand Down
Loading

0 comments on commit a67fc36

Please sign in to comment.