forked from 99designs/smartling
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpush_test.go
159 lines (131 loc) · 3.56 KB
/
push_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
"github.com/Smartling/smartling-cli/mocks"
smartling "github.com/Smartling/api-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type request struct {
response string
code int
}
type roundTripFunc func(req *http.Request) *http.Response
func (function roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return function(req), nil
}
func mockHttpClient(function roundTripFunc) *http.Client {
return &http.Client{
Transport: function,
}
}
func TestPushStopUnauthorized(t *testing.T) {
args := getArgs("README.md README.md")
httpClient := getMockHttpClient([]request{{"{}", 401}})
mockGlobber(args)
defer func() {
globFilesLocally = globFilesLocallyFunc
}()
client := getClient(httpClient)
err := doFilesPush(&client, getConfig(), args)
assert.True(t, errors.Is(err, smartling.NotAuthorizedError{}))
}
func TestPushContinueFakeError(t *testing.T) {
args := getArgs("README.md README.md")
mockGlobber(args)
defer func() {
globFilesLocally = globFilesLocallyFunc
}()
client := &mocks.ClientInterface{}
client.On("UploadFile", "test", mock.Anything).
Return(nil, smartling.APIError{Cause: errors.New("some error")}).
Times(2)
err := doFilesPush(client, getConfig(), args)
assert.EqualError(
t,
err,
"ERROR: failed to upload 2 files\n\nfailed to upload files README.md, README.md")
client.AssertExpectations(t)
}
func TestPushStopApiError(t *testing.T) {
args := getArgs("README.md README.md")
mockGlobber(args)
defer func() {
globFilesLocally = globFilesLocallyFunc
}()
client := &mocks.ClientInterface{}
expectedError := smartling.APIError{
Cause: errors.New("some error"),
Code: "MAINTENANCE_MODE_ERROR",
}
client.On("UploadFile", "test", mock.Anything).
Return(nil, expectedError).
Once()
err := doFilesPush(client, getConfig(), args)
assert.True(t, errors.Is(err, expectedError))
client.AssertExpectations(t)
}
func getMockHttpClient(responses []request) *http.Client {
responseCount := 0
return mockHttpClient(func(req *http.Request) *http.Response {
var response string
var statusCode int
header := make(http.Header)
header.Add("Content-Type", "application/json")
if responseCount >= len(responses) {
response = responses[len(responses)-1].response
statusCode = responses[len(responses)-1].code
} else {
response = responses[responseCount].response
statusCode = responses[responseCount].code
responseCount++
}
return &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(bytes.NewBufferString(response)),
Header: header,
}
})
}
func getArgs(file string) map[string]interface{} {
args := make(map[string]interface{})
args["--authorize"] = false
args["--directory"] = ""
args["<file>"] = file
return args
}
func getConfig() Config {
fileConfig := make(map[string]FileConfig)
fileConfig["default"] = FileConfig{
Push: struct {
Type string `yaml:"type,omitempty"`
Directives map[string]string `yaml:"directives,omitempty,flow"`
}{Type: "md"},
}
return Config{
UserID: "test",
Secret: "test",
ProjectID: "test",
Files: fileConfig,
}
}
func getClient(httpClient *http.Client) smartling.Client {
client := smartling.NewClient("test", "test")
client.HTTP = httpClient
return *client
}
func mockGlobber(args map[string]interface{}) {
globFilesLocally = func(
directory string,
base string,
mask string,
) ([]string, error) {
return strings.Split(fmt.Sprintf("%s", args["<file>"]), " "), nil
}
}