forked from authelia/oauth2-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
91 lines (78 loc) · 3.23 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
"authelia.com/provider/oauth2/i18n"
"authelia.com/provider/oauth2/x/errorsx"
)
func TestRFC6749Error(t *testing.T) {
t.Run("case=wrap", func(t *testing.T) {
orig := errors.New("hi")
wrap := new(RFC6749Error)
wrap.Wrap(orig)
assert.EqualValues(t, orig.(errorsx.StackTracer).StackTrace(), wrap.StackTrace())
})
t.Run("case=wrap_self", func(t *testing.T) {
wrap := new(RFC6749Error)
wrap.Wrap(wrap)
assert.Empty(t, wrap.StackTrace())
})
}
func TestErrorI18N(t *testing.T) {
catalog := i18n.NewDefaultMessageCatalog([]*i18n.DefaultLocaleBundle{
{
LangTag: "en",
Messages: []*i18n.DefaultMessage{
{
ID: "access_denied",
FormattedMessage: "The resource owner or authorization server denied the request.",
},
{
ID: "badRequestMethod",
FormattedMessage: "HTTP method is '%s', expected 'POST'.",
},
},
},
{
LangTag: "es",
Messages: []*i18n.DefaultMessage{
{
ID: "access_denied",
FormattedMessage: "El propietario del recurso o el servidor de autorización denegó la solicitud.",
},
{
ID: "HTTP method is '%s', expected 'POST'.",
FormattedMessage: "El método HTTP es '%s', esperado 'POST'.",
},
{
ID: "Unable to parse HTTP body, make sure to send a properly formatted form request body.",
FormattedMessage: "No se puede analizar el cuerpo HTTP, asegúrese de enviar un cuerpo de solicitud de formulario con el formato adecuado.",
},
{
ID: "badRequestMethod",
FormattedMessage: "El método HTTP es '%s', esperado 'POST'.",
},
},
},
})
t.Run("case=legacy", func(t *testing.T) {
err := ErrAccessDenied.WithLocalizer(catalog, language.Spanish).WithHintf("HTTP method is '%s', expected 'POST'.", "GET")
assert.EqualValues(t, "El propietario del recurso o el servidor de autorización denegó la solicitud. El método HTTP es 'GET', esperado 'POST'.", err.GetDescription())
})
t.Run("case=unsupported_locale_legacy", func(t *testing.T) {
err := ErrAccessDenied.WithLocalizer(catalog, language.Afrikaans).WithHintf("HTTP method is '%s', expected 'POST'.", "GET")
assert.EqualValues(t, "The resource owner or authorization server denied the request. HTTP method is 'GET', expected 'POST'.", err.GetDescription())
})
t.Run("case=simple", func(t *testing.T) {
err := ErrAccessDenied.WithLocalizer(catalog, language.Spanish).WithHintIDOrDefaultf("badRequestMethod", "HTTP method is '%s', expected 'POST'.", "GET")
assert.EqualValues(t, "El propietario del recurso o el servidor de autorización denegó la solicitud. El método HTTP es 'GET', esperado 'POST'.", err.GetDescription())
})
t.Run("case=unsupported_locale", func(t *testing.T) {
err := ErrAccessDenied.WithLocalizer(catalog, language.Afrikaans).WithHintIDOrDefaultf("badRequestMethod", "HTTP method is '%s', expected 'POST'.", "GET")
assert.EqualValues(t, "The resource owner or authorization server denied the request. HTTP method is 'GET', expected 'POST'.", err.GetDescription())
})
}