Skip to content

Commit 520d90d

Browse files
mosh0405anandkunal
authored andcommitted
PolicyAssetAssociation resource - adding unit tests
1 parent 12abfe0 commit 520d90d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package incapsula
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func TestClientPolicyAssetAssociatedPositive(t *testing.T) {
12+
isAssociated, err := ClientPolicyAssetAssociatedBase(t, true)
13+
if err != nil {
14+
t.Errorf("unexpected error")
15+
}
16+
if !isAssociated {
17+
t.Errorf("expected policy to be assosiated")
18+
}
19+
}
20+
21+
func TestClientPolicyAssetAssociatedNegative(t *testing.T) {
22+
isAssociated, err := ClientPolicyAssetAssociatedBase(t, false)
23+
if err == nil {
24+
t.Errorf("epected error but got none")
25+
}
26+
if isAssociated {
27+
t.Errorf("expected policy to NOT be assosiated")
28+
}
29+
}
30+
31+
func ClientPolicyAssetAssociatedBase(t *testing.T, shouldBeAssociated bool) (bool, error) {
32+
log.Printf("======================== BEGIN TEST ========================")
33+
log.Printf("[DEBUG] Running test Running test client_policy_asset_association.TestClientPolicyAssetAssociated, shouldBeAssociated set to %t", shouldBeAssociated)
34+
apiID := "foo"
35+
apiKey := "bar"
36+
assetID := "5432"
37+
policyID := "11"
38+
assetType := "WEBSITE"
39+
40+
endpoint := fmt.Sprintf("/policies/v2/policies/%s/assets/%s/%s?api_id=%s&api_key=%s", policyID, assetType, assetID, apiID, apiKey)
41+
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
42+
if req.URL.String() != endpoint {
43+
t.Errorf("Should have have hit %s endpoint. Got: %s", endpoint, req.URL.String())
44+
}
45+
if shouldBeAssociated {
46+
rw.WriteHeader(200)
47+
rw.Write([]byte(`{"value":true,"isError":false}`))
48+
} else {
49+
rw.WriteHeader(404)
50+
rw.Write([]byte(`{"value":false,"isError":false}`))
51+
}
52+
}))
53+
defer server.Close()
54+
55+
config := &Config{APIID: apiID, APIKey: apiKey, BaseURLAPI: server.URL}
56+
client := &Client{config: config, httpClient: &http.Client{}}
57+
58+
return client.isPolicyAssetAssociated(policyID, assetID, assetType)
59+
60+
}

0 commit comments

Comments
 (0)