@@ -16,8 +16,11 @@ package git
1616
1717import (
1818 "context"
19+ "encoding/json"
1920 "errors"
21+ "io"
2022 "net/http"
23+ "strings"
2124 "testing"
2225
2326 "github.com/codefresh-io/cli-v2/pkg/git/mocks"
@@ -38,22 +41,70 @@ func Test_gitlab_checkApiScope(t *testing.T) {
3841 wantErr string
3942 beforeFn func (t * testing.T , c * mocks.MockRoundTripper )
4043 }{
41- "Should fail if POST fails" : {
44+ "Should fail if POST projects fails" : {
4245 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" ,
4366 beforeFn : func (_ * testing.T , c * mocks.MockRoundTripper ) {
4467 c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Return (nil , errors .New ("some error" ))
4568 },
4669 },
4770 "Should fail if POST fails with 403" : {
4871 wantErr : "git-token is invalid or missing required \" api\" scope" ,
4972 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+ })
5087 c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).Return (& http.Response {
5188 StatusCode : http .StatusForbidden ,
5289 }, nil )
5390 },
5491 },
5592 "Should succeed if POST returns 400" : {
5693 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+ })
57108 c .EXPECT ().RoundTrip (gomock .AssignableToTypeOf (& http.Request {})).Times (1 ).DoAndReturn (func (req * http.Request ) (* http.Response , error ) {
58109 assert .Equal (t , "POST" , req .Method )
59110 assert .Equal (t , "https://some.server/api/v4/projects" , req .URL .String ())
0 commit comments