-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtwitter_test.go
More file actions
152 lines (130 loc) · 3.9 KB
/
Copy pathtwitter_test.go
File metadata and controls
152 lines (130 loc) · 3.9 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package auth
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/oauth2"
)
func TestTwitterLogin(t *testing.T) {
db := NewUserDB(sqlx.MustConnect("sqlite3", ":memory:"))
settings := DefaultSettings
settings.TwitterClientID = "cid"
settings.TwitterClientSecret = "csec"
settings.TwitterRedirectURL = "http://localhost/callback"
h := New(db, settings)
req := httptest.NewRequest("GET", "/user/oauth/login/twitter", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusTemporaryRedirect {
t.Errorf("Expected redirect, got %d", resp.StatusCode)
}
loc, _ := resp.Location()
if !strings.HasPrefix(loc.String(), "https://twitter.com/i/oauth2/authorize") {
t.Errorf("Wrong redirect URL: %s", loc.String())
}
cookies := resp.Cookies()
var stateCookie *http.Cookie
for _, c := range cookies {
if c.Name == "twitter_oauth_state" {
stateCookie = c
break
}
}
if stateCookie == nil {
t.Fatal("State cookie not set")
}
}
type mockTransport struct {
RoundTripFunc func(req *http.Request) (*http.Response, error)
}
func (m *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return m.RoundTripFunc(req)
}
func TestTwitterCallback(t *testing.T) {
db := NewUserDB(sqlx.MustConnect("sqlite3", ":memory:"))
settings := DefaultSettings
settings.TwitterClientID = "cid"
settings.TwitterClientSecret = "csec"
settings.TwitterRedirectURL = "/callback" // Test relative path
h := New(db, settings)
// 1. Simulate Login to get cookie (with next)
loginReq := httptest.NewRequest("GET", "/user/oauth/login/twitter?next=/dashboard", nil)
loginW := httptest.NewRecorder()
h.ServeHTTP(loginW, loginReq)
cookies := loginW.Result().Cookies()
var stateCookie *http.Cookie
for _, c := range cookies {
if c.Name == "twitter_oauth_state" {
stateCookie = c
break
}
}
stateVal := strings.Split(stateCookie.Value, "|")[0]
// 2. Prepare Callback
callbackReq := httptest.NewRequest("GET", "/user/oauth/callback/twitter?state="+stateVal+"&code=fakerequestcode", nil)
callbackReq.AddCookie(stateCookie)
callbackW := httptest.NewRecorder()
// Mock Transport
mock := &mockTransport{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
if req.URL.String() == "https://api.twitter.com/2/oauth2/token" {
// Return fake token
return &http.Response{
StatusCode: 200,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewBufferString(`{
"access_token": "valid_token",
"token_type": "bearer",
"expires_in": 3600
}`)),
}, nil
}
if req.URL.String() == "https://api.twitter.com/2/users/me" {
// Return fake user
return &http.Response{
StatusCode: 200,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewBufferString(`{
"data": {
"id": "12345",
"name": "Test User",
"username": "testuser"
}
}`)),
}, nil
}
return nil, nil // Error
},
}
client := &http.Client{Transport: mock}
ctx := context.WithValue(callbackReq.Context(), oauth2.HTTPClient, client)
// Inject mocked context
h.ServeHTTP(callbackW, callbackReq.WithContext(ctx))
resp := callbackW.Result()
if resp.StatusCode != http.StatusFound { // Redirect
t.Errorf("Expected redirect after callback, got %d. Body: %s", resp.StatusCode, callbackW.Body.String())
}
loc, _ := resp.Location()
if loc.Path != "/dashboard" {
t.Errorf("Expected redirect to /dashboard, got %s", loc.Path)
}
// Verify user created
tx := db.Begin(context.Background())
uid := tx.GetOauthUser("twitter", "12345")
if uid == 0 {
t.Error("User was not created in DB")
}
// Check email
_, password := tx.GetPassword("testuser@twitter.example.com")
if password != "" {
t.Error("Password should be empty/mismatched but account should exist")
}
tx.Rollback()
}