From 21034f1feeefe6c54876ae45ef5ac7787d2b6c1c Mon Sep 17 00:00:00 2001 From: "A. Diamond" Date: Thu, 17 Aug 2023 16:47:16 -0400 Subject: [PATCH] Added tests for creating and deleting job-specific tags --- server/controllers/controllers_test.go | 21 ++++-- .../job_metadata_controller_test.go | 65 ++++++++++++++++++- server/views/job/new_tag.html | 3 + 3 files changed, 80 insertions(+), 9 deletions(-) diff --git a/server/controllers/controllers_test.go b/server/controllers/controllers_test.go index d7e333d..df36435 100644 --- a/server/controllers/controllers_test.go +++ b/server/controllers/controllers_test.go @@ -49,17 +49,15 @@ func NewPostRequest(endpointUrl string, params url.Values) (*http.Request, error return req, err } -func DoSimpleGetTest(t *testing.T, endpointUrl string, expected []string) { +func GetUrl(t *testing.T, endpointUrl string) string { w := httptest.NewRecorder() req, _ := http.NewRequest(http.MethodGet, endpointUrl, nil) dartServer.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code) - html := w.Body.String() - ok, notFound := AssertContainsAllStrings(html, expected) - assert.True(t, ok, "Missing from page %s: %v", endpointUrl, notFound) + return w.Body.String() } -func DoSimplePostTest(t *testing.T, settings PostTestSettings) { +func PostUrl(t *testing.T, settings PostTestSettings) string { w := httptest.NewRecorder() req, err := NewPostRequest(settings.EndpointUrl, settings.Params) require.Nil(t, err) @@ -68,9 +66,18 @@ func DoSimplePostTest(t *testing.T, settings PostTestSettings) { if settings.ExpectedRedirectLocation != "" { assert.Equal(t, settings.ExpectedRedirectLocation, w.Header().Get("Location")) } + return w.Body.String() +} + +func DoSimpleGetTest(t *testing.T, endpointUrl string, expected []string) { + html := GetUrl(t, endpointUrl) + ok, notFound := AssertContainsAllStrings(html, expected) + assert.True(t, ok, "Missing from page %s: %v", endpointUrl, notFound) +} + +func DoSimplePostTest(t *testing.T, settings PostTestSettings) { + html := PostUrl(t, settings) if len(settings.ExpectedContent) > 0 { - html := w.Body.String() - //fmt.Println(html) ok, notFound := AssertContainsAllStrings(html, settings.ExpectedContent) assert.True(t, ok, "Missing from page %s: %v", settings.EndpointUrl, notFound) } diff --git a/server/controllers/job_metadata_controller_test.go b/server/controllers/job_metadata_controller_test.go index 718846b..e0829b3 100644 --- a/server/controllers/job_metadata_controller_test.go +++ b/server/controllers/job_metadata_controller_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/APTrust/dart-runner/core" + "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -68,10 +69,70 @@ func TestJobSaveMetadata(t *testing.T) { } } -func TestJobSaveTag(t *testing.T) { +func TestJobAddTag(t *testing.T) { + defer core.ClearDartTable() + job := loadTestJob(t) + assert.NoError(t, core.ObjSave(job)) + expected := []string{ + `id="TagDefinition_TagFile"`, + `id="TagDefinition_TagName"`, + `id="TagDefinition_UserValue"`, + `id="TagDefinition_ID"`, + } + + jobUrl := fmt.Sprintf("/jobs/add_tag/%s", job.ID) + DoSimpleGetTest(t, jobUrl, expected) } -func TestJobDeleteTag(t *testing.T) { +func TestJobSaveAndDeleteTag(t *testing.T) { + defer core.ClearDartTable() + job := loadTestJob(t) + assert.NoError(t, core.ObjSave(job)) + + newTagID := uuid.NewString() + newTagValue := "1234-5678" + + expected := []string{ + newTagID, + newTagValue, + "custom.txt", + } + params := url.Values{} + params.Add("ID", newTagID) + params.Add("TagFile", "custom.txt") + params.Add("TagName", "Test-Tag") + params.Add("IsBuiltIn", "false") + params.Add("IsUserAddedTag", "true") + params.Add("WasAddedForJob", "true") + params.Add("UserValue", newTagValue) + + postTestSettings := PostTestSettings{ + EndpointUrl: fmt.Sprintf("/jobs/add_tag/%s", job.ID), + Params: params, + ExpectedResponseCode: http.StatusOK, + } + DoSimplePostTest(t, postTestSettings) + + // Now reload the job metadata page and make sure + // our new tag is there. + jobMetadataUrl := fmt.Sprintf("/jobs/metadata/%s", job.ID) + DoSimpleGetTest(t, jobMetadataUrl, expected) + + // Now let's delete that new tag. + params = url.Values{} + params.Set("tagId", newTagID) + postTestSettings = PostTestSettings{ + EndpointUrl: fmt.Sprintf("/jobs/delete_tag/%s", job.ID), + Params: params, + ExpectedResponseCode: http.StatusOK, + } + DoSimplePostTest(t, postTestSettings) + + // Make sure the tag was deleted + html := GetUrl(t, jobMetadataUrl) + for _, str := range expected { + assert.NotContains(t, html, str, str) + } } diff --git a/server/views/job/new_tag.html b/server/views/job/new_tag.html index b9b30a1..51785ff 100644 --- a/server/views/job/new_tag.html +++ b/server/views/job/new_tag.html @@ -9,6 +9,9 @@ {{ template "partials/input_text.html" dict "field" .form.Fields.UserValue }} {{ template "partials/input_hidden.html" dict "field" .form.Fields.ID }} + {{ template "partials/input_hidden.html" dict "field" .form.Fields.IsBuiltIn }} + {{ template "partials/input_hidden.html" dict "field" .form.Fields.IsUserAddedTag }} + {{ template "partials/input_hidden.html" dict "field" .form.Fields.WasAddedForJob }}