test, delete handler

pull/99/head
Karolis Rusenas 2017-09-26 15:15:54 +01:00
parent 9b7bff5a89
commit 3712c3f58c
3 changed files with 91 additions and 1 deletions

View File

@ -30,3 +30,17 @@ func (s *TriggerServer) approvalsHandler(resp http.ResponseWriter, req *http.Req
resp.Write(bts)
}
func (s *TriggerServer) approvalDeleteHandler(resp http.ResponseWriter, req *http.Request) {
identifier := getID(req)
err := s.approvalsManager.Delete(identifier)
if err != nil {
fmt.Fprintf(resp, "%s", err)
resp.WriteHeader(http.StatusInternalServerError)
return
}
resp.WriteHeader(http.StatusOK)
fmt.Fprintf(resp, identifier)
}

View File

@ -0,0 +1,72 @@
package http
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/rusenask/keel/approvals"
"github.com/rusenask/keel/cache/memory"
"github.com/rusenask/keel/provider"
"github.com/rusenask/keel/types"
"github.com/rusenask/keel/util/codecs"
)
func TestListApprovals(t *testing.T) {
fp := &fakeProvider{}
mem := memory.NewMemoryCache(100*time.Millisecond, 100*time.Millisecond, 10*time.Millisecond)
am := approvals.New(mem, codecs.DefaultSerializer())
providers := provider.New([]provider.Provider{fp}, am)
srv := NewTriggerServer(&Opts{Providers: providers, ApprovalManager: am})
srv.registerRoutes(srv.router)
err := am.Create(&types.Approval{
Identifier: "123",
VotesRequired: 5,
NewVersion: "2.0.0",
CurrentVersion: "1.0.0",
})
if err != nil {
t.Fatalf("failed to create approval: %s", err)
}
// listing
req, err := http.NewRequest("GET", "/v1/approvals", nil)
if err != nil {
t.Fatalf("failed to create req: %s", err)
}
rec := httptest.NewRecorder()
srv.router.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Errorf("unexpected status code: %d", rec.Code)
t.Log(rec.Body.String())
}
var approvals []*types.Approval
err = json.Unmarshal(rec.Body.Bytes(), &approvals)
if err != nil {
t.Fatalf("failed to unmarshal response into approvals: %s", err)
}
if len(approvals) != 1 {
t.Fatalf("expected to find 1 approval but found: %d", len(approvals))
}
if approvals[0].VotesRequired != 5 {
t.Errorf("unexpected votes required")
}
if approvals[0].NewVersion != "2.0.0" {
t.Errorf("unexpected new version: %s", approvals[0].NewVersion)
}
if approvals[0].CurrentVersion != "1.0.0" {
t.Errorf("unexpected current version: %s", approvals[0].CurrentVersion)
}
}

View File

@ -71,7 +71,10 @@ func (s *TriggerServer) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
s.server.Shutdown(ctx)
}
func getID(req *http.Request) string {
return mux.Vars(req)["id"]
}
func (s *TriggerServer) registerRoutes(mux *mux.Router) {
@ -80,8 +83,9 @@ func (s *TriggerServer) registerRoutes(mux *mux.Router) {
// version handler
mux.HandleFunc("/version", s.versionHandler).Methods("GET", "OPTIONS")
//
// approvals
mux.HandleFunc("/v1/approvals", s.approvalsHandler).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/approvals/{id}", s.approvalDeleteHandler).Methods("DELETE", "OPTIONS")
// native webhooks handler
mux.HandleFunc("/v1/webhooks/native", s.nativeHandler).Methods("POST", "OPTIONS")