test language picking

This commit is contained in:
cblgh 2021-04-19 17:56:59 +02:00
parent 8d1f790f57
commit 0574c9a235
3 changed files with 130 additions and 1 deletions

View File

@ -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")
}

View File

@ -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")
}

View File

@ -60,7 +60,7 @@
</p>
<h3 class="text-gray-400 text-sm font-bold mb-2">{{ i18n "SetDefaultLanguageTitle" }}</h3>
<details class="mb-8 self-start max-w-sm">
<summary class="px-3 py-1 max-w-sm rounded shadow bg-white ring-1 ring-gray-300 hover:bg-gray-100 cursor-pointer">
<summary id="language-summary" class="px-3 py-1 max-w-sm rounded shadow bg-white ring-1 ring-gray-300 hover:bg-gray-100 cursor-pointer">
{{ $.CurrentLanguage }}
</summary>
<div class="mt-2 bg-gray bg-white px-3 rounded ring-1 ring-gray-300">