File: intercom_test.go

package info (click to toggle)
golang-github-markbates-goth 1.42.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 968 kB
  • sloc: makefile: 3
file content (145 lines) | stat: -rw-r--r-- 3,757 bytes parent folder | download
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
//go:build ignore
// +build ignore

package intercom_test

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/pat"
	"github.com/markbates/goth"
	"github.com/markbates/goth/providers/intercom"
	"github.com/stretchr/testify/assert"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"
)

type fetchUserPayload struct {
	ID            string `json:"id"`
	Email         string `json:"email"`
	Name          string `json:"name"`
	Link          string `json:"link"`
	EmailVerified bool   `json:"email_verified"`
	Avatar        struct {
		URL string `json:"image_url"`
	} `json:"avatar"`
}

func Test_New(t *testing.T) {
	t.Parallel()
	a := assert.New(t)

	provider := intercomProvider()
	a.Equal(provider.ClientKey, os.Getenv("INTERCOM_KEY"))
	a.Equal(provider.Secret, os.Getenv("INTERCOM_SECRET"))
	a.Equal(provider.CallbackURL, "/foo")
}

func Test_Implements_Provider(t *testing.T) {
	t.Parallel()
	a := assert.New(t)
	a.Implements((*goth.Provider)(nil), intercomProvider())
}
func Test_BeginAuth(t *testing.T) {
	t.Parallel()
	a := assert.New(t)
	provider := intercomProvider()
	session, err := provider.BeginAuth("test_state")
	s := session.(*intercom.Session)
	a.NoError(err)
	a.Contains(s.AuthURL, "https://app.intercom.io/oauth")
	a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("INTERCOM_KEY")))
	a.Contains(s.AuthURL, "state=test_state")
}

func Test_SessionFromJSON(t *testing.T) {
	t.Parallel()
	a := assert.New(t)

	provider := intercomProvider()

	s, err := provider.UnmarshalSession(`{"AuthURL":"https://app.intercom.io/oauth","AccessToken":"1234567890"}`)
	a.NoError(err)
	session := s.(*intercom.Session)
	a.Equal(session.AuthURL, "https://app.intercom.io/oauth")
	a.Equal(session.AccessToken, "1234567890")
}

func intercomProvider() *intercom.Provider {
	return intercom.New(os.Getenv("INTERCOM_KEY"), os.Getenv("INTERCOM_SECRET"), "/foo", "basic")
}

func Test_FetchUser(t *testing.T) {
	a := assert.New(t)

	u := fetchUserPayload{}
	u.ID = "1"
	u.Email = "wash@serenity.now"
	u.Name = "Hoban Washburne"
	u.EmailVerified = true
	u.Avatar.URL = "http://avatarURL"

	mockIntercomFetchUser(&u, func(ts *httptest.Server) {
		provider := intercomProvider()
		session := &intercom.Session{AccessToken: "token"}

		user, err := provider.FetchUser(session)
		a.NoError(err)

		a.Equal("1", user.UserID)
		a.Equal("wash@serenity.now", user.Email)
		a.Equal("Hoban Washburne", user.Name)
		a.Equal("Hoban", user.FirstName)
		a.Equal("Washburne", user.LastName)
		a.Equal("http://avatarURL", user.AvatarURL)
		a.Equal(true, user.RawData["email_verified"])
		a.Equal("token", user.AccessToken)
	})
}

func Test_FetchUnverifiedUser(t *testing.T) {
	a := assert.New(t)

	u := fetchUserPayload{}
	u.ID = "1"
	u.Email = "wash@serenity.now"
	u.Name = "Hoban Washburne"
	u.EmailVerified = false
	u.Avatar.URL = "http://avatarURL"

	mockIntercomFetchUser(&u, func(ts *httptest.Server) {
		provider := intercomProvider()
		session := &intercom.Session{AccessToken: "token"}

		user, err := provider.FetchUser(session)
		a.NoError(err)

		a.Equal("1", user.UserID)
		a.Equal("wash@serenity.now", user.Email)
		a.Equal("Hoban Washburne", user.Name)
		a.Equal("Hoban", user.FirstName)
		a.Equal("Washburne", user.LastName)
		a.Equal("http://avatarURL", user.AvatarURL)
		a.Equal(false, user.RawData["email_verified"])
		a.Equal("token", user.AccessToken)
	})
}

func mockIntercomFetchUser(fetchUserPayload *fetchUserPayload, f func(*httptest.Server)) {
	p := pat.New()
	p.Get("/me", func(res http.ResponseWriter, req *http.Request) {
		json.NewEncoder(res).Encode(fetchUserPayload)
	})
	ts := httptest.NewServer(p)
	defer ts.Close()

	originalUserURL := intercom.UserURL

	intercom.UserURL = ts.URL + "/me"

	f(ts)

	intercom.UserURL = originalUserURL
}