Skip to content

Commit

Permalink
create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-goodisman committed Apr 4, 2024
1 parent 74370d6 commit 49833b5
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
102 changes: 102 additions & 0 deletions integration-tests/acceptance/denylist_http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
"reflect"
"testing"
)

func doRequest(method string, path string, body map[string]interface{}, t *testing.T, expectedCode int) interface{} {
jsonBody, err := json.Marshal(body)
if err != nil {
t.Fatalf("Error encoding req body as json: %s", err)
}
req, err := http.NewRequest(method, os.Getenv("OTR_URL")+path, bytes.NewBuffer(jsonBody))
if err != nil {
t.Fatalf("Error creating req: %s", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := (&http.Client{}).Do(req)
if err != nil {
t.Fatalf("Error sending request: %s", err)
}

defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error eceiving response body: %s", err)
}

if resp.StatusCode != expectedCode {
t.Fatalf("Expected status code %d, but got %d.\nBody was: %s", expectedCode, resp.StatusCode, respBody)
}

var data interface{}
err = json.Unmarshal(respBody, &data)
if err != nil {
t.Fatalf("Error parsing JSON response: %s", err)
}

return data
}

// Test the /denylist HTTP operations
func TestDenyList(t *testing.T) {
// GET empty list of rules
data := doRequest("GET", "/denylist", map[string]interface{}{}, t, 200)
if !reflect.DeepEqual(data, []string{}) {
t.Fatalf("Expected empty list from blank GET, but got %#v", data)
}
// PUT new rule
data = doRequest("PUT", "/denylist", map[string]interface{}{"keys": "a.b.c", "regex": "^abc$"}, t, 201)
id, ok := data.(string)
if !ok {
t.Fatalf("Expected string from PUT, but got %#v", id)
}
// GET list with new rule in it
data = doRequest("GET", "/denylist", map[string]interface{}{}, t, 200)
if !reflect.DeepEqual(data, []string{id}) {
t.Fatalf("Expected singleton from GET, but got %#v", data)
}
// GET existing rule
data = doRequest("GET", "/denylist/"+id, map[string]interface{}{}, t, 200)
if !reflect.DeepEqual(data, map[string]interface{}{
"keys": "a.b.c",
"regex": "^abc$",
}) {
t.Fatalf("Expected matched body from GET, but got %#v", data)
}
// PUT second rule
data = doRequest("PUT", "/denylist", map[string]interface{}{"keys": "d.e.f", "regex": "^def$"}, t, 201)
id2, ok := data.(string)
if !ok {
t.Fatalf("Expected string from PUT, but got %#v", id)
}
// GET second rule
data = doRequest("GET", "/denylist/"+id2, map[string]interface{}{}, t, 200)
if !reflect.DeepEqual(data, map[string]interface{}{
"keys": "d.e.f",
"regex": "^def$",
}) {
t.Fatalf("Expected matched body from GET, but got %#v", data)
}
// GET list with both rules
data = doRequest("GET", "/denylist", map[string]interface{}{}, t, 200)
if !reflect.DeepEqual(data, []string{id, id2}) {
t.Fatalf("Expected doubleton from GET, but got %#v", data)
}
// DELETE first rule
doRequest("DELETE", "/denylist/"+id, map[string]interface{}{}, t, 204)
// GET first rule
doRequest("GET", "/denylist/"+id, map[string]interface{}{}, t, 404)
// GET list with only second rule
data = doRequest("GET", "/denylist", map[string]interface{}{}, t, 200)
if !reflect.DeepEqual(data, []string{id2}) {
t.Fatalf("Expected singleton from GET, but got %#V", data)
}
}
23 changes: 23 additions & 0 deletions integration-tests/acceptance/denylist_oplog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"testing"
)

func TestDenyOplog(t *testing.T) {
// TODO

// start harness
// send message
// confirm message arrived

// create denylist
// send message
// confirm message did NOT arrive

// remove denylist
// confirm message STILL did not arrive

// send message
// confirm message arrived
}

0 comments on commit 49833b5

Please sign in to comment.