Skip to content

Commit

Permalink
Added tests for creating and deleting job-specific tags
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Aug 17, 2023
1 parent fdce796 commit 21034f1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
21 changes: 14 additions & 7 deletions server/controllers/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
65 changes: 63 additions & 2 deletions server/controllers/job_metadata_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions server/views/job/new_tag.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

<div class="bottom-buttons">
<div class="pull-right">
Expand Down

0 comments on commit 21034f1

Please sign in to comment.