Skip to content

Commit

Permalink
Merge pull request #25 from signalsciences/AddResponseCodeToSiteRule
Browse files Browse the repository at this point in the history
add response code to site rules.actions
  • Loading branch information
jhanrahan-sigsci authored Mar 7, 2022
2 parents 52f44e3 + 38a2171 commit ee1e001
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
7 changes: 5 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func (sc *Client) doRequest(method, url, reqBody string) ([]byte, error) {
if resp.StatusCode != http.StatusNoContent {
return body, errMsg(body)
}
case "PUT":
fallthrough
case "PATCH":
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return body, errMsg(body)
Expand Down Expand Up @@ -1881,8 +1883,9 @@ type Condition struct {

// Action contains the rule action
type Action struct {
Type string `json:"type,omitempty"` //(block, allow, exclude)
Signal string `json:"signal,omitempty"`
Type string `json:"type,omitempty"` //(block, allow, exclude)
Signal string `json:"signal,omitempty"`
ResponseCode int `json:"responseCode,omitempty"` //(400-499)
}

// RateLimit holds all the data that is specific to rate limit rules
Expand Down
83 changes: 82 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestCreateReadUpdateDeleteSiteRules(t *testing.T) {
t.Fatal(err)
}
if !compareSiteRuleBody(updateSiteRuleBody, updateResp.CreateSiteRuleBody) {
t.Errorf("CreateSiteRulesgot: %v expected %v", createResp, createSiteRulesBody)
t.Errorf("CreateSiteRules got: %v expected %v", updateResp, updateSiteRuleBody)
}

readall, err := sc.GetAllSiteRules(corp, site)
Expand Down Expand Up @@ -1299,3 +1299,84 @@ func TestClient_GetSitePrimaryAgentKey(t *testing.T) {
t.Error("Expected secret key to be populated")
}
}

func TestCreateSiteRulesResponseCode(t *testing.T) {
createSiteRulesBody := CreateSiteRuleBody{
Type: "request",
GroupOperator: "all",
Enabled: true,
Reason: "Example site rule",
Expiration: "",
Conditions: []Condition{
{
Type: "single",
Field: "ip",
Operator: "equals",
Value: "1.2.3.4",
},
},
Actions: []Action{
{
Type: "block",
ResponseCode: 499,
},
},
}
sc := NewTokenClient(testcreds.email, testcreds.token)
corp := testcreds.corp
site := testcreds.site
createResp, err := sc.CreateSiteRule(corp, site, createSiteRulesBody)
if err != nil {
t.Fatal(err)
}
if len(createResp.CreateSiteRuleBody.Actions) > 0 && createResp.CreateSiteRuleBody.Actions[0].ResponseCode != 499 {
t.Errorf("expected response code to be 499")
}
if !compareSiteRuleBody(createSiteRulesBody, createResp.CreateSiteRuleBody) {
t.Errorf("CreateSiteRulesgot: %v expected %v", createResp, createSiteRulesBody)
}

readResp, err := sc.GetSiteRuleByID(corp, site, createResp.ID)
if err != nil {
t.Fatal(err)
}
if !compareSiteRuleBody(createSiteRulesBody, readResp.CreateSiteRuleBody) {
t.Errorf("CreateSiteRulesgot: %v expected %v", createResp, createSiteRulesBody)
}
updateSiteRuleBody := CreateSiteRuleBody{
Type: "request",
GroupOperator: "all",
Enabled: true,
Reason: "Example site rule",
Expiration: "",
Conditions: []Condition{
{
Type: "single",
Field: "ip",
Operator: "equals",
Value: "1.2.3.4",
},
},
Actions: []Action{
{
Type: "block",
ResponseCode: 418,
},
},
}
updateResp, err := sc.UpdateSiteRuleByID(corp, site, createResp.ID, updateSiteRuleBody)
if err != nil {
t.Fatal(err)
}
if len(updateResp.CreateSiteRuleBody.Actions) > 0 && updateResp.CreateSiteRuleBody.Actions[0].ResponseCode != 418 {
t.Errorf("expected response code to be 418, I'm a teapot.")
}
if !compareSiteRuleBody(updateSiteRuleBody, updateResp.CreateSiteRuleBody) {
t.Errorf("CreateSiteRules got: %v expected %v", updateResp, updateSiteRuleBody)
}

err = sc.DeleteSiteRuleByID(corp, site, createResp.ID)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit ee1e001

Please sign in to comment.