File: access_token_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 (82 lines) | stat: -rw-r--r-- 1,785 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
package api

import (
	"net/url"
	"reflect"
	"testing"
)

func TestFormResponse_AccessToken(t *testing.T) {
	tests := []struct {
		name     string
		response FormResponse
		want     *AccessToken
		wantErr  *Error
	}{
		{
			name: "with token",
			response: FormResponse{
				values: url.Values{
					"access_token": []string{"ATOKEN"},
					"token_type":   []string{"bearer"},
					"scope":        []string{"repo gist"},
				},
			},
			want: &AccessToken{
				Token:        "ATOKEN",
				RefreshToken: "",
				Type:         "bearer",
				Scope:        "repo gist",
			},
			wantErr: nil,
		},
		{
			name: "with refresh token",
			response: FormResponse{
				values: url.Values{
					"access_token":  []string{"ATOKEN"},
					"refresh_token": []string{"AREFRESHTOKEN"},
					"token_type":    []string{"bearer"},
					"scope":         []string{"repo gist"},
				},
			},
			want: &AccessToken{
				Token:        "ATOKEN",
				RefreshToken: "AREFRESHTOKEN",
				Type:         "bearer",
				Scope:        "repo gist",
			},
			wantErr: nil,
		},
		{
			name: "no token",
			response: FormResponse{
				StatusCode: 200,
				values: url.Values{
					"error": []string{"access_denied"},
				},
			},
			want: nil,
			wantErr: &Error{
				Code:         "access_denied",
				ResponseCode: 200,
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := tt.response.AccessToken()
			if err != nil {
				apiError := err.(*Error)
				if !reflect.DeepEqual(apiError, tt.wantErr) {
					t.Fatalf("error %v, want %v", apiError, tt.wantErr)
				}
			} else if tt.wantErr != nil {
				t.Fatalf("want error %v, got nil", tt.wantErr)
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("FormResponse.AccessToken() = %v, want %v", got, tt.want)
			}
		})
	}
}