func TestClear(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } s, err := mgo.Dial("") if !assert.NoError(t, err) { return } defer cleanup(s, "testupdate")() h := NewHandler(s, "testupdate", "test") items := []*resource.Item{ {ID: "1", Payload: map[string]interface{}{"id": "1", "name": "a"}}, {ID: "2", Payload: map[string]interface{}{"id": "2", "name": "b"}}, {ID: "3", Payload: map[string]interface{}{"id": "3", "name": "c"}}, {ID: "4", Payload: map[string]interface{}{"id": "4", "name": "d"}}, } err = h.Insert(context.Background(), items) assert.NoError(t, err) lookup := resource.NewLookupWithQuery(schema.Query{ schema.In{Field: "name", Values: []schema.Value{"c", "d"}}, }) deleted, err := h.Clear(context.Background(), lookup) assert.NoError(t, err) assert.Equal(t, 2, deleted) lookup = resource.NewLookupWithQuery(schema.Query{ schema.Equal{Field: "id", Value: "2"}, }) deleted, err = h.Clear(context.Background(), lookup) assert.NoError(t, err) assert.Equal(t, 1, deleted) }
func TestHandlerPutItem(t *testing.T) { i := resource.NewIndex() s := mem.NewHandler() i.Bind("foo", schema.Schema{Fields: schema.Fields{"id": {}, "name": {}}}, s, resource.DefaultConf) h, _ := NewHandler(i) w := httptest.NewRecorder() r, _ := http.NewRequest("PUT", "/foo/1", bytes.NewBufferString(`{"name": "test"}`)) h.ServeHTTP(w, r) assert.Equal(t, 201, w.Code) b, _ := ioutil.ReadAll(w.Body) assert.Equal(t, "{\"id\":\"1\",\"name\":\"test\"}", string(b)) lkp := resource.NewLookupWithQuery(schema.Query{schema.Equal{Field: "id", Value: "1"}}) l, err := s.Find(context.TODO(), lkp, 1, 1) assert.NoError(t, err) assert.Len(t, l.Items, 1) }
func TestHandlerPatchItem(t *testing.T) { i := resource.NewIndex() s := mem.NewHandler() s.Insert(context.TODO(), []*resource.Item{ {ID: "1", Payload: map[string]interface{}{"id": "1", "foo": "bar", "bar": "baz"}}, }) i.Bind("foo", schema.Schema{Fields: schema.Fields{"id": {}, "foo": {}, "bar": {}}}, s, resource.DefaultConf) h, _ := NewHandler(i) w := httptest.NewRecorder() r, _ := http.NewRequest("PATCH", "/foo/1", bytes.NewBufferString(`{"foo": "baz"}`)) h.ServeHTTP(w, r) assert.Equal(t, 200, w.Code) b, _ := ioutil.ReadAll(w.Body) assert.Equal(t, `{"bar":"baz","foo":"baz","id":"1"}`, string(b)) lkp := resource.NewLookupWithQuery(schema.Query{schema.Equal{Field: "id", Value: "1"}}) l, err := s.Find(context.TODO(), lkp, 1, 1) assert.NoError(t, err) if assert.Len(t, l.Items, 1) { assert.Equal(t, map[string]interface{}{"id": "1", "foo": "baz", "bar": "baz"}, l.Items[0].Payload) } }
func (m myAuthMiddleware) Handle(ctx context.Context, r *http.Request, next rest.Next) (context.Context, int, http.Header, interface{}) { if u, p, ok := r.BasicAuth(); ok { // Lookup the user by its id lookup := resource.NewLookupWithQuery(schema.Query{ schema.Equal{Field: "id", Value: u}, }) list, err := m.userResource.Find(ctx, lookup, 1, 1) if err != nil { // If user resource storage handler returned an error, stop the middleware chain return ctx, 0, nil, err } if len(list.Items) == 1 { user := list.Items[0] if schema.VerifyPassword(user.Payload["password"], []byte(p)) { // Get the current route from the context route, ok := rest.RouteFromContext(ctx) if ok { // If the current resource is "users", set the resource field to "id" // as user resource doesn't reference itself thru a "user" field. field := "user" if route.ResourcePath.Path() == "users" { field = "id" } // Prepent the resource path with the user resource route.ResourcePath.Prepend(m.userResource, field, u) // Go the the next middleware return next(ctx) } } } } // Stop the middleware chain and return a 401 HTTP error headers := http.Header{} headers.Set("WWW-Authenticate", "Basic realm=\"API\"") return ctx, 401, headers, &rest.Error{401, "Please provide proper credentials", nil} }
func TestFind(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } s, err := mgo.Dial("") if !assert.NoError(t, err) { return } defer cleanup(s, "testfind")() h := NewHandler(s, "testfind", "test") h2 := NewHandler(s, "testfind", "test2") items := []*resource.Item{ {ID: "1", Payload: map[string]interface{}{"id": "1", "name": "a", "age": 1}}, {ID: "2", Payload: map[string]interface{}{"id": "2", "name": "b", "age": 2}}, {ID: "3", Payload: map[string]interface{}{"id": "3", "name": "c", "age": 3}}, {ID: "4", Payload: map[string]interface{}{"id": "4", "name": "d", "age": 4}}, } ctx := context.Background() assert.NoError(t, h.Insert(ctx, items)) assert.NoError(t, h2.Insert(ctx, items)) lookup := resource.NewLookup() l, err := h.Find(ctx, lookup, 1, -1) if assert.NoError(t, err) { assert.Equal(t, 1, l.Page) assert.Equal(t, -1, l.Total) // Mongo doesn't support total counting assert.Len(t, l.Items, 4) // Do not check result's content as its order is unpredictable } lookup = resource.NewLookupWithQuery(schema.Query{ schema.Equal{Field: "name", Value: "c"}, }) l, err = h.Find(ctx, lookup, 1, 100) if assert.NoError(t, err) { assert.Equal(t, 1, l.Page) assert.Equal(t, -1, l.Total) // Mongo doesn't support total counting if assert.Len(t, l.Items, 1) { item := l.Items[0] assert.Equal(t, "3", item.ID) assert.Equal(t, map[string]interface{}{"id": "3", "name": "c", "age": 3}, item.Payload) } } lookup = resource.NewLookupWithQuery(schema.Query{ schema.In{Field: "name", Values: []schema.Value{"c", "d"}}, }) lookup.SetSorts([]string{"name"}) l, err = h.Find(ctx, lookup, 1, 100) if assert.NoError(t, err) { assert.Equal(t, 1, l.Page) assert.Equal(t, -1, l.Total) // Mongo doesn't support total counting if assert.Len(t, l.Items, 2) { item := l.Items[0] assert.Equal(t, "3", item.ID) assert.Equal(t, map[string]interface{}{"id": "3", "name": "c", "age": 3}, item.Payload) item = l.Items[1] assert.Equal(t, "4", item.ID) assert.Equal(t, map[string]interface{}{"id": "4", "name": "d", "age": 4}, item.Payload) } } lookup = resource.NewLookupWithQuery(schema.Query{ schema.Equal{Field: "id", Value: "3"}, }) l, err = h.Find(ctx, lookup, 1, 1) if assert.NoError(t, err) { assert.Equal(t, 1, l.Page) assert.Equal(t, -1, l.Total) // Mongo doesn't support total counting if assert.Len(t, l.Items, 1) { item := l.Items[0] assert.Equal(t, "3", item.ID) assert.Equal(t, map[string]interface{}{"id": "3", "name": "c", "age": 3}, item.Payload) } } lookup = resource.NewLookupWithQuery(schema.Query{ schema.Equal{Field: "id", Value: "10"}, }) l, err = h.Find(ctx, lookup, 1, 1) if assert.NoError(t, err) { assert.Equal(t, 1, l.Page) assert.Equal(t, -1, l.Total) // Mongo doesn't support total counting assert.Len(t, l.Items, 0) } lookup = resource.NewLookupWithQuery(schema.Query{ schema.In{Field: "id", Values: []schema.Value{"3", "4", "10"}}, }) l, err = h.Find(ctx, lookup, 1, -1) if assert.NoError(t, err) { assert.Equal(t, 1, l.Page) assert.Equal(t, -1, l.Total) // Mongo doesn't support total counting assert.Len(t, l.Items, 2) } }