From: Maciej Wereski Date: Fri, 6 Oct 2017 15:43:18 +0000 (+0200) Subject: HTTP API: Implement modification of Boruta requests X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9082c51b1863b2af78c458a6584112a521c10a58;p=tools%2Fboruta.git HTTP API: Implement modification of Boruta requests Change-Id: I8d91554776871dfd7be85f64d868c81911682491 Signed-off-by: Maciej Wereski --- diff --git a/server/api/v1/error.go b/server/api/v1/error.go index a5867d1..330523c 100644 --- a/server/api/v1/error.go +++ b/server/api/v1/error.go @@ -47,6 +47,9 @@ var ( // ErrBadID is returned when User provided ID which can't be parsed into // uint. ErrBadID = errors.New("ID provided in URL isn't valid") + // ErrIDMismatch is returned when User provided different ID values in + // URL and JSON. + ErrIDMismatch = errors.New("request ID set in JSON doesn't match ID from URL") ) // isNotFoundError returns true if passed error is of type NotFoundError. diff --git a/server/api/v1/handlers.go b/server/api/v1/handlers.go index 954c389..54b6fbb 100644 --- a/server/api/v1/handlers.go +++ b/server/api/v1/handlers.go @@ -65,7 +65,26 @@ func (api *API) closeRequestHandler(r *http.Request, ps map[string]string) respo // request and calls appropriate methods: SetRequestValidAfter(), // SetRequestDeadline() and SetRequestPriority(). func (api *API) updateRequestHandler(r *http.Request, ps map[string]string) responseData { - return newServerError(ErrNotImplemented, "update request") + defer r.Body.Close() + + reqid, err := parseReqID(ps["id"]) + if err != nil { + return newServerError(ErrBadID) + } + + var req ReqInfo + if err = json.NewDecoder(r.Body).Decode(&req); err != nil { + return newServerError(err) + } + + if req.ID != 0 && req.ID != reqid { + return newServerError(ErrIDMismatch) + } + // When ID wasn't set in JSON (or was set to 0) then it should be set to + // the value from URL. + req.ID = reqid + + return newServerError(api.reqs.UpdateRequest(&req)) } // getRequestInfoHandler parses HTTP request for getting information about Boruta diff --git a/server/api/v1/handlers_test.go b/server/api/v1/handlers_test.go index 70871c9..3730358 100644 --- a/server/api/v1/handlers_test.go +++ b/server/api/v1/handlers_test.go @@ -110,15 +110,73 @@ func TestUpdateRequestHandler(t *testing.T) { assert, m, api := initTest(t) defer m.finish() + methods := []string{http.MethodPost} + path := "/api/v1/reqs/" + setPrioJSON := `{"Priority":4}` + prefix := "update-req-" + + valid := &ReqInfo{ + ID: ReqID(1), + Priority: Priority(4), + } + missing := &ReqInfo{ + ID: ReqID(2), + Priority: Priority(4), + } + m.rq.EXPECT().UpdateRequest(valid).Return(nil).Times(2) + malformedJSONTest := testFromTempl(malformedJSONTestTempl, prefix, path+"1", methods...) + invalidIDTest := testFromTempl(invalidIDTestTempl, prefix, path+invalidID, methods...) + notFoundTest := testFromTempl(notFoundTestTempl, prefix, path+"2", methods...) + notFoundTest.json = setPrioJSON + m.rq.EXPECT().UpdateRequest(missing).Return(NotFoundError("Request")) + + var req ReqInfo + var err error + req.Deadline, err = time.Parse(dateLayout, future) + assert.Nil(err) + req.ValidAfter, err = time.Parse(dateLayout, past) + assert.Nil(err) + tests := []requestTest{ { - name: "update-req", - path: "/api/v1/reqs/8", - methods: []string{http.MethodPost}, + // valid - only priority in JSON + name: prefix + "prio", + path: path + "1", + methods: methods, + json: setPrioJSON, + contentType: contentTypeJSON, + status: http.StatusNoContent, + }, + { + // valid - full JSON + name: prefix + "valid", + path: path + "1", + methods: methods, + json: string(jsonMustMarshal(valid)), + contentType: contentTypeJSON, + status: http.StatusNoContent, + }, + { + // no action + name: prefix + "empty", + path: path + "1", + methods: methods, json: ``, contentType: contentTypeJSON, - status: http.StatusNotImplemented, + status: http.StatusBadRequest, }, + { + // Request ID mismatch between URL and JSON + name: prefix + "mismatch", + path: path + "2", + methods: methods, + json: string(jsonMustMarshal(valid)), + contentType: contentTypeJSON, + status: http.StatusBadRequest, + }, + malformedJSONTest, + invalidIDTest, + notFoundTest, } runTests(assert, api, tests) diff --git a/server/api/v1/testdata/update-req-POST.json b/server/api/v1/testdata/update-req-POST.json deleted file mode 100644 index dba72fb..0000000 --- a/server/api/v1/testdata/update-req-POST.json +++ /dev/null @@ -1 +0,0 @@ -{"error":"not implemented yet: update request"} \ No newline at end of file diff --git a/server/api/v1/testdata/update-req-bad-id-POST.json b/server/api/v1/testdata/update-req-bad-id-POST.json new file mode 100644 index 0000000..9a1b539 --- /dev/null +++ b/server/api/v1/testdata/update-req-bad-id-POST.json @@ -0,0 +1 @@ +{"error":"invalid request: ID provided in URL isn't valid"} \ No newline at end of file diff --git a/server/api/v1/testdata/update-req-empty-POST.json b/server/api/v1/testdata/update-req-empty-POST.json new file mode 100644 index 0000000..6bf6e01 --- /dev/null +++ b/server/api/v1/testdata/update-req-empty-POST.json @@ -0,0 +1 @@ +{"error":"no body provided in HTTP request"} \ No newline at end of file diff --git a/server/api/v1/testdata/update-req-malformed-json-POST.json b/server/api/v1/testdata/update-req-malformed-json-POST.json new file mode 100644 index 0000000..c59dde1 --- /dev/null +++ b/server/api/v1/testdata/update-req-malformed-json-POST.json @@ -0,0 +1 @@ +{"error":"invalid request: unexpected EOF"} \ No newline at end of file diff --git a/server/api/v1/testdata/update-req-mismatch-POST.json b/server/api/v1/testdata/update-req-mismatch-POST.json new file mode 100644 index 0000000..7c2784c --- /dev/null +++ b/server/api/v1/testdata/update-req-mismatch-POST.json @@ -0,0 +1 @@ +{"error":"invalid request: request ID set in JSON doesn't match ID from URL"} \ No newline at end of file diff --git a/server/api/v1/testdata/update-req-missing-POST.json b/server/api/v1/testdata/update-req-missing-POST.json new file mode 100644 index 0000000..bb60840 --- /dev/null +++ b/server/api/v1/testdata/update-req-missing-POST.json @@ -0,0 +1 @@ +{"error":"Request not found"} \ No newline at end of file diff --git a/server/api/v1/testdata/update-req-prio-POST.json b/server/api/v1/testdata/update-req-prio-POST.json new file mode 100644 index 0000000..e69de29 diff --git a/server/api/v1/testdata/update-req-valid-POST.json b/server/api/v1/testdata/update-req-valid-POST.json new file mode 100644 index 0000000..e69de29