forked from shuheiktgw/go-travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication_test.go
70 lines (55 loc) · 2.1 KB
/
authentication_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
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package travis
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestAuthenticationService_UsingGithubToken_without_token(t *testing.T) {
as := &AuthenticationService{client: NewClient(ApiOrgUrl, "")}
_, _, err := as.UsingGithubToken(context.TODO(), "")
if err == nil {
t.Fatal("AuthenticationService.UsingGithubToken with empty token: error is not supposed to be nil")
}
}
func TestAuthenticationService_UsingGithubToken_with_token(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/auth/github", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Accept", "application/vnd.travis-ci.2.1+json")
fmt.Fprint(w, `{"access_token":"test_access_token"}`)
})
repo, _, err := client.Authentication.UsingGithubToken(context.Background(), "test_github_token")
if err != nil {
t.Errorf("AuthenticationService.UsingGithubToken returned error: %v", err)
}
want := AccessToken("test_access_token")
if !reflect.DeepEqual(repo, want) {
t.Errorf("AuthenticationService.UsingGithubToken returned %+v, want %+v", repo, want)
}
}
func TestAuthenticationService_UsingTravisToken_without_token(t *testing.T) {
as := &AuthenticationService{client: NewClient(ApiOrgUrl, "")}
err := as.UsingTravisToken("")
if err == nil {
t.Fatal("AuthenticationService.UsingTravisToken with empty token: error is not supposed to be nil")
}
}
func TestAuthenticationService_UsingTravisToken_with_token(t *testing.T) {
token := "abc123easyasdoremi"
as := &AuthenticationService{client: NewClient(ApiOrgUrl, token)}
err := as.UsingTravisToken(token)
if err != nil {
t.Fatalf("AuthenticationService.UsingTravisToken: unexpected error occurred: %s", err)
}
authHeader := as.client.Headers["Authorization"]
if authHeader != fmt.Sprintf("token %s", token) {
t.Fatalf("AuthenticationService.Headers: unexpected Authorization %s; expected %s", authHeader, token)
}
}