forked from vouch/vouch-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogout_test.go
96 lines (83 loc) · 2.69 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
/*
Copyright 2020 The Vouch Proxy Authors.
Use of this source code is governed by The MIT License (MIT) that
can be found in the LICENSE file. Software distributed under The
MIT License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR CONDITIONS OF ANY KIND, either express or implied.
*/
package handlers
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/vouch/vouch-proxy/pkg/cfg"
)
func TestLogoutHandler(t *testing.T) {
setUp("/config/testing/handler_logout_url.yml")
handler := http.HandlerFunc(LogoutHandler)
tests := []struct {
name string
url string
wantcode int
}{
{"allowed", "http://myapp.example.com/login", http.StatusFound},
{"allowed", "https://oauth2.googleapis.com/revoke", http.StatusFound},
{"not allowed", "http://myapp.example.com/loginagain", http.StatusBadRequest},
{"not allowed", "http://google.com/", http.StatusBadRequest},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest("GET", "/logout?url="+tt.url, nil)
req.Host = "myapp.example.com"
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != tt.wantcode {
t.Errorf("LogoutHandler() status = %v, want %v", rr.Code, tt.wantcode)
}
if rr.Code == http.StatusFound && rr.Header().Get("Location") != tt.url {
t.Errorf("LogoutHandler() redirect = %s, want %s", rr.Header().Get("Location"), tt.url)
}
})
}
}
func TestProviderLogoutHandler(t *testing.T) {
setUp("/config/testing/handler_logout_provider.yml")
handler := http.HandlerFunc(LogoutHandler)
tests := []struct {
name string
url string
wantcode int
}{
{"allowed", "http://myapp.example.com/login", http.StatusFound},
{"allowed", "https://oauth2.googleapis.com/revoke", http.StatusFound},
{"not allowed", "http://myapp.example.com/loginagain", http.StatusBadRequest},
{"not allowed", "http://google.com/", http.StatusBadRequest},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest("GET", "/logout?url="+tt.url, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != tt.wantcode {
t.Errorf("LogoutHandler() status = %v, want %v", rr.Code, tt.wantcode)
}
if rr.Code == http.StatusFound {
wanted := tt.url
req, _ := http.NewRequest("GET", cfg.GenOAuth.LogoutURL, nil)
q := req.URL.Query()
q.Add("post_logout_redirect_uri", wanted)
req.URL.RawQuery = q.Encode()
wanted = req.URL.String()
if rr.Header().Get("Location") != wanted {
t.Errorf("LogoutHandler() redirect = %s, want %s", rr.Header().Get("Location"), wanted)
}
}
})
}
}