-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
67 lines (59 loc) · 1.65 KB
/
test_helpers.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
package pkg
import (
"testing"
"time"
"github.com/ory/fosite/handler/oauth2"
"github.com/ory/fosite/storage"
"github.com/ory/fosite/token/hmac"
"github.com/ory/ladon"
"github.com/ory/ladon/manager/memory"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var HMACStrategy = &oauth2.HMACSHAStrategy{
Enigma: &hmac.HMACStrategy{
GlobalSecret: []byte("1234567890123456789012345678901234567890"),
},
AccessTokenLifespan: time.Hour,
AuthorizeCodeLifespan: time.Hour,
}
func RequireError(t *testing.T, expectError bool, err error, args ...interface{}) {
if err != nil && !expectError {
t.Logf("Unexpected error: %s\n", err.Error())
t.Logf("Arguments: %v\n", args)
if e, ok := errors.Cause(err).(stackTracer); ok {
t.Logf("Stack:\n%+v\n", e.StackTrace())
}
t.Logf("\n\n")
}
require.Equal(t, expectError, err != nil, "%v", args)
}
func AssertError(t *testing.T, expectError bool, err error, args ...interface{}) {
assert.Equal(t, expectError, err != nil, "%v", args)
if err != nil && !expectError {
t.Logf("Unexpected error: %s\n", err.Error())
t.Logf("Arguments: %s\n", args)
if e, ok := errors.Cause(err).(stackTracer); ok {
t.Logf("Stack:\n%+v\n", e.StackTrace())
}
t.Logf("\n\n")
}
}
func LadonWarden(ps map[string]ladon.Policy) ladon.Warden {
return &ladon.Ladon{
Manager: &memory.MemoryManager{
Policies: ps,
},
}
}
func FositeStore() *storage.MemoryStore {
return storage.NewMemoryStore()
}
func Tokens(length int) (res [][]string) {
for i := 0; i < length; i++ {
tok, sig, _ := HMACStrategy.Enigma.Generate()
res = append(res, []string{sig, tok})
}
return res
}