Skip to content

Commit 1f3e4b7

Browse files
committed
test: added test case for auditing resource
1 parent 8ebd1d7 commit 1f3e4b7

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

mongodbatlas/auditings_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package mongodbatlas
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/go-test/deep"
10+
"github.com/mwielbut/pointy"
11+
)
12+
13+
func TestConfigureAuditing(t *testing.T) {
14+
setup()
15+
defer teardown()
16+
17+
groupID := "6d2065c687d9d64ae7acdg41"
18+
19+
mux.HandleFunc(fmt.Sprintf("/"+auditingsPath, groupID), func(w http.ResponseWriter, r *http.Request) {
20+
expected := map[string]interface{}{
21+
"auditAuthorizationSuccess": false,
22+
"auditFilter": "{\n \"atype\": \"authenticate\",\n \"param\": {\n \"user\": \"auditAdmin\",\n \"db\": \"admin\",\n \"mechanism\": \"SCRAM-SHA-1\"\n }\n}",
23+
"enabled": true,
24+
}
25+
26+
var v map[string]interface{}
27+
err := json.NewDecoder(r.Body).Decode(&v)
28+
if err != nil {
29+
t.Fatalf("decode json: %v", err)
30+
}
31+
32+
if diff := deep.Equal(v, expected); diff != nil {
33+
t.Errorf("Request body\n got=%#v\nwant=%#v \n\ndiff=%#v", v, expected, diff)
34+
}
35+
36+
fmt.Fprint(w, `{
37+
"auditAuthorizationSuccess": false,
38+
"auditFilter": "{\n \"atype\": \"authenticate\",\n \"param\": {\n \"user\": \"auditAdmin\",\n \"db\": \"admin\",\n \"mechanism\": \"SCRAM-SHA-1\"\n }\n}",
39+
"configurationType": "FILTER_JSON",
40+
"enabled": true
41+
}`)
42+
})
43+
44+
auditingRequest := &Auditing{
45+
AuditAuthorizationSuccess: pointy.Bool(false),
46+
AuditFilter: "{\n \"atype\": \"authenticate\",\n \"param\": {\n \"user\": \"auditAdmin\",\n \"db\": \"admin\",\n \"mechanism\": \"SCRAM-SHA-1\"\n }\n}",
47+
Enabled: pointy.Bool(true),
48+
}
49+
50+
auditingResponse, _, err := client.Auditings.Configure(ctx, groupID, auditingRequest)
51+
if err != nil {
52+
t.Errorf("Auditings.Configure returned error: %v", err)
53+
return
54+
}
55+
56+
expected := &Auditing{
57+
AuditAuthorizationSuccess: pointy.Bool(false),
58+
AuditFilter: "{\n \"atype\": \"authenticate\",\n \"param\": {\n \"user\": \"auditAdmin\",\n \"db\": \"admin\",\n \"mechanism\": \"SCRAM-SHA-1\"\n }\n}",
59+
ConfigurationType: "FILTER_JSON",
60+
Enabled: pointy.Bool(true),
61+
}
62+
63+
if diff := deep.Equal(auditingResponse, expected); diff != nil {
64+
t.Errorf("Request body\n got=%#v\nwant=%#v \n\ndiff=%#v", auditingResponse, expected, diff)
65+
}
66+
}
67+
68+
func TestAuditings_Get(t *testing.T) {
69+
setup()
70+
defer teardown()
71+
72+
groupID := "6d2065c687d9d64ae7acdg41"
73+
74+
mux.HandleFunc(fmt.Sprintf("/"+auditingsPath, groupID), func(w http.ResponseWriter, r *http.Request) {
75+
testMethod(t, r, http.MethodGet)
76+
fmt.Fprint(w, fmt.Sprintf(`{
77+
"auditAuthorizationSuccess": false,
78+
"auditFilter": "{\n \"atype\": \"authenticate\",\n \"param\": {\n \"user\": \"auditAdmin\",\n \"db\": \"admin\",\n \"mechanism\": \"SCRAM-SHA-1\"\n }\n}",
79+
"configurationType": "FILTER_JSON",
80+
"enabled": true
81+
}`))
82+
})
83+
84+
auditing, _, err := client.Auditings.Get(ctx, groupID)
85+
if err != nil {
86+
t.Errorf("Auditings.Get returned error: %v", err)
87+
}
88+
89+
expected := &Auditing{
90+
AuditAuthorizationSuccess: pointy.Bool(false),
91+
AuditFilter: "{\n \"atype\": \"authenticate\",\n \"param\": {\n \"user\": \"auditAdmin\",\n \"db\": \"admin\",\n \"mechanism\": \"SCRAM-SHA-1\"\n }\n}",
92+
ConfigurationType: "FILTER_JSON",
93+
Enabled: pointy.Bool(true),
94+
}
95+
96+
if diff := deep.Equal(auditing, expected); diff != nil {
97+
t.Errorf("Request body\n got=%#v\nwant=%#v \n\ndiff=%#v", auditing, expected, diff)
98+
}
99+
}

0 commit comments

Comments
 (0)