-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclient_factory_test.go
More file actions
90 lines (69 loc) · 3.24 KB
/
Copy pathclient_factory_test.go
File metadata and controls
90 lines (69 loc) · 3.24 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
package apiclient_test
import (
"net/http"
"testing"
"github.com/OctopusDeploy/cli/pkg/apiclient"
"github.com/OctopusDeploy/cli/test/testutil"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/stretchr/testify/assert"
)
const hostUrl = "http://octopus.com"
const apiKey = "API-APIKEY01"
const accessToken = "token"
func TestValidateMandatoryEnvironment_WhenHostIsNotSupplied_ReturnsError(t *testing.T) {
err := apiclient.ValidateMandatoryEnvironment("", apiKey, accessToken, false)
assert.Error(t, err)
}
func TestValidateMandatoryEnvironment_WhenApiKeyAndAccessTokenAreNotSupplied_ReturnsError(t *testing.T) {
err := apiclient.ValidateMandatoryEnvironment(hostUrl, "", "", false)
assert.Error(t, err)
}
func TestValidateMandatoryEnvironment_WhenHostAndApiKeyAreSupplied_DoesNotReturnError(t *testing.T) {
err := apiclient.ValidateMandatoryEnvironment(hostUrl, apiKey, "", false)
assert.Nil(t, err)
}
func TestValidateMandatoryEnvironment_WhenHostAndAccessTokenAreSupplied_DoesNotReturnError(t *testing.T) {
err := apiclient.ValidateMandatoryEnvironment(hostUrl, "", accessToken, false)
assert.Nil(t, err)
}
func TestNewClientFactory_WhenHostIsNotSupplied_ReturnsError(t *testing.T) {
apiKeyCredential, _ := client.NewApiKey(apiKey)
_, err := apiclient.NewClientFactory(nil, "", apiKeyCredential, "", qa)
assert.Error(t, err)
}
func TestNewClientFactory_WhenHostIsNotAValidUrl_ReturnsError(t *testing.T) {
apiKeyCredential, _ := client.NewApiKey(apiKey)
_, err := apiclient.NewClientFactory(nil, "http_foo:bar/this-is-invalid", apiKeyCredential, "", qa)
assert.Error(t, err)
}
func TestNewClientFactory_WhenApiKeyAndAccessTokenAreNotSupplied_ReturnsError(t *testing.T) {
_, err := apiclient.NewClientFactory(nil, hostUrl, nil, "", qa)
assert.Error(t, err)
}
func TestNewClientFactory_WhenHostAndApiKeyAreSupplied_ReturnsClientFactory(t *testing.T) {
apiKeyCredential, _ := client.NewApiKey(apiKey)
factory, err := apiclient.NewClientFactory(nil, hostUrl, apiKeyCredential, "", qa)
testutil.RequireSuccess(t, err)
assert.NotNil(t, factory)
}
func TestNewClientFactory_WhenHostAndAccessTokenAreSupplied_ReturnsClientFactory(t *testing.T) {
accessTokenCredential, _ := client.NewAccessToken(accessToken)
factory, err := apiclient.NewClientFactory(nil, hostUrl, accessTokenCredential, "", qa)
testutil.RequireSuccess(t, err)
assert.NotNil(t, factory)
}
func TestClientFactory_EnableDryRunGuard_RefusesMutatingRequests(t *testing.T) {
transport := &testutil.RecordingRoundTripper{}
apiKeyCredential, _ := client.NewApiKey(apiKey)
clientFactory, err := apiclient.NewClientFactory(&http.Client{Transport: transport}, hostUrl, apiKeyCredential, "", qa)
testutil.RequireSuccess(t, err)
clientFactory.EnableDryRunGuard()
httpClient, err := clientFactory.GetHttpClient()
testutil.RequireSuccess(t, err)
_, err = httpClient.Post(hostUrl+"/api/Spaces-1/releases/create/v1", "application/json", nil)
assert.ErrorContains(t, err, "dry run blocked a POST request to /api/Spaces-1/releases/create/v1")
assert.Empty(t, transport.Requests, "a mutating request must not reach the server")
_, err = httpClient.Get(hostUrl + "/api/Spaces-1/projects/all")
assert.Nil(t, err)
assert.Len(t, transport.Requests, 1, "read-only requests still go through")
}