This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
/
installedapp_test.go
67 lines (59 loc) · 1.68 KB
/
installedapp_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
// Copyright 2013 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed by the Apache v2.0 license that can be
// found in the LICENSE file.
package ofh
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"testing"
"github.com/maruel/ut"
)
type roundTripperStub struct {
requests []*http.Request
replies []*http.Response
}
func (r *roundTripperStub) RoundTrip(req *http.Request) (resp *http.Response, err error) {
r.requests = append(r.requests, req)
resp = r.replies[0]
r.replies = r.replies[1:]
return
}
func asReader(v string) io.ReadCloser {
return ioutil.NopCloser(bytes.NewBufferString(v))
}
func TestInstalledApp(t *testing.T) {
i := &InstalledApp{
ClientID: "C",
ClientSecret: "S",
AuthURL: "http://localhost/auth",
TokenURL: "http://localhost/token",
ScopedTokenCache: make(map[string]*TokenCache),
}
tokReply := `{"access_token":"a", "refresh_token": "r", "id_token": "i"}`
resp := []*http.Response{
{StatusCode: 200, Body: asReader(tokReply)},
}
r := &roundTripperStub{[]*http.Request{}, resp}
prompt := func(string) string {
return "auth"
}
_, err := i.GetClientPrompt("scope", r, prompt)
ut.AssertEqual(t, nil, err)
ut.AssertEqual(t, 1, len(r.requests))
i.Lock()
defer i.Unlock()
ut.AssertEqual(t, true, i.ShouldSave())
i.ClearDirtyBit()
ut.AssertEqual(t, false, i.ShouldSave())
}
func TestMakeInstalledApp(t *testing.T) {
actual := MakeInstalledApp()
expected := &InstalledApp{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
ScopedTokenCache: make(map[string]*TokenCache),
}
ut.AssertEqual(t, expected, actual)
}