-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathuser_test.go
89 lines (74 loc) · 2.07 KB
/
user_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
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package user
import (
"context"
"testing"
"time"
"github.com/drone/drone/core"
"github.com/drone/drone/mock/mockscm"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/golang/mock/gomock"
)
var noContext = context.Background()
func TestFind(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
checkToken := func(ctx context.Context) {
got, ok := ctx.Value(scm.TokenKey{}).(*scm.Token)
if !ok {
t.Errorf("Expect token stored in context")
return
}
want := &scm.Token{
Token: "755bb80e5b",
Refresh: "e08f3fa43e",
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}
now := time.Now()
mockUser := &scm.User{
Login: "octocat",
Email: "octocat@github.com",
Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
Created: now,
Updated: now,
}
mockUsers := mockscm.NewMockUserService(controller)
mockUsers.EXPECT().Find(gomock.Any()).Do(checkToken).Return(mockUser, nil, nil)
client := new(scm.Client)
client.Users = mockUsers
want := &core.User{
Login: "octocat",
Email: "octocat@github.com",
Avatar: "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
Created: now.Unix(),
Updated: now.Unix(),
}
got, err := New(client, nil).Find(noContext, "755bb80e5b", "e08f3fa43e")
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}
func TestFind_Error(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUsers := mockscm.NewMockUserService(controller)
mockUsers.EXPECT().Find(gomock.Any()).Return(nil, nil, scm.ErrNotFound)
client := new(scm.Client)
client.Users = mockUsers
got, err := New(client, nil).Find(noContext, "755bb80e5b", "e08f3fa43e")
if err == nil {
t.Errorf("Expect error finding user")
}
if got != nil {
t.Errorf("Expect nil user on error")
}
}