@@ -2,9 +2,11 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "errors"
5
6
"fmt"
6
7
smartling "github.com/Smartling/api-sdk-go"
7
8
"github.com/stretchr/testify/assert"
9
+ "github.com/stretchr/testify/mock"
8
10
"io/ioutil"
9
11
"net/http"
10
12
"strings"
@@ -18,13 +20,26 @@ type request struct {
18
20
19
21
type roundTripFunc func (req * http.Request ) * http.Response
20
22
21
- func ( f roundTripFunc ) RoundTrip ( req * http. Request ) ( * http. Response , error ) {
22
- return f ( req ), nil
23
+ type MockSmartlingClient struct {
24
+ mock. Mock
23
25
}
24
26
25
- func mockHttpClient (fn roundTripFunc ) * http.Client {
27
+ func (mock * MockSmartlingClient ) UploadFile (
28
+ projectID string ,
29
+ request smartling.FileUploadRequest ,
30
+ ) (* smartling.FileUploadResult , error ) {
31
+ args := mock .Called (projectID , request )
32
+ var result smartling.FileUploadResult
33
+ return & result , args .Error (1 )
34
+ }
35
+
36
+ func (function roundTripFunc ) RoundTrip (req * http.Request ) (* http.Response , error ) {
37
+ return function (req ), nil
38
+ }
39
+
40
+ func mockHttpClient (function roundTripFunc ) * http.Client {
26
41
return & http.Client {
27
- Transport : fn ,
42
+ Transport : function ,
28
43
}
29
44
}
30
45
@@ -42,105 +57,51 @@ func TestPushStopUnauthorized(t *testing.T) {
42
57
43
58
err := doFilesPush (& client , getConfig (), args )
44
59
45
- assert .EqualError (
46
- t ,
47
- err ,
48
- "ERROR: unable to upload file \" README.md\" \n " +
49
- "└─ failed to upload original file: unable to authenticate: " +
50
- "authentication parameters are invalid\n \n " +
51
- "Check, that you have enough permissions to upload file to the specified project" )
60
+ assert .True (t , errors .Is (err , smartling.NotAuthorizedError {}))
52
61
}
53
62
54
63
func TestPushContinueFakeError (t * testing.T ) {
55
64
args := getArgs ("README.md README.md" )
56
65
57
- responses := []request {{`{
58
- "response": {
59
- "code": "SUCCESS",
60
- "data": {
61
- "accessToken": "accessToken",
62
- "refreshToken": "refreshToken"
63
- }
64
- }
65
- }` , 200 },
66
- {`{
67
- "response": {
68
- "code": "SUCCESS",
69
- "data": {
70
- }
71
- }
72
- }` , 401 },
73
- {`{
74
- "response": {
75
- "code": "SUCCESS",
76
- "data": {
77
- }
78
- }
79
- }` , 200 },
80
- }
81
-
82
- httpClient := getMockHttpClient (responses )
83
-
84
66
mockGlobber (args )
85
67
defer func () {
86
68
globFilesLocally = globFilesLocallyFunc
87
69
}()
88
70
89
- client := getClient (httpClient )
71
+ client := new (MockSmartlingClient )
72
+ client .On ("UploadFile" , "test" , mock .Anything ).
73
+ Return (nil , smartling.APIError {Cause : errors .New ("some error" )}).
74
+ Times (2 )
90
75
91
- err := doFilesPush (& client , getConfig (), args )
76
+ err := doFilesPush (client , getConfig (), args )
92
77
assert .EqualError (
93
78
t ,
94
79
err ,
95
- "ERROR: failed to upload 1 files\n \n failed to upload files README.md" )
80
+ "ERROR: failed to upload 2 files\n \n failed to upload files README.md, README.md" )
81
+ client .AssertExpectations (t )
96
82
}
97
83
98
84
func TestPushStopApiError (t * testing.T ) {
99
85
args := getArgs ("README.md README.md" )
100
86
101
- responses := []request {{`{
102
- "response": {
103
- "code": "SUCCESS",
104
- "data": {
105
- "accessToken": "accessToken",
106
- "refreshToken": "refreshToken"
107
- }
108
- }
109
- }` , 200 },
110
- {`{
111
- "response": {
112
- "code": "MAINTENANCE_MODE_ERROR",
113
- "data": {
114
- "accessToken": "accessToken",
115
- "refreshToken": "refreshToken"
116
- }
117
- }
118
- }` , 500 },
119
- {`{
120
- "response": {
121
- "code": "SUCCESS",
122
- "data": {
123
- "accessToken": "accessToken",
124
- "refreshToken": "refreshToken"
125
- }
126
- }
127
- }` , 200 },
128
- }
129
-
130
- httpClient := getMockHttpClient (responses )
131
-
132
87
mockGlobber (args )
133
88
defer func () {
134
89
globFilesLocally = globFilesLocallyFunc
135
90
}()
136
91
137
- client := getClient (httpClient )
92
+ client := new (MockSmartlingClient )
93
+ expectedError := smartling.APIError {
94
+ Cause : errors .New ("some error" ),
95
+ Code : "MAINTENANCE_MODE_ERROR" ,
96
+ }
97
+ client .On ("UploadFile" , "test" , mock .Anything ).
98
+ Return (nil , expectedError ).
99
+ Once ()
138
100
139
- err := doFilesPush (& client , getConfig (), args )
101
+ err := doFilesPush (client , getConfig (), args )
140
102
141
- assert .Error (t , err )
142
- assert .Contains (t , err .Error (), "ERROR: unable to upload file \" README.md\" \n " +
143
- "└─ failed to upload original file: API call returned unexpected HTTP code: 500" )
103
+ assert .True (t , errors .Is (err , expectedError ))
104
+ client .AssertExpectations (t )
144
105
}
145
106
146
107
func getMockHttpClient (responses []request ) * http.Client {
0 commit comments