@@ -16,8 +16,11 @@ package git
16
16
17
17
import (
18
18
"context"
19
+ "encoding/json"
19
20
"errors"
21
+ "io"
20
22
"net/http"
23
+ "strings"
21
24
"testing"
22
25
23
26
"github.com/codefresh-io/cli-v2/pkg/git/mocks"
@@ -38,22 +41,70 @@ func Test_gitlab_checkApiScope(t *testing.T) {
38
41
wantErr string
39
42
beforeFn func (t * testing.T , c * mocks.MockRoundTripper )
40
43
}{
41
- "Should fail if POST fails" : {
44
+ "Should fail if POST projects fails" : {
42
45
wantErr : "failed checking api scope: Post \" https://some.server/api/v4/projects\" : some error" ,
46
+ beforeFn : func (_ * testing.T , c * mocks.MockRoundTripper ) {
47
+ c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).DoAndReturn (func (req * http.Request ) (* http.Response , error ) {
48
+ assert .Equal (t , "GET" , req .Method )
49
+ assert .Equal (t , "https://some.server/api/v4/user" , req .URL .String ())
50
+ body , _ := json .Marshal (& gitlabUserResponse {
51
+ Username : "username" ,
52
+ Bot : false ,
53
+ })
54
+ bodyReader := io .NopCloser (strings .NewReader (string (body [:])))
55
+ res := & http.Response {
56
+ StatusCode : 200 ,
57
+ Body : bodyReader ,
58
+ }
59
+ return res , nil
60
+ })
61
+ c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Return (nil , errors .New ("some error" ))
62
+ },
63
+ },
64
+ "Should fail if GET user fails" : {
65
+ wantErr : "failed checking api scope: failed getting user: Get \" https://some.server/api/v4/user\" : some error" ,
43
66
beforeFn : func (_ * testing.T , c * mocks.MockRoundTripper ) {
44
67
c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Return (nil , errors .New ("some error" ))
45
68
},
46
69
},
47
70
"Should fail if POST fails with 403" : {
48
71
wantErr : "git-token is invalid or missing required \" api\" scope" ,
49
72
beforeFn : func (_ * testing.T , c * mocks.MockRoundTripper ) {
73
+ c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).DoAndReturn (func (req * http.Request ) (* http.Response , error ) {
74
+ assert .Equal (t , "GET" , req .Method )
75
+ assert .Equal (t , "https://some.server/api/v4/user" , req .URL .String ())
76
+ body , _ := json .Marshal (& gitlabUserResponse {
77
+ Username : "username" ,
78
+ Bot : false ,
79
+ })
80
+ bodyReader := io .NopCloser (strings .NewReader (string (body [:])))
81
+ res := & http.Response {
82
+ StatusCode : 200 ,
83
+ Body : bodyReader ,
84
+ }
85
+ return res , nil
86
+ })
50
87
c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).Return (& http.Response {
51
88
StatusCode : http .StatusForbidden ,
52
89
}, nil )
53
90
},
54
91
},
55
92
"Should succeed if POST returns 400" : {
56
93
beforeFn : func (t * testing.T , c * mocks.MockRoundTripper ) {
94
+ c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).DoAndReturn (func (req * http.Request ) (* http.Response , error ) {
95
+ assert .Equal (t , "GET" , req .Method )
96
+ assert .Equal (t , "https://some.server/api/v4/user" , req .URL .String ())
97
+ body , _ := json .Marshal (& gitlabUserResponse {
98
+ Username : "username" ,
99
+ Bot : false ,
100
+ })
101
+ bodyReader := io .NopCloser (strings .NewReader (string (body [:])))
102
+ res := & http.Response {
103
+ StatusCode : 200 ,
104
+ Body : bodyReader ,
105
+ }
106
+ return res , nil
107
+ })
57
108
c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).DoAndReturn (func (req * http.Request ) (* http.Response , error ) {
58
109
assert .Equal (t , "POST" , req .Method )
59
110
assert .Equal (t , "https://some.server/api/v4/projects" , req .URL .String ())
0 commit comments