File: webapp_flow_test.go

package info (click to toggle)
golang-github-cli-oauth 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 168 kB
  • sloc: makefile: 2
file content (155 lines) | stat: -rw-r--r-- 3,342 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
146
147
148
149
150
151
152
153
154
155
package webapp

import (
	"bytes"
	"context"
	"io/ioutil"
	"net"
	"net/http"
	"net/url"
	"testing"
)

func TestFlow_BrowserURL(t *testing.T) {
	server := &localServer{
		listener: &fakeListener{
			addr: &net.TCPAddr{Port: 12345},
		},
	}

	type fields struct {
		server   *localServer
		clientID string
		state    string
	}
	type args struct {
		baseURL string
		params  BrowserParams
	}
	tests := []struct {
		name    string
		fields  fields
		args    args
		want    string
		wantErr bool
	}{
		{
			name: "happy path",
			fields: fields{
				server: server,
				state:  "xy/z",
			},
			args: args{
				baseURL: "https://github.com/authorize",
				params: BrowserParams{
					ClientID:    "CLIENT-ID",
					RedirectURI: "http://127.0.0.1/hello",
					Scopes:      []string{"repo", "read:org"},
					AllowSignup: true,
				},
			},
			want:    "https://github.com/authorize?client_id=CLIENT-ID&redirect_uri=http%3A%2F%2F127.0.0.1%3A12345%2Fhello&scope=repo+read%3Aorg&state=xy%2Fz",
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			flow := &Flow{
				server:   tt.fields.server,
				clientID: tt.fields.clientID,
				state:    tt.fields.state,
			}
			got, err := flow.BrowserURL(tt.args.baseURL, tt.args.params)
			if (err != nil) != tt.wantErr {
				t.Errorf("Flow.BrowserURL() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("Flow.BrowserURL() = %v, want %v", got, tt.want)
			}
		})
	}
}

type apiStub struct {
	status      int
	body        string
	contentType string
}

type postArgs struct {
	url    string
	params url.Values
}

type apiClient struct {
	stubs []apiStub
	calls []postArgs

	postCount int
}

func (c *apiClient) PostForm(u string, params url.Values) (*http.Response, error) {
	stub := c.stubs[c.postCount]
	c.calls = append(c.calls, postArgs{url: u, params: params})
	c.postCount++
	return &http.Response{
		Body: ioutil.NopCloser(bytes.NewBufferString(stub.body)),
		Header: http.Header{
			"Content-Type": {stub.contentType},
		},
		StatusCode: stub.status,
	}, nil
}

func TestFlow_AccessToken(t *testing.T) {
	server := &localServer{
		listener: &fakeListener{
			addr: &net.TCPAddr{Port: 12345},
		},
		resultChan: make(chan CodeResponse),
	}

	flow := Flow{
		server:   server,
		clientID: "CLIENT-ID",
		state:    "xy/z",
	}

	client := &apiClient{
		stubs: []apiStub{
			{
				body:        "access_token=ATOKEN&token_type=bearer&scope=repo+gist",
				status:      200,
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
			},
		},
	}

	go func() {
		server.resultChan <- CodeResponse{
			Code:  "ABC-123",
			State: "xy/z",
		}
	}()

	token, err := flow.Wait(context.Background(), client, "https://github.com/access_token", WaitOptions{ClientSecret: "OAUTH-SEKRIT"})
	if err != nil {
		t.Fatalf("AccessToken() error: %v", err)
	}

	if len(client.calls) != 1 {
		t.Fatalf("expected 1 HTTP POST, got %d", len(client.calls))
	}
	apiPost := client.calls[0]
	if apiPost.url != "https://github.com/access_token" {
		t.Errorf("HTTP POST to %q", apiPost.url)
	}
	if params := apiPost.params.Encode(); params != "client_id=CLIENT-ID&client_secret=OAUTH-SEKRIT&code=ABC-123&state=xy%2Fz" {
		t.Errorf("HTTP POST params: %v", params)
	}

	if token.Token != "ATOKEN" {
		t.Errorf("Token = %q", token.Token)
	}
}