-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathresource_response_mutation_test.go
More file actions
67 lines (60 loc) · 1.89 KB
/
resource_response_mutation_test.go
File metadata and controls
67 lines (60 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package scim_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/elimity-com/scim"
"github.com/elimity-com/scim/schema"
)
// TestResponseDoesNotMutateHandlerAttributes verifies that serving a GET request
// does not modify the attributes map stored inside the resource handler.
//
// Resource.response() previously did:
//
// response := r.Attributes // reference copy, not a value copy
// response["id"] = r.ID // writes back into the handler's stored map
//
// The result: a second GET on the same resource would find framework-injected
// keys ("id", "schemas", "meta") already present in the attributes, leading to
// duplicate or stale data in the response.
func TestResponseDoesNotMutateHandlerAttributes(t *testing.T) {
handler := &testResourceHandler{
data: map[string]testData{
"0001": {
attributes: scim.ResourceAttributes{
"userName": "alice",
},
},
},
schema: schema.CoreUserSchema(),
}
s, err := scim.NewServer(&scim.ServerArgs{
ServiceProviderConfig: &scim.ServiceProviderConfig{},
ResourceTypes: []scim.ResourceType{
{
Name: "User",
Endpoint: "/Users",
Schema: schema.CoreUserSchema(),
Handler: handler,
},
},
})
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/Users/0001", nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
// After the GET, the handler's stored map must not contain any of the
// framework-injected keys. If response() mutated the map, these will be
// present and a subsequent GET would return them as user-supplied data.
stored := handler.data["0001"].attributes
for _, key := range []string{"id", "schemas", "meta"} {
if _, ok := stored[key]; ok {
t.Errorf("Resource.response() mutated handler attributes: key %q was injected into the stored map", key)
}
}
}