def test_get_without_own_record(self): graph = Graph(user_id=4, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() response = self._get(None) self.assertEquals(response.status_code, httplib.OK) self.assertEquals(json.loads(response.content), [])
def test_delete_with_id_nonexistent(self): graph = Graph(user_id=5) graph.save() nonexistent_id = graph.id graph.delete() response = self._delete(nonexistent_id) self.assertEquals(response.status_code, httplib.NOT_FOUND)
def test_delete_with_id_nonowner(self): graph = Graph( user_id=4, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() response = self._delete(graph.id) self.assertEquals(response.status_code, httplib.FORBIDDEN)
def test_delete_with_id(self): graph = Graph(user_id=5, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() response = self._delete(graph.id) self.assertEquals(response.status_code, httplib.OK) self.assertRaises(Graph.DoesNotExist, lambda: Graph.objects.get(id=graph.id))
def test_get_with_id_nonexsitent(self): graph = Graph(user_id=5, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() id = graph.id graph.delete() response = self._get(id) self.assertEquals(response.status_code, httplib.NOT_FOUND)
def test_put_with_id_nonexistent(self): graph = Graph(user_id=5, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() nonexistent_id = graph.id graph.delete() record = { 'server_id': 4, 'host_id': 5, 'item_id': 6, } response = self._put(nonexistent_id, record) self.assertEquals(response.status_code, httplib.NOT_FOUND)
def test_get(self): graph = Graph(user_id=5, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() response = self._get(None) self.assertEquals(response.status_code, httplib.OK) record = { 'id': graph.id, 'user_id': graph.user_id, 'server_id': 1, 'host_id': 2, 'item_id': 3, } self.assertEquals(json.loads(response.content), [record])
def test_put_with_id_nonowner(self): graph = Graph(user_id=4, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() new_record = { 'server_id': 4, 'host_id': 5, 'item_id': 6, } settings_json = json.dumps(new_record) response = self._put(graph.id, settings_json) self.assertEquals(response.status_code, httplib.FORBIDDEN) graph_in_db = Graph.objects.get(id=graph.id) self.assertEquals(graph, graph_in_db)
def test_put_with_id(self): graph = Graph(user_id=5, settings_json='{"server_id":1,"host_id":2,"item_id":3}') graph.save() new_record = { 'server_id': 4, 'host_id': 5, 'item_id': 6, } settings_json = json.dumps(new_record) response = self._put(graph.id, settings_json) self.assertEquals(response.status_code, httplib.OK) record = { 'id': graph.id, 'user_id': graph.user_id, } record.update(new_record) self.assertEquals(json.loads(response.content), record)
def graphs(request, id): content_type = 'application/json' try: user_id = get_user_id_from_hatohol_server(request) except (NoHatoholUser, NoHatoholSession): return http.HttpResponseForbidden(content_type=content_type) if request.method == 'POST': graph = Graph(user_id=user_id, settings_json=request.body) try: graph.full_clean() except ValidationError as e: return http.HttpResponseBadRequest(json.dumps(e.messages), content_type=content_type) graph.save() response = http.HttpResponse(to_json(graph), content_type=content_type, status=201) response['Location'] = reverse('hatohol.views.graphs', args=[graph.id]) return response elif request.method == 'PUT': if id is None: message = 'id is required' return http.HttpResponseBadRequest(to_json(message), content_type=content_type) try: graph = Graph.objects.get(id=id) if graph.user_id != user_id: return http.HttpResponseForbidden(content_type=content_type) graph.settings_json = request.body graph.full_clean() graph.save() return http.HttpResponse(to_json(graph), content_type=content_type) except Graph.DoesNotExist: return http.HttpResponseNotFound(content_type=content_type) except ValidationError as e: return http.HttpResponseBadRequest(json.dumps(e.messages), content_type=content_type) elif request.method == 'DELETE': if id is None: message = 'id is required' return http.HttpResponseBadRequest(to_json(message), content_type=content_type) try: graph = Graph.objects.get(id=id) except Graph.DoesNotExist: return http.HttpResponseNotFound() else: if graph.user_id != user_id: return http.HttpResponseForbidden(content_type=content_type) graph.delete() return http.HttpResponse() else: if id: try: graph = Graph.objects.get(id=id) except Graph.DoesNotExist: return http.HttpResponseNotFound() if graph.user_id != user_id: return http.HttpResponseForbidden(content_type=content_type) response = graph else: graphs = Graph.objects.filter(user_id=user_id).order_by('id') response = graphs return http.HttpResponse(to_json(response), content_type=content_type)