-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
76 lines (64 loc) · 2.27 KB
/
main_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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"golang.org/x/oauth2"
)
func init() {
os.Setenv("LINKEDIN_CLIENT_ID", "mock-client-id")
os.Setenv("LINKEDIN_CLIENT_SECRET", "mock-client-secret")
os.Setenv("REDIRECT_URL", "http://localhost:8080/callback")
}
// Test the displayProfile function
func TestDisplayProfile(t *testing.T) {
// Create a mock HTTP server to return a sample response
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v2/me" {
profile := map[string]interface{}{
"firstName": "John",
"lastName": "Doe",
"email": "[email protected]",
}
json.NewEncoder(w).Encode(profile)
}
}))
defer server.Close()
// Create a new OAuth2 token with a mock access token
token := &oauth2.Token{AccessToken: "mock-access-token"}
// Update the profileEndpoint to use the mock server URL
profileEndpoint = server.URL + "/v2/me"
// Call the displayProfile function with the mock token
profile := displayProfile(token)
// Verify that the profile was parsed correctly
if profile == nil {
t.Error("Failed to parse profile information")
}
if profile["firstName"] != "John" {
t.Errorf("Expected firstName to be 'John', but got '%s'", profile["firstName"])
}
if profile["lastName"] != "Doe" {
t.Errorf("Expected lastName to be 'Doe', but got '%s'", profile["lastName"])
}
if profile["email"] != "[email protected]" {
t.Errorf("Expected email to be '[email protected]', but got '%s'", profile["email"])
}
}
func TestIndexHandler(t *testing.T) {
// Create a new HTTP request
req, _ := http.NewRequest("GET", "/", nil)
// Create a new HTTP response recorder to capture the response
w := httptest.NewRecorder()
// Call the indexHandler function with the mock request and response
indexHandler(w, req)
// Verify that the response is a redirect to the OAuth2 provider
if w.Code != http.StatusTemporaryRedirect {
t.Errorf("Expected status code %d, but got %d", http.StatusTemporaryRedirect, w.Code)
}
expectedLocation := oauthConfig.AuthCodeURL("state", oauth2.AccessTypeOffline)
if w.Header().Get("Location") != expectedLocation {
t.Errorf("Expected Location header to be '%s', but got '%s'", expectedLocation, w.Header().Get("Location"))
}
}