-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserauth_example_test.go
More file actions
58 lines (49 loc) · 1.29 KB
/
userauth_example_test.go
File metadata and controls
58 lines (49 loc) · 1.29 KB
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
package userauth_test
import (
"errors"
"fmt"
"github.com/go-bumbu/userauth"
"github.com/go-bumbu/userauth/userstore/staticusers"
)
func Example_usrauth_CanLogin() {
// define static UserGetter
u := []staticusers.User{
{
Id: "demo",
HashPw: userauth.MustHashPw("demo"),
Enabled: false,
},
{
Id: "admin",
HashPw: userauth.MustHashPw("admin"),
Enabled: true,
},
}
// use static UserGetter as mock user provider,
// UserGetter can also be loaded from files with static.FromFile("my-file.yaml|json")
users := staticusers.Users{Users: u}
// create a login handler that will check user login
loginHandler := userauth.LoginHandler{
UserStore: &users,
}
// check if the user demo (from file) can log in
isOK, _ := loginHandler.CanLogin("admin", "admin")
fmt.Printf("user admin can login: %v\n", isOK)
// check if the user demo can't log in since the account is disabled
isOK, err := loginHandler.CanLogin("demo", "demo")
switch {
case errors.Is(err, userauth.ErrUserNotFound), errors.Is(err, userauth.ErrUserDisabled):
// expected errors
default:
panicOnErr(err)
}
fmt.Printf("user demo can login: %v", isOK)
// Output:
// user admin can login: true
// user demo can login: false
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}