* tests/http.scm (http-post-json, mr-json-open, mr-json-close mr-spec): New variables. New tests for Gitlab API. --- tests/http.scm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
diff --git a/tests/http.scm b/tests/http.scm index a124761..3281bcc 100644 --- a/tests/http.scm +++ b/tests/http.scm @@ -21,6 +21,7 @@ (use-modules (cuirass http) (cuirass database) + (cuirass gitlab) (cuirass specification) (cuirass utils) (tests common) @@ -41,6 +42,9 @@ (call-with-values (lambda () (http-get uri)) (lambda (response body) body))) +(define (http-post-json uri body) + (http-post uri #:body body #:headers '((content-type application/json)))) + (define (wait-until-ready port) ;; Wait until the server is accepting connections. (let ((conn (socket PF_INET SOCK_STREAM 0))) @@ -85,6 +89,17 @@ (channel . "packages") (directory . "dir2"))))))) +(define mr-json-open + "{\"event_type\":\"merge_request\",\"object_attributes\":{\"action\":\"open\",\"merge_status\":\"can_be_merged\",\"source_branch\":\"test-branch\",\"source\":{\"git_http_url\":\"https://gitlab.instance.test/source-repo/fork-name.git\",\"name\":\"test-project\"}}}") + +(define mr-json-close + "{\"event_type\":\"merge_request\",\"object_attributes\":{\"action\":\"close\",\"merge_status\":\"can_be_merged\",\"source_branch\":\"test-branch\",\"source\":{\"git_http_url\":\"https://gitlab.instance.test/source-repo/fork-name.git\",\"name\":\"test-project\"}}}") + +(define mr-spec + (gitlab-merge-request->specification + (gitlab-event-value + (pk 'json (json->gitlab-event mr-json-open))))) + (test-group-with-cleanup "http" (test-assert "db-init" (begin @@ -323,6 +338,25 @@ (http-get-body (test-cuirass-uri "/api/jobs/history?spec=guix&names=fake-job&nr=10")))))) + (test-equal "/admin/gitlab/event creates a spec from a new merge request" + (specification-name mr-spec) + (begin + (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-open) + (specification-name (db-get-specification (specification-name mr-spec))))) + + (test-equal "/admin/gitlab/event error when a merge request has already been created" + 400 + (response-code (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-open))) + + (test-assert "/admin/gitlab/event removes a spec from a closed merge request" + (begin + (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-close) + (not (db-get-specification (specification-name mr-spec))))) + + (test-equal "/admin/gitlab/event error when a merge request has already been closed" + 404 + (response-code (http-post-json (test-cuirass-uri "/admin/gitlab/event") mr-json-close))) + (test-assert "db-close" (begin (db-close (%db)) -- 2.45.1