forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
101 lines (90 loc) · 2.45 KB
/
session_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
92
93
94
95
96
97
98
99
100
101
package http_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"go.uber.org/zap"
platform "github.com/influxdata/influxdb"
platformhttp "github.com/influxdata/influxdb/http"
"github.com/influxdata/influxdb/mock"
)
// NewMockSessionBackend returns a SessionBackend with mock services.
func NewMockSessionBackend() *platformhttp.SessionBackend {
return &platformhttp.SessionBackend{
Logger: zap.NewNop().With(zap.String("handler", "session")),
SessionService: mock.NewSessionService(),
PasswordsService: mock.NewPasswordsService("", ""),
}
}
func TestSessionHandler_handleSignin(t *testing.T) {
type fields struct {
PasswordsService platform.PasswordsService
SessionService platform.SessionService
}
type args struct {
user string
password string
}
type wants struct {
cookie string
code int
}
tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "successful compare password",
fields: fields{
SessionService: &mock.SessionService{
CreateSessionFn: func(context.Context, string) (*platform.Session, error) {
return &platform.Session{
ID: platform.ID(0),
Key: "abc123xyz",
CreatedAt: time.Date(2018, 9, 26, 0, 0, 0, 0, time.UTC),
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
UserID: platform.ID(1),
}, nil
},
},
PasswordsService: &mock.PasswordsService{
ComparePasswordFn: func(context.Context, string, string) error {
return nil
},
},
},
args: args{
user: "user1",
password: "supersecret",
},
wants: wants{
cookie: "session=abc123xyz",
code: http.StatusNoContent,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := NewMockSessionBackend()
b.PasswordsService = tt.fields.PasswordsService
b.SessionService = tt.fields.SessionService
h := platformhttp.NewSessionHandler(b)
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "http://localhost:9999/api/v2/signin", nil)
r.SetBasicAuth(tt.args.user, tt.args.password)
h.ServeHTTP(w, r)
if got, want := w.Code, tt.wants.code; got != want {
t.Errorf("bad status code: got %d want %d", got, want)
}
headers := w.Header()
cookie := headers.Get("Set-Cookie")
if got, want := cookie, tt.wants.cookie; got != want {
t.Errorf("expected session cookie to be set: got %q want %q", got, want)
}
})
}
}