Skip to content

Commit

Permalink
Merge pull request #36 from gocardless/dyson-allow-upload-user-delete…
Browse files Browse the repository at this point in the history
…-instance

Allow upload user to delete any instance
  • Loading branch information
dyson authored Jan 31, 2018
2 parents 266a9c2 + 685c63c commit 84b994c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Unreleased

No changes.

1.3.0
-----
Allow the upload user to delete any instance via API

1.2.0
-----

Expand Down
2 changes: 1 addition & 1 deletion DRAUPNIR_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.3.0
2 changes: 1 addition & 1 deletion routes/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (i Instances) Destroy(w http.ResponseWriter, r *http.Request) {
return
}

if email != instance.UserEmail {
if email != auth.UPLOAD_USER_EMAIL && email != instance.UserEmail {
RenderError(w, http.StatusNotFound, notFoundError)
return
}
Expand Down
42 changes: 42 additions & 0 deletions routes/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"testing"

"github.com/gocardless/draupnir/auth"
"github.com/gocardless/draupnir/models"
"github.com/google/jsonapi"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -331,3 +332,44 @@ func TestInstanceDestroyFromWrongUser(t *testing.T) {
assert.Equal(t, http.StatusNotFound, recorder.Code)
assert.Equal(t, notFoundError, response)
}

func TestInstanceDestroyFromUploadUser(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest("DELETE", "/instances/1", nil)

store := FakeInstanceStore{
_Get: func(id int) (models.Instance, error) {
return models.Instance{
ID: 1,
ImageID: 1,
Port: 5432,
CreatedAt: timestamp(),
UpdatedAt: timestamp(),
UserEmail: "test@draupnir",
}, nil
},
_Destroy: func(instance models.Instance) error {
return nil
},
}

executor := FakeExecutor{
_DestroyInstance: func(instanceID int) error {
return nil
},
}

authenticator := FakeAuthenticator{
_AuthenticateRequest: func(r *http.Request) (string, error) {
return auth.UPLOAD_USER_EMAIL, nil
},
}

routeSet := Instances{InstanceStore: store, Executor: executor, Authenticator: authenticator, Logger: log.NewNopLogger()}
router := mux.NewRouter()
router.HandleFunc("/instances/{id}", routeSet.Destroy).Methods("DELETE")
router.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusNoContent, recorder.Code)
assert.Equal(t, 0, len(recorder.Body.Bytes()))
}

0 comments on commit 84b994c

Please sign in to comment.