-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package admin | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ssb-ngi-pointer/go-ssb-room/web/router" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
||
/* can't test English atm due to web/i18n/i18ntesting/testing.go:justTheKeys, which generates translations that are just | ||
* translationLabel = "translationLabel" | ||
*/ | ||
// func TestLanguageSetDefaultLanguageEnglish(t *testing.T) { | ||
// ts := newSession(t) | ||
// a := assert.New(t) | ||
// | ||
// ts.ConfigDB.GetDefaultLanguageReturns("en", nil) | ||
// | ||
// u := ts.URLTo(router.AdminSettings) | ||
// html, resp := ts.Client.GetHTML(u) | ||
// a.Equal(http.StatusOK, resp.Code, "Wrong HTTP status code") | ||
// | ||
// fmt.Println(html.Html()) | ||
// summaryElement := html.Find("#language-summary") | ||
// summaryText := strings.TrimSpace(summaryElement.Text()) | ||
// a.Equal("English", summaryText, "summary language should display english translation of language name") | ||
// } | ||
|
||
func TestLanguageSetDefaultLanguage(t *testing.T) { | ||
ts := newSession(t) | ||
a := assert.New(t) | ||
|
||
ts.ConfigDB.GetDefaultLanguageReturns("de", nil) | ||
|
||
u := ts.URLTo(router.AdminSettings) | ||
html, resp := ts.Client.GetHTML(u) | ||
a.Equal(http.StatusOK, resp.Code, "Wrong HTTP status code") | ||
|
||
summaryElement := html.Find("#language-summary") | ||
summaryText := strings.TrimSpace(summaryElement.Text()) | ||
a.Equal("Deutsch", summaryText, "summary language should display german translation of language name") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
// "github.com/ssb-ngi-pointer/go-ssb-room/roomdb" | ||
"github.com/ssb-ngi-pointer/go-ssb-room/web/i18n" | ||
"github.com/ssb-ngi-pointer/go-ssb-room/web/router" | ||
) | ||
|
||
func TestLanguageDefaultNoCookie(t *testing.T) { | ||
ts := setup(t) | ||
a := assert.New(t) | ||
route := ts.URLTo(router.CompleteIndex) | ||
postEndpoint := ts.URLTo(router.CompleteSetLanguage) | ||
formAction := postEndpoint.Path | ||
|
||
html, res := ts.Client.GetHTML(route) | ||
a.Equal(http.StatusOK, res.Code, "wrong HTTP status code") | ||
|
||
languageForms := html.Find(fmt.Sprintf(`form[action="%s"]`, formAction)) | ||
// two languages: english, deutsch => two <form> elements | ||
a.Equal(2, languageForms.Length()) | ||
|
||
// verify there is no language cookie to set yet | ||
cookieHeader := res.Header()["Set-Cookie"] | ||
for _, cookie := range cookieHeader { | ||
cookieName := strings.Split(cookie, "=")[0] | ||
a.NotEqual(cookieName, i18n.LanguageCookieName) | ||
} | ||
} | ||
|
||
func TestLanguageChooseGerman(t *testing.T) { | ||
ts := setup(t) | ||
a := assert.New(t) | ||
route := ts.URLTo(router.CompleteIndex) | ||
postEndpoint := ts.URLTo(router.CompleteSetLanguage) | ||
formAction := postEndpoint.Path | ||
|
||
html, res := ts.Client.GetHTML(route) | ||
a.Equal(http.StatusOK, res.Code, "wrong HTTP status code") | ||
|
||
csrfTokenElem := html.Find(fmt.Sprintf(`form[action="%s"] input[type="hidden"]`, formAction)) | ||
a.Equal(6, csrfTokenElem.Length()) | ||
|
||
csrfName, has := csrfTokenElem.First().Attr("name") | ||
a.True(has, "should have a name attribute") | ||
|
||
csrfValue, has := csrfTokenElem.First().Attr("value") | ||
a.True(has, "should have value attribute") | ||
|
||
// construct the post request fields, simulating picking a language | ||
setLanguageFields := url.Values{ | ||
"lang": []string{"de"}, | ||
"page": []string{"/"}, | ||
csrfName: []string{csrfValue}, | ||
} | ||
|
||
// set the referer header (important! otherwise our nicely crafted request yields a 500 :'() | ||
var refererHeader = make(http.Header) | ||
refererHeader.Set("Referer", "https://localhost") | ||
ts.Client.SetHeaders(refererHeader) | ||
|
||
// send the post request | ||
postRes := ts.Client.PostForm(postEndpoint, setLanguageFields) | ||
a.Equal(http.StatusSeeOther, postRes.Code, "wrong HTTP status code for sign in") | ||
|
||
// verify there is one language cookie to set | ||
cookieHeader := postRes.Header()["Set-Cookie"] | ||
var languageCookies int | ||
for _, cookie := range cookieHeader { | ||
cookieName := strings.Split(cookie, "=")[0] | ||
if cookieName == i18n.LanguageCookieName { | ||
languageCookies += 1 | ||
} | ||
} | ||
a.Equal(1, languageCookies, "should have one language cookie set after posting") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters