Skip to content

Commit

Permalink
Locales unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhadvig committed May 3, 2021
1 parent c1596f2 commit 98d3e38
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
4 changes: 4 additions & 0 deletions pkg/server/errorpage/errorpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"k8s.io/klog/v2"

"github.com/openshift/library-go/pkg/apiserver/httprequest"

"github.com/openshift/oauth-server/pkg/server/locales"
)

// ErrorPage implements auth and grant error handling by rendering an error page for browser-like clients
Expand Down Expand Up @@ -56,6 +58,7 @@ func (p *ErrorPage) GrantError(err error, w http.ResponseWriter, req *http.Reque
type ErrorData struct {
Error string
ErrorCode string
Locale map[string]string
}

// ErrorPageRenderer handles rendering a given error code/message
Expand Down Expand Up @@ -88,6 +91,7 @@ func NewErrorPageTemplateRenderer(templateFile string) (ErrorPageRenderer, error
func (r *errorPageTemplateRenderer) Render(data ErrorData, w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusOK)
data.Locale = locales.GetLocale(req.Header.Get("Accept-Language"))
if err := r.errorPageTemplate.Execute(w, data); err != nil {
utilruntime.HandleError(fmt.Errorf("unable to render error page template: %v", err))
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/server/login/locales.go → pkg/server/locales/locales.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package login
package locales

import "golang.org/x/text/language"

func getLocale(langBase string) map[string]string {
func GetLocale(acceptLangHeader string) map[string]string {
langBase := getPreferredLang(acceptLangHeader)
switch langBase {
case language.English.String():
return locale_en
Expand Down
47 changes: 47 additions & 0 deletions pkg/server/locales/locales_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package locales

import (
"reflect"
"testing"
)

func TestLocales(t *testing.T) {
tests := []struct {
name string
header string
locale map[string]string
}{
{
name: "Test empty 'Accept-Language' request header which defaults to English language",
header: "",
locale: locale_en,
},
{
name: "Test 'Accept-Language' request header which favours English language",
header: "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
locale: locale_en,
},
{
name: "Test 'Accept-Language' request header which favours Japan language",
header: "ja;q=0.8, en;q=0.7",
locale: locale_ja,
},
{
name: "Test 'Accept-Language' request header which favours Korean language",
header: "ja;q=0.8, ko;q=0.9",
locale: locale_ko,
},
{
name: "Test 'Accept-Language' request header which favours Chinese language",
header: "en;q=0.3, zh;q=0.7",
locale: locale_zh,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !reflect.DeepEqual(GetLocale(tt.header), tt.locale) {
t.Error(tt.name)
}
})
}
}
9 changes: 2 additions & 7 deletions pkg/server/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
metrics "github.com/openshift/oauth-server/pkg/prometheus"
"github.com/openshift/oauth-server/pkg/server/csrf"
"github.com/openshift/oauth-server/pkg/server/errorpage"
"github.com/openshift/oauth-server/pkg/server/locales"
"github.com/openshift/oauth-server/pkg/server/redirect"
)

Expand Down Expand Up @@ -130,13 +131,7 @@ func (l *Login) handleLoginForm(w http.ResponseWriter, req *http.Request) {
return
}

languageQuery := req.URL.Query().Get("lng")
if languageQuery != "" {
form.Locale = getLocale(languageQuery)
} else {
form.Locale = getLocale(getPreferredLang(req.Header.Get("Accept-Language")))
}

form.Locale = locales.GetLocale(req.Header.Get("Accept-Language"))
form.ErrorCode = req.URL.Query().Get(reasonParam)
if len(form.ErrorCode) > 0 {
if msg, hasMsg := errorMessages[form.ErrorCode]; hasMsg {
Expand Down
5 changes: 4 additions & 1 deletion pkg/server/selectprovider/selectprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/openshift/oauth-server/pkg/api"
"github.com/openshift/oauth-server/pkg/oauth/handlers"
"github.com/openshift/oauth-server/pkg/server/locales"
)

type SelectProviderRenderer interface {
Expand All @@ -31,6 +32,7 @@ func NewSelectProvider(render SelectProviderRenderer, forceInterstitial bool) ha

type ProviderData struct {
Providers []api.ProviderInfo
Locale map[string]string
}

// NewSelectProviderRenderer creates a select provider renderer that takes in an optional custom template to
Expand Down Expand Up @@ -108,7 +110,8 @@ type selectProviderTemplateRenderer struct {
func (r selectProviderTemplateRenderer) Render(providers []api.ProviderInfo, w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := r.selectProviderTemplate.Execute(w, ProviderData{Providers: providers}); err != nil {
locale := locales.GetLocale(req.Header.Get("Accept-Language"))
if err := r.selectProviderTemplate.Execute(w, ProviderData{Providers: providers, Locale: locale}); err != nil {
utilruntime.HandleError(fmt.Errorf("unable to render select provider template: %v", err))
}
}
3 changes: 2 additions & 1 deletion pkg/server/selectprovider/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ body { background-color: var(--pf-global--BackgroundColor--dark-100); }
{{ else }}
<h1 class="pf-c-title pf-m-3xl">{{ .Locale.LogInWith }}</h1>
<ul>
{{ $logInWith := .Locale.LogInWith }}
{{ range $provider := .Providers }}
<li class="idp">
<a href="{{$provider.URL}}" class="pf-c-button pf-m-secondary pf-m-block" title="{{ .Locale.LogInWith }} {{$provider.Name}}">{{$provider.Name}}</a>
<a href="{{$provider.URL}}" class="pf-c-button pf-m-secondary pf-m-block" title="{{ $logInWith }} {{$provider.Name}}">{{$provider.Name}}</a>
</li>
{{ end }}
</ul>
Expand Down

0 comments on commit 98d3e38

Please sign in to comment.