-
Notifications
You must be signed in to change notification settings - Fork 0
/
unauthenticated_test.go
63 lines (48 loc) · 1.45 KB
/
unauthenticated_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
package zerrors
import (
"errors"
"github.com/samber/oops"
"testing"
"github.com/stretchr/testify/assert"
)
func TestToUnauthenticatedOnNil(t *testing.T) {
err := ToUnauthenticated(nil, "id")
ok := IsUnauthenticated(err)
assert.False(t, ok)
assert.Equal(t, err, nil)
}
func TestToUnauthenticated(t *testing.T) {
err := ToUnauthenticated(errors.New("not domain"), "id")
assert.True(t, IsUnauthenticated(err))
var e oops.OopsError
ok := errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnauthenticated, e.Code())
}
func TestIsUnauthenticatedOnNonDomain(t *testing.T) {
err := errors.New("I am found!")
ok := IsUnauthenticated(err)
assert.False(t, ok)
}
func TestThrowUnauthenticated(t *testing.T) {
err := ThrowUnauthenticated("create", "user", "sam")
ok := IsUnauthenticated(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "cannot create 'sam' of kind 'user'")
}
func TestUnauthenticatedf(t *testing.T) {
err := Unauthenticatedf("something happened: %s", "reason")
ok := IsUnauthenticated(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "something happened: reason")
}
func TestToUnauthenticatedf(t *testing.T) {
err := ToUnauthenticatedf(errors.New("something happened"), "key: %s", "value")
ok := IsUnauthenticated(err)
assert.True(t, ok)
var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrUnauthenticated, e.Code())
assert.Equal(t, err.Error(), "key: value: something happened")
}