forked from u5surf/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogout_test.go
100 lines (83 loc) · 1.82 KB
/
logout_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
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestLogout__noCookie(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
w := httptest.NewRecorder()
r := httptest.NewRequest("DELETE", "/users/logout", nil)
logoutRoute(auth)(w, r)
w.Flush()
if w.Code != 200 {
t.Errorf("got %d", w.Code)
}
}
func TestLogout__cookieDataWithoutUser(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
w := httptest.NewRecorder()
r := httptest.NewRequest("DELETE", "/users/logout", nil)
r.Header.Set("Cookie", "random data")
logoutRoute(auth)(w, r)
w.Flush()
if w.Code != 200 {
t.Errorf("got %d", w.Code)
}
}
func TestLogout__full(t *testing.T) {
auth, err := createTestAuthable()
if err != nil {
t.Fatal(err)
}
defer auth.cleanup()
w := httptest.NewRecorder()
r := httptest.NewRequest("DELETE", "/users/logout", nil)
data := "user data"
r.Header.Set("Cookie", "moov_auth="+data)
// Write a user
userId, _ := hash(fmt.Sprintf("%d", time.Now().Unix()))
cookie := &http.Cookie{
Name: "moov_auth",
Value: data,
Expires: time.Now().Add(1 * time.Hour),
}
if err := auth.writeCookie(userId, cookie); err != nil {
t.Fatal(err)
}
// Verify userId exists
id, err := auth.findUserId(cookie.Value)
if err != nil {
t.Fatal(err)
}
if id == "" {
t.Error("no userId found")
}
// Perofrm logout
logoutRoute(auth)(w, r)
w.Flush()
if w.Code != 200 {
t.Errorf("got %d", w.Code)
}
// Check auth.findUserId
id, err = auth.findUserId(cookie.Value)
if err != nil {
t.Fatal(err)
}
if id != "" {
t.Errorf("userId=%s", id)
}
}