From 14eb3005b689174fa84ec62ccc8cdd1f60b1a35c Mon Sep 17 00:00:00 2001 From: charltonaustin Date: Mon, 10 Oct 2016 12:55:00 -0400 Subject: [PATCH] Some fixes for code review. --- data/model/message.py | 5 +---- endpoints/api/superuser.py | 38 +++++++++++++++++++------------------- initdb.py | 2 +- test/test_api_usage.py | 8 +++++--- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/data/model/message.py b/data/model/message.py index 779595746..420733548 100644 --- a/data/model/message.py +++ b/data/model/message.py @@ -2,10 +2,7 @@ from data.database import Messages def get_messages(): - messages = [] - for message in Messages.select(): - messages.append({'id': message.id, 'content': message.content}) - return messages + return Messages.select() def create(messages): for message in messages: diff --git a/endpoints/api/superuser.py b/endpoints/api/superuser.py index fa6af3803..0de8ae78f 100644 --- a/endpoints/api/superuser.py +++ b/endpoints/api/superuser.py @@ -820,9 +820,10 @@ class SuperUserServiceKeyApproval(ApiResource): abort(403) + @resource('/v1/messages') @show_if(features.SUPER_USERS) -class Messages(ApiResource): +class SuperUserMessages(ApiResource): """ Resource for getting a list of super user messages """ schemas = { @@ -831,21 +832,17 @@ class Messages(ApiResource): 'type': 'object', 'description': 'Messages that a super user has saved in the past', 'properties': { - 'messages': { - 'type': 'array', - 'description': 'Array of message', - 'items': { - 'type': 'object', - 'description': 'A single message', - 'properties': { - 'id':{ - 'type': 'integer', - 'description': 'The message id', - }, - 'content': { - 'type': 'string', - 'description': 'The actual message', - }, + 'message': { + 'type': 'object', + 'description': 'A single message', + 'properties': { + 'id': { + 'type': 'integer', + 'description': 'The message id', + }, + 'content': { + 'type': 'string', + 'description': 'The actual message', }, } }, @@ -856,9 +853,9 @@ class Messages(ApiResource): @nickname('getMessages') def get(self): """ Return a super users messages """ - messages = model.message.get_messages() + messages = list(model.message.get_messages()) return { - 'messages': messages, + 'messages': [message_view(m) for m in messages], } @require_scope(scopes.SUPERUSER) @@ -867,4 +864,7 @@ class Messages(ApiResource): def post(self): """ Create a message """ body = request.get_json() - model.message.create(body['messages']) + model.message.create([body['message']]) + +def message_view(message): + return {'id': message.id, 'content': message.content} diff --git a/initdb.py b/initdb.py index 3ce6b487b..07fa92039 100644 --- a/initdb.py +++ b/initdb.py @@ -776,7 +776,7 @@ def populate_database(minimal=False, with_storage=False): 'trigger_id': trigger.uuid, 'config': json.loads(trigger.config), 'service': trigger.service.name}) - model.message.create([{'content': 'We love you Quay customers!'}]) + model.message.create([{'content': 'We love you, Quay customers!'}]) fake_queue = WorkQueue('fakequeue', tf) fake_queue.put(['canonical', 'job', 'name'], '{}') diff --git a/test/test_api_usage.py b/test/test_api_usage.py index 90665aff9..382719af7 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -66,7 +66,7 @@ from endpoints.api.permission import (RepositoryUserPermission, RepositoryTeamPe from endpoints.api.superuser import (SuperUserLogs, SuperUserList, SuperUserManagement, SuperUserServiceKeyManagement, SuperUserServiceKey, SuperUserServiceKeyApproval, SuperUserTakeOwnership, - Messages) + SuperUserMessages) from endpoints.api.secscan import RepositoryImageSecurity from endpoints.api.suconfig import (SuperUserRegistryStatus, SuperUserConfig, SuperUserConfigFile, SuperUserCreateInitialSuperUser) @@ -4283,11 +4283,13 @@ class TestSuperUserManagement(ApiTestCase): self.login(ADMIN_ACCESS_USER) # Create a message - self.postJsonResponse(Messages, data=dict(messages=[{"content": "new message"}])) + self.postJsonResponse(SuperUserMessages, data=dict(message={"content": "new message"})) - json = self.getJsonResponse(Messages) + json = self.getJsonResponse(SuperUserMessages) self.assertEquals(len(json['messages']), 2) + self.assertEquals(json['messages'][1]["content"], "new message") + self.assertEquals(json['messages'][1]["id"], 2) if __name__ == '__main__': unittest.main()