Skip to content

Commit

Permalink
fix review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cblgh committed Mar 16, 2021
1 parent 991df6c commit 6cd0974
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions web/handlers/admin/allow_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestAllowListAdd(t *testing.T) {
a.Equal(addURL.String(), action)

webassert.ElementsInForm(t, formSelection, []webassert.FormElement{
{Tag: "input", Name: "pub_key", Type: "text"},
{Name: "pub_key", Type: "text"},
})

newKey := "@x7iOLUcq3o+sjGeAnipvWeGzfuYgrXl8L4LYlxIhwDc=.ed25519"
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestAllowListRemoveConfirmation(t *testing.T) {
a.Equal(addURL.String(), action)

webassert.ElementsInForm(t, form, []webassert.FormElement{
{Tag: "input", Name: "id", Type: "hidden", Value: "666"},
{Name: "id", Type: "hidden", Value: "666"},
})
}

Expand Down
14 changes: 8 additions & 6 deletions web/handlers/admin/notices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNoticeSaveActuallyCalled(t *testing.T) {
u := urlTo(router.AdminNoticeSave)
formValues := url.Values{"id": id, "title": title, "content": content, "language": language}
resp := ts.Client.PostForm(u.String(), formValues)
a.NotEqual(http.StatusInternalServerError, resp.Code)
a.Equal(http.StatusSeeOther, resp.Code, "POST should work")
a.Equal(1, ts.NoticeDB.SaveCallCount(), "noticedb should have saved after POST completed")
}

Expand Down Expand Up @@ -114,9 +114,10 @@ func TestNoticeDraftLanguageIncludesAllFields(t *testing.T) {
html, resp := ts.Client.GetHTML(u.String())
form := html.Find("form")
a.Equal(http.StatusOK, resp.Code, "Wrong HTTP status code")
// FormElement defaults to input if tag omitted
webassert.ElementsInForm(t, form, []webassert.FormElement{
{Tag: "input", Name: "title"},
{Tag: "input", Name: "language"},
{Name: "title"},
{Name: "language"},
{Tag: "textarea", Name: "content"},
})
}
Expand All @@ -142,10 +143,11 @@ func TestNoticeEditFormIncludesAllFields(t *testing.T) {

a.Equal(http.StatusOK, resp.Code, "Wrong HTTP status code")
// check for all the form elements & verify their initial contents are set correctly
// FormElement defaults to input if tag omitted
webassert.ElementsInForm(t, form, []webassert.FormElement{
{Tag: "input", Name: "title", Value: notice.Title},
{Tag: "input", Name: "language", Value: notice.Language},
{Tag: "input", Name: "id", Value: fmt.Sprintf("%d", notice.ID), Type: "hidden"},
{Name: "title", Value: notice.Title},
{Name: "language", Value: notice.Language},
{Name: "id", Value: fmt.Sprintf("%d", notice.ID), Type: "hidden"},
{Tag: "textarea", Name: "content"},
})
}
2 changes: 2 additions & 0 deletions web/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func NewURLTo(appRouter *mux.Router) func(string, ...interface{}) *url.URL {
return func(routeName string, ps ...interface{}) *url.URL {
route := appRouter.Get(routeName)
if route == nil {
// TODO: https://github.com/ssb-ngi-pointer/go-ssb-room/issues/35 for a
// for reference, see https://github.com/ssb-ngi-pointer/go-ssb-room/pull/64
// level.Warn(l).Log("msg", "no such route", "route", routeName, "params", fmt.Sprintf("%v", ps))
return &url.URL{}
}
Expand Down
14 changes: 9 additions & 5 deletions web/webassert/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,31 @@ type FormElement struct {
func ElementsInForm(t *testing.T, form *goquery.Selection, elems []FormElement) {
a := assert.New(t)
for _, e := range elems {
// empty Tag defaults to <input>
if e.Tag == "" {
e.Tag = "input"
}

inputSelector := form.Find(fmt.Sprintf("%s[name=%s]", e.Tag, e.Name))
ok := a.Equal(1, inputSelector.Length(), "expected to find input with name %s", e.Name)
elementSelector := form.Find(fmt.Sprintf("%s[name=%s]", e.Tag, e.Name))
ok := a.Equal(1, elementSelector.Length(), "expected to find element with name %s", e.Name)
if !ok {
continue
}

if e.Value != "" {
value, has := inputSelector.Attr("value")
value, has := elementSelector.Attr("value")
a.True(has, "expected value attribute input[name=%s]", e.Name)
a.Equal(e.Value, value, "wrong value attribute on input[name=%s]", e.Name)
}

if e.Type != "" {
tipe, has := inputSelector.Attr("type")
tipe, has := elementSelector.Attr("type")
a.True(has, "expected type attribute input[name=%s]", e.Name)
a.Equal(e.Type, tipe, "wrong type attribute on input[name=%s]", e.Name)
}

if e.Placeholder != "" {
tipe, has := inputSelector.Attr("placeholder")
tipe, has := elementSelector.Attr("placeholder")
a.True(has, "expected placeholder attribute input[name=%s]", e.Name)
a.Equal(e.Placeholder, tipe, "wrong placeholder attribute on input[name=%s]", e.Name)
}
Expand Down

0 comments on commit 6cd0974

Please sign in to comment.