Skip to content

Commit

Permalink
Merge pull request cloudflare#639 from itsmatinx/add_tests_for_zone_l…
Browse files Browse the repository at this point in the history
…ockdown_and_few_changes

Add tests for the Zone Lockdown functionality and few small changes
  • Loading branch information
jacobbednarz authored May 19, 2021
2 parents 8279958 + 248b87d commit 0e0a80b
Show file tree
Hide file tree
Showing 2 changed files with 338 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lockdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strconv"
"time"

"github.com/pkg/errors"
)
Expand All @@ -20,6 +21,8 @@ type ZoneLockdown struct {
Configurations []ZoneLockdownConfig `json:"configurations"`
Paused bool `json:"paused"`
Priority int `json:"priority,omitempty"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
}

// ZoneLockdownConfig represents a Zone Lockdown config, which comprises
Expand Down
335 changes: 335 additions & 0 deletions lockdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCreateZoneLockdown(t *testing.T) {
setup()
defer teardown()

input := ZoneLockdown{
Description: "Restrict access to these endpoints to requests from a known IP address",
URLs: []string{"api.mysite.com/some/endpoint*"},
Configurations: []ZoneLockdownConfig{{
Target: "ip",
Value: "198.51.100.4",
}},
Paused: false,
}

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s")

var v ZoneLockdown
err := json.NewDecoder(r.Body).Decode(&v)
require.NoError(t, err)
assert.Equal(t, input, v)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"paused": false,
"description": "Restrict access to these endpoints to requests from a known IP address",
"urls": [
"api.mysite.com/some/endpoint*"
],
"configurations": [
{
"target": "ip",
"value": "198.51.100.4"
}
]
}
}`)
}

mux.HandleFunc("/zones/"+zoneID+"/firewall/lockdowns", handler)

actual, err := client.CreateZoneLockdown(context.Background(), zoneID, input)
require.NoError(t, err)

createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
want := &ZoneLockdownResponse{
Result: ZoneLockdown{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Description: input.Description,
URLs: input.URLs,
Configurations: input.Configurations,
Paused: input.Paused,
CreatedOn: &createdOn,
ModifiedOn: &modifiedOn,
},
Response: Response{
Success: true,
Errors: []ResponseInfo{},
Messages: []ResponseInfo{},
},
}
assert.Equal(t, want, actual)
}

func TestUpdateZoneLockdownt(t *testing.T) {
setup()
defer teardown()

input := ZoneLockdown{
Description: "Restrict access to these endpoints to requests from a known IP address",
URLs: []string{"api.mysite.com/some/endpoint*"},
Configurations: []ZoneLockdownConfig{{
Target: "ip",
Value: "198.51.100.4",
}},
Paused: false,
}

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s")

var v ZoneLockdown
err := json.NewDecoder(r.Body).Decode(&v)
require.NoError(t, err)
assert.Equal(t, input, v)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"paused": false,
"description": "Restrict access to these endpoints to requests from a known IP address",
"urls": [
"api.mysite.com/some/endpoint*"
],
"configurations": [
{
"target": "ip",
"value": "198.51.100.4"
}
]
}
}`)
}

zoneLockdownID := "372e67954025e0ba6aaa6d586b9e0b59"

mux.HandleFunc("/zones/"+zoneID+"/firewall/lockdowns/"+zoneLockdownID, handler)

actual, err := client.UpdateZoneLockdown(context.Background(), zoneID, zoneLockdownID, input)
require.NoError(t, err)

createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
want := &ZoneLockdownResponse{
Result: ZoneLockdown{
ID: zoneLockdownID,
Description: input.Description,
URLs: input.URLs,
Configurations: input.Configurations,
Paused: input.Paused,
CreatedOn: &createdOn,
ModifiedOn: &modifiedOn,
},
Response: Response{
Success: true,
Errors: []ResponseInfo{},
Messages: []ResponseInfo{},
},
}
assert.Equal(t, want, actual)
}

func TestDeleteZoneLockdown(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s")

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59"
}
}`)
}

zoneLockdownID := "372e67954025e0ba6aaa6d586b9e0b59"

mux.HandleFunc("/zones/"+zoneID+"/firewall/lockdowns/"+zoneLockdownID, handler)

actual, err := client.DeleteZoneLockdown(context.Background(), zoneID, zoneLockdownID)
require.NoError(t, err)

want := &ZoneLockdownResponse{
Result: ZoneLockdown{
ID: zoneLockdownID,
},
Response: Response{
Success: true,
Errors: []ResponseInfo{},
Messages: []ResponseInfo{},
},
}
assert.Equal(t, want, actual)
}

func TestZoneLockdown(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s")

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"paused": false,
"description": "Restrict access to these endpoints to requests from a known IP address",
"urls": [
"api.mysite.com/some/endpoint*"
],
"configurations": [
{
"target": "ip",
"value": "198.51.100.4"
}
]
}
}`)
}

zoneLockdownID := "372e67954025e0ba6aaa6d586b9e0b59"

mux.HandleFunc("/zones/"+zoneID+"/firewall/lockdowns/"+zoneLockdownID, handler)

actual, err := client.ZoneLockdown(context.Background(), zoneID, zoneLockdownID)
require.NoError(t, err)

createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
want := &ZoneLockdownResponse{
Result: ZoneLockdown{
ID: zoneLockdownID,
Description: "Restrict access to these endpoints to requests from a known IP address",
URLs: []string{"api.mysite.com/some/endpoint*"},
Configurations: []ZoneLockdownConfig{{
Target: "ip",
Value: "198.51.100.4",
}},
Paused: false,
CreatedOn: &createdOn,
ModifiedOn: &modifiedOn,
},
Response: Response{
Success: true,
Errors: []ResponseInfo{},
Messages: []ResponseInfo{},
},
}
assert.Equal(t, want, actual)
}

func TestListZoneLockdowns(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s")
assert.Equal(t, "1", r.URL.Query().Get("page"))

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"id": "372e67954025e0ba6aaa6d586b9e0b59",
"created_on": "2014-01-01T05:20:00Z",
"modified_on": "2014-01-01T05:20:00Z",
"paused": false,
"description": "Restrict access to these endpoints to requests from a known IP address",
"urls": [
"api.mysite.com/some/endpoint*"
],
"configurations": [
{
"target": "ip",
"value": "198.51.100.4"
}
]
}
],
"result_info": {
"page": 1,
"per_page": 20,
"count": 1,
"total_count": 2000
}
}`)

}

mux.HandleFunc("/zones/"+zoneID+"/firewall/lockdowns", handler)

actual, err := client.ListZoneLockdowns(context.Background(), zoneID, 1)
require.NoError(t, err)

createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
want := &ZoneLockdownListResponse{
Result: []ZoneLockdown{{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Description: "Restrict access to these endpoints to requests from a known IP address",
URLs: []string{"api.mysite.com/some/endpoint*"},
Configurations: []ZoneLockdownConfig{{
Target: "ip",
Value: "198.51.100.4",
}},
Paused: false,
CreatedOn: &createdOn,
ModifiedOn: &modifiedOn,
}},
Response: Response{
Success: true,
Errors: []ResponseInfo{},
Messages: []ResponseInfo{},
},
ResultInfo: ResultInfo{
Page: 1,
PerPage: 20,
Count: 1,
Total: 2000,
},
}
assert.Equal(t, want, actual)
}

0 comments on commit 0e0a80b

Please sign in to comment.