added doormen ownership
This commit is contained in:
1 parent
f0628e4682
commit
f7a8d416c1
27 files changed
+489
-126
No files matched your search
+10
-4
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Create() http.Handler {
|
||||
func Create(root string) http.Handler {
|
||||
serverSpecification := &simpleapi.Endpoint{
|
||||
"/server",
|
||||
"Get the server specification.",
|
||||
@@ -29,26 +29,32 @@ func Create() http.Handler {
|
||||
|
||||
doorman := &simpleapi.Endpoint{
|
||||
"/doormen/{id}",
|
||||
"Get the doorman.",
|
||||
"Get a specific doorman by id.",
|
||||
"GET",
|
||||
GetDoorman,
|
||||
}
|
||||
|
||||
updateDoorman := &simpleapi.Endpoint{
|
||||
"/doormen/{id}",
|
||||
"Update the doorman with new probabilities.",
|
||||
`Update the doorman with new probabilities or owner emails. The payload
|
||||
must be a valid doorman with the same id than the targeted doorman.
|
||||
The values of the PUT doorman will replace the values of the old
|
||||
ones.`,
|
||||
"PUT",
|
||||
UpdateDoorman,
|
||||
}
|
||||
|
||||
api := simpleapi.New(
|
||||
"/api",
|
||||
root,
|
||||
serverSpecification,
|
||||
createDoorman,
|
||||
allDoorman,
|
||||
doorman,
|
||||
updateDoorman,
|
||||
)
|
||||
api.EnableDocumentation("documentation")
|
||||
api.AddMiddlewares(AuthenticationMiddleware)
|
||||
api.InitRouter()
|
||||
|
||||
return api
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gorilla/context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const userKey = "user"
|
||||
|
||||
func GetUser(req *http.Request) string {
|
||||
if rv := context.Get(req, userKey); rv != nil {
|
||||
return rv.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func SetUser(req *http.Request, user string) {
|
||||
context.Set(req, userKey, user)
|
||||
}
|
||||
|
||||
func AuthenticationMiddleware(h http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
query := req.URL.Query()
|
||||
user := query.Get("user")
|
||||
SetUser(req, user)
|
||||
h(w, req)
|
||||
context.Clear(req)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAuthentication(t *testing.T) {
|
||||
handler := func(w http.ResponseWriter, req *http.Request) {
|
||||
if user := GetUser(req); user != "natasha" {
|
||||
t.Error(user)
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "http://example.com/foo?user=natasha", nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
AuthenticationMiddleware(handler)(w, req)
|
||||
if user := GetUser(req); user != "" {
|
||||
t.Error(user)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,8 @@ func CreateDoorman(w http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
wdef.Id = bson.NewObjectId()
|
||||
if err := wdef.Validate(); err != nil {
|
||||
|
||||
if err := wdef.Validate(GetUser(request)); err != nil {
|
||||
w.WriteHeader(400)
|
||||
fmt.Fprintln(w, "New doorman is invalid \n", err)
|
||||
return
|
||||
|
||||
@@ -16,7 +16,11 @@ func GetIdFromLocation(p string) bson.ObjectId {
|
||||
|
||||
func TestCreateDoorman(t *testing.T) {
|
||||
conn = inmemoryconnector.NewInMemoryDBConnector()
|
||||
payload := `{"name":"name","values":[{"name":"red","probability":0.2},{"name":"blue","probability":0.8}]}`
|
||||
payload := `
|
||||
{"name":"name",
|
||||
"values":[{"name":"red","probability":0.2},{"name":"blue","probability":0.8}],
|
||||
"emails": ["alice"]
|
||||
}`
|
||||
req, err := http.NewRequest(
|
||||
"POST",
|
||||
"http://bigtits.com/api/doormen",
|
||||
@@ -26,6 +30,7 @@ func TestCreateDoorman(t *testing.T) {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
SetUser(req, "alice")
|
||||
w := httptest.NewRecorder()
|
||||
CreateDoorman(w, req)
|
||||
if w.Code != 201 {
|
||||
|
||||
@@ -22,7 +22,7 @@ func UpdateDoorman(w http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
wdef.Id = id
|
||||
|
||||
if err := wdef.Validate(); err != nil {
|
||||
if err := wdef.Validate(GetUser(request)); err != nil {
|
||||
Write400Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user