forked from simpleforce/simpleforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce_test.go
More file actions
170 lines (141 loc) · 4.35 KB
/
force_test.go
File metadata and controls
170 lines (141 loc) · 4.35 KB
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
160
161
162
163
164
165
166
167
168
169
170
package simpleforce
import (
"log"
"os"
"testing"
"github.com/jarcoal/httpmock"
)
var (
sfUser = os.ExpandEnv("${SF_USER}")
sfPass = os.ExpandEnv("${SF_PASS}")
sfToken = os.ExpandEnv("${SF_TOKEN}")
sfURL = func() string {
if os.ExpandEnv("${SF_URL}") != "" {
return os.ExpandEnv("${SF_URL}")
} else {
return DefaultURL
}
}()
)
func activateHttpMock() {
}
func registerLoginMock(c *Client) {
loginResp := `<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope>
<env:Body>
<env:loginResponse>
<env:result>
<env:serverUrl>https://na0-api.salesforce.com/services/Soap/c/2.5</env:serverUrl>
<env:sessionId>sessionId</env:sessionId>
<env:userId>userId</env:userId>
<env:userInfo>
<env:userEmail>userEmail</env:userEmail>
<env:userFullName>userFullName</env:userFullName>
<env:userName>userName</env:userName>
</env:userInfo>
</env:result>
</env:loginResponse>
</env:Body>
</env:Envelope>`
mockURL := "https://login.salesforce.com//services/Soap/u/" + c.apiVersion
httpmock.RegisterResponder("POST", mockURL,
httpmock.NewStringResponder(200, loginResp))
}
func requireClient(t *testing.T, skippable bool) *Client {
httpmock.Activate()
httpmock.Reset()
client := NewClient(sfURL, DefaultClientID, DefaultAPIVersion)
if client == nil {
t.Fail()
}
registerLoginMock(client)
err := client.LoginPassword(sfUser, sfPass, sfToken)
if err != nil {
t.Fatal()
}
return client
}
func TestClient_LoginPassword_success(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
client := NewClient(sfURL, DefaultClientID, DefaultAPIVersion)
if client == nil {
t.Fatal()
}
registerLoginMock(client)
// Use token
err := client.LoginPassword(sfUser, sfPass, sfToken)
if err != nil {
t.Fail()
} else {
log.Println(logPrefix, "sessionID:", client.sessionID)
}
}
func TestClient_LoginPassword_fail(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
client := NewClient(sfURL, DefaultClientID, DefaultAPIVersion)
if client == nil {
t.Fatal()
}
loginErrResp := `<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sf="urn:fault.partner.soap.sforce.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode>INVALID_LOGIN</faultcode>
<faultstring>INVALID_LOGIN: Invalid username, password, security token; or user locked out.</faultstring>
<detail>
<sf:LoginFault xsi:type="sf:LoginFault">
<sf:exceptionCode>INVALID_LOGIN</sf:exceptionCode>
<sf:exceptionMessage>Invalid username, password, security token; or user locked out.</sf:exceptionMessage>
</sf:LoginFault>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>`
mockURL := "https://login.salesforce.com//services/Soap/u/" + client.apiVersion
httpmock.RegisterResponder("POST", mockURL,
httpmock.NewStringResponder(500, loginErrResp))
err := client.LoginPassword("__INVALID_USER__", "__INVALID_PASS__", "__INVALID_TOKEN__")
if err == nil {
t.Fail()
}
}
func TestClient_LoginPasswordNoToken(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
client := NewClient(sfURL, DefaultClientID, DefaultAPIVersion)
if client == nil {
t.Fatal()
}
registerLoginMock(client)
// Trusted IP must be configured AND the request must be initiated from the trusted IP range.
err := client.LoginPassword(sfUser, sfPass, "")
if err != nil {
t.FailNow()
} else {
log.Println(logPrefix, "sessionID:", client.sessionID)
}
}
func TestClient_LoginOAuth(t *testing.T) {
}
func TestClient_Query(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
client := requireClient(t, true)
mockURL := "https://na0-api.salesforce.com/services/data/v" + client.apiVersion + "/query?q=SELECT%20Id%2CLastModifiedById%2CLastModifiedDate%2CParentId%2CCommentBody%20FROM%20CaseComment"
httpmock.RegisterResponder("GET", mockURL,
httpmock.NewStringResponder(200, `{"TotalSize": 0, "Done": true, "NextRecordsURL": "NextRecordsURL", "records": []}`))
q := "SELECT Id,LastModifiedById,LastModifiedDate,ParentId,CommentBody FROM CaseComment"
result, err := client.Query(q)
if err != nil {
log.Println(logPrefix, "query failed,", err)
t.FailNow()
}
}
func TestMain(m *testing.M) {
m.Run()
}