add waitingroom rule tests
ADD
.changelog/1102.txt
+```release-note:enhancement
+waiting room: add support for waiting room rules
+```
\ No newline at end of file

MODIFY
waiting_room.go
waiting_room.go
@@ -455,8 +455,8 @@ func (api *API) UpdateWaitingRoomRule(ctx context.Context, zoneID string, waitin
// DeleteWaitingRoomRule deletes a rule for a Waiting Room.
//
// API reference: https://api.cloudflare.com/#waiting-room-delete-waiting-room-rule
-func (api *API) DeleteWaitingRoomRule(ctx context.Context, zoneID string, waitingRoomID string, ruleId string) ([]WaitingRoomRule, error) {
- uri := fmt.Sprintf("/zones/%s/waiting_rooms/%s/rules/%s", zoneID, waitingRoomID, ruleId)
+func (api *API) DeleteWaitingRoomRule(ctx context.Context, zoneID string, waitingRoomID string, ruleID string) ([]WaitingRoomRule, error) {
+ uri := fmt.Sprintf("/zones/%s/waiting_rooms/%s/rules/%s", zoneID, waitingRoomID, ruleID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return nil, err

MODIFY
waiting_room_test.go
waiting_room_test.go
@@ -13,6 +13,7 @@ import (
var waitingRoomID = "699d98642c564d2e855e9661899b7252"
var waitingRoomEventID = "25756b2dfe6e378a06b033b670413757"
+var waitingRoomRuleID = "25756b2dfe6e378a06b033b670413757"
var testTimestampWaitingRoom = time.Now().UTC()
var testTimestampWaitingRoomEvent = time.Now().UTC()
var testTimestampWaitingRoomEventPrequeue = time.Now().UTC()
@@ -78,6 +79,18 @@ var waitingRoomStatusJSON = fmt.Sprintf(`
}
`, waitingRoomEventID)
+var waitingRoomRuleJSON = fmt.Sprintf(`
+{
+ "id": "%s",
+ "version": "1",
+ "description": "bypass ip",
+ "action": "bypass_waiting_room",
+ "expression": "ip.src in {1.2.3.4 5.6.7.8}",
+ "enabled": true,
+ "last_updated": "%s"
+}
+`, waitingRoomRuleID, testTimestampWaitingRoom.Format(time.RFC3339Nano))
+
var waitingRoomPagePreviewJSON = `
{
"preview_url": "http://waitingrooms.dev/preview/35af8c12-6d68-4608-babb-b53435a5ddfb"
@@ -137,6 +150,16 @@ var waitingRoomPagePreview = WaitingRoomPagePreviewURL{
PreviewURL: "http://waitingrooms.dev/preview/35af8c12-6d68-4608-babb-b53435a5ddfb",
}
+var waitingRoomRule = WaitingRoomRule{
+ ID: waitingRoomRuleID,
+ Version: "1",
+ Action: "bypass_waiting_room",
+ Expression: "ip.src in {1.2.3.4 5.6.7.8}",
+ Description: "bypass ip",
+ Enabled: true,
+ LastUpdated: &testTimestampWaitingRoom,
+}
+
func TestListWaitingRooms(t *testing.T) {
setup()
defer teardown()
@@ -617,3 +640,136 @@ func TestDeleteWaitingRoomEvent(t *testing.T) {
err := client.DeleteWaitingRoomEvent(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252", "25756b2dfe6e378a06b033b670413757")
assert.NoError(t, err)
}
+
+func TestListWaitingRoomRules(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", r.Method)
+ w.Header().Set("content-type", "application/json")
+ fmt.Fprintf(w, `{
+ "success": true,
+ "errors": [],
+ "messages": [],
+ "result": [
+ %s
+ ]
+ }
+ `, waitingRoomRuleJSON)
+ }
+
+ mux.HandleFunc("/zones/"+testZoneID+"/waiting_rooms/699d98642c564d2e855e9661899b7252/rules", handler)
+ want := []WaitingRoomRule{waitingRoomRule}
+
+ actual, err := client.ListWaitingRoomRules(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252")
+ if assert.NoError(t, err) {
+ assert.Equal(t, want, actual)
+ }
+}
+
+func TestCreateWaitingRoomRule(t *testing.T) {
+ setup()
+ defer teardown()
+
+ handler := func(w http.ResponseWriter, r *http.Request) {
+ assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
+ w.Header().Set("content-type", "application/json")
+ fmt.Fprintf(w, `{
+ "success": true,
+ "errors": [],
+ "messages": [],
+ "result": [
+ %s
+ ]
+ }
+ `, waitingRoomRuleJSON)
+ }
+
+ mux.HandleFunc("/zones/"+testZoneID+"/waiting_rooms/699d98642c564d2e855e9661899b7252/rules", handler)
+ want := []WaitingRoomRule{waitingRoomRule}
+
+ actual, err := client.CreateWaitingRoomRule(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252", waitingRoomRule)
+ if assert.NoError(t, err) {
+ assert.Equal(t, want, actual)
+ }
+}
+
+func TestUpdateWaitingRoomRule(t *testing.T) {
+ setup()
+ defer teardown()
+
+ handler := func(w http.ResponseWriter, r *http.Request) {
+ assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)
+ w.Header().Set("content-type", "application/json")
+ fmt.Fprintf(w, `{
+ "success": true,
+ "errors": [],
+ "messages": [],
+ "result": [
+ %s
+ ]
+ }
+ `, waitingRoomRuleJSON)
+ }
+
+ mux.HandleFunc("/zones/"+testZoneID+"/waiting_rooms/699d98642c564d2e855e9661899b7252/rules/"+waitingRoomRuleID, handler)
+ want := []WaitingRoomRule{waitingRoomRule}
+
+ actual, err := client.UpdateWaitingRoomRule(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252", waitingRoomRule)
+ if assert.NoError(t, err) {
+ assert.Equal(t, want, actual)
+ }
+}
+
+func TestDeleteWaitingRoomRule(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", r.Method)
+ w.Header().Set("content-type", "application/json")
+ fmt.Fprint(w, `{
+ "success": true,
+ "errors": [],
+ "messages": [],
+ "result": []
+ }
+ `)
+ }
+
+ mux.HandleFunc("/zones/"+testZoneID+"/waiting_rooms/699d98642c564d2e855e9661899b7252/rules/"+waitingRoomRuleID, handler)
+ want := []WaitingRoomRule{}
+
+ actual, err := client.DeleteWaitingRoomRule(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252", waitingRoomRuleID)
+ if assert.NoError(t, err) {
+ assert.Equal(t, want, actual)
+ }
+}
+
+func TestReplaceWaitingRoomRules(t *testing.T) {
+ setup()
+ defer teardown()
+
+ handler := func(w http.ResponseWriter, r *http.Request) {
+ assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
+ w.Header().Set("content-type", "application/json")
+ fmt.Fprintf(w, `{
+ "success": true,
+ "errors": [],
+ "messages": [],
+ "result": [
+ %s
+ ]
+ }
+ `, waitingRoomRuleJSON)
+ }
+
+ mux.HandleFunc("/zones/"+testZoneID+"/waiting_rooms/699d98642c564d2e855e9661899b7252/rules", handler)
+ want := []WaitingRoomRule{waitingRoomRule}
+
+ actual, err := client.ReplaceWaitingRoomRules(context.Background(), testZoneID, "699d98642c564d2e855e9661899b7252", want)
+ if assert.NoError(t, err) {
+ assert.Equal(t, want, actual)
+ }
+}

add waitingroom rule tests