Skip to content
Snippets Groups Projects
Commit aeac8c83 authored by Alexis Poyen's avatar Alexis Poyen
Browse files

WIP : capturer API

parent 204ea8c3
No related branches found
No related tags found
1 merge request!4Resolve "API user capturer"
Pipeline #5293 failed
package models
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"forge.grandlyon.com/apoyen/elections/internal/auth"
)
// HandleCapturer handle API calls on Capturer
func (d *DataHandler) HandleCapturer(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(strings.TrimPrefix(r.URL.Path, "/api/Capturer/"))
switch method := r.Method; method {
case "GET":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN":
d.getCapturerAdmin(w, r, id)
case "CAPTURER":
d.getCapturerCapturer(w, r, id)
case "VISUALIZER":
http.Error(w, ErrorCannotAccessRessource, http.StatusForbidden)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
case "POST":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN":
d.postCapturerAdmin(w, r)
case "CAPTURER", "VISUALIZER":
http.Error(w, ErrorCannotAccessRessource, http.StatusForbidden)
}
case "PUT":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN":
case "CAPTURER":
case "VISUALIZER":
}
case "DELETE":
switch auth.GetLoggedUserTechnical(w, r).Role {
case "ADMIN":
case "CAPTURER":
case "VISUALIZER":
}
default:
http.Error(w, "method not allowed", 400)
}
}
func (d *DataHandler) getCapturerAdmin(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Capturer
if err := d.db.Preload("DeskRounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(o)
} else {
var o []Capturer
d.db.Preload("DeskRounds").Find(&o)
json.NewEncoder(w).Encode(o)
}
}
func (d *DataHandler) getCapturerCapturer(w http.ResponseWriter, r *http.Request, id int) {
user := d.getLoggedUser(w, r).(Capturer)
fmt.Println(user)
if id != 0 {
var o Capturer
if err := d.db.Preload("DeskRounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
// fmt.Println(o)
if o.UserID != user.UserID {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
return
}
json.NewEncoder(w).Encode(o)
} else {
var o []Capturer
d.db.Preload("DeskRounds").Where("id = ?", user.ID).Find(&o)
json.NewEncoder(w).Encode(o)
}
}
func (d *DataHandler) postCapturerAdmin(w http.ResponseWriter, r *http.Request) {
var o Capturer
err := json.NewDecoder(r.Body).Decode(&o)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var capturer Capturer
if err := d.db.Where("user_id = ?", o.UserID).First(&capturer).Error; err == nil {
http.Error(w, "UserID is already bind to a Capturer", http.StatusInternalServerError)
return
}
d.db.Create(&o)
d.db.Last(&o)
json.NewEncoder(w).Encode(o)
}
......@@ -23,13 +23,13 @@ func AdminTests(t *testing.T) {
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
// Create a capturer
do("POST", "/api/UserCapturer", xsrfHeader, `{"userID":2,"name":"Capturer"}`, 200, ``)
do("POST", "/api/Capturer", xsrfHeader, `{"UserID":1,"Name":"Capturer"}`, 200, `{"ID":3,"UserID":1,"Name":"Capturer","DeskRounds":null}`)
// Get the capturer
do("GET", "/api/UserCapturer/1", xsrfHeader, "", 200, `{"ID":1,"userID":2,"name":"Capturer","deskRounds":[]}`)
do("GET", "/api/Capturer/1", xsrfHeader, "", 200, `{"ID":1,"UserID":2,"Name":"Capturer","DeskRounds":[]}`)
// Get all the capturer
do("GET", "/api/UserCapturer/", xsrfHeader, "", 200, `[{"ID":1,"userID":2,"name":"Capturer","deskRounds":[]}]`)
do("GET", "/api/Capturer/", xsrfHeader, "", 200, `[{"ID":1,"UserID":2,"Name":"Capturer","DeskRounds":[]},{"ID":2,"UserID":3,"Name":"Capturer","DeskRounds":[]},{"ID":3,"UserID":1,"Name":"Capturer","DeskRounds":[]}]`)
// Delete a capturer
do("DELETE", "/api/UserCapturer/2", xsrfHeader, ``, 200, ``)
do("DELETE", "/api/Capturer/3", xsrfHeader, ``, 200, ``)
}
// Do a in memory login with an known admin
......
......@@ -23,19 +23,19 @@ func CapturerTests(t *testing.T) {
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
// Create a capturer should fail with 405
do("POST", "/api/UserCapturer", xsrfHeader, `{"userID":2,"name":"Capturer"}`, 405, `You're not authorize to execute this method on this ressource.`)
do("POST", "/api/Capturer", xsrfHeader, `{"userID":2,"name":"Capturer"}`, 403, `You can not access this ressource`)
// Get the capturer connected
do("GET", "/api/UserCapturer/1", xsrfHeader, "", 200, `{"ID":1,"userID":2,"name":"Capturer","deskRounds":[]}`)
do("GET", "/api/Capturer/1", xsrfHeader, "", 200, `{"ID":1,"UserID":2,"Name":"Capturer","DeskRounds":[]}`)
// Get another capturer should fail with 405
do("GET", "/api/UserCapturer/2", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`)
do("GET", "/api/Capturer/2", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`)
// Get all the capturer return only the capturer connected
do("GET", "/api/UserCapturer/", xsrfHeader, "", 200, `[{"ID":1,"userID":2,"name":"Capturer","deskRounds":[]}]`)
do("GET", "/api/Capturer/", xsrfHeader, "", 200, `[{"ID":1,"UserID":2,"Name":"Capturer","DeskRounds":[]}]`)
// Delete a capturer should fail
do("DELETE", "/api/UserCapturer/1", xsrfHeader, ``, 405, `You're not authorize to execute this method on this ressource.`)
do("DELETE", "/api/Capturer/1", xsrfHeader, ``, 405, `You're not authorize to execute this method on this ressource.`)
}
// Do a in memory login with an known admin
do("POST", "/Login", noH, `{"login": "admin","password": "password"}`, 200, "")
do("POST", "/Login", noH, `{"login": "capturer","password": "password"}`, 200, "")
tests()
// Try to logout (must pass)
do("GET", "/Logout", noH, "", 200, "Logout OK")
......
......@@ -59,8 +59,8 @@ func TestAll(t *testing.T) {
AdminTests(t)
resetData(t)
CapturerTests(t)
resetData(t)
VisualizerTests(t)
// resetData(t)
// VisualizerTests(t)
os.RemoveAll("./data")
}
......@@ -85,8 +85,9 @@ func appTests(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
// xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
do("POST", "/api/Capturer", xsrfHeader, `{"UserID":2,"Name":"Capturer"}`, 500, `UserID is already bind to a Capturer`)
}
// Do an OAuth2 login with an known admin
do("GET", "/OAuth2Login", noH, "", 200, "<!DOCTYPE html>")
......@@ -113,7 +114,8 @@ func resetData(t *testing.T) {
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
// Create a capturer
do("POST", "/api/UserCapturer", xsrfHeader, `{"userID":2,"name":"Capturer"}`, 200, ``)
do("POST", "/api/Capturer", xsrfHeader, `{"UserID":2,"Name":"Capturer"}`, 200, `{"ID":1,"UserID":2,"Name":"Capturer","DeskRounds":null}`)
do("POST", "/api/Capturer", xsrfHeader, `{"UserID":3,"Name":"Capturer"}`, 200, `{"ID":2,"UserID":3,"Name":"Capturer","DeskRounds":null}`)
}
do("POST", "/Login", noH, `{"login": "admin","password": "password"}`, 200, "")
init()
......
......@@ -23,17 +23,17 @@ func VisualizerTests(t *testing.T) {
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
// Create a capturer should fail with 405
do("POST", "/api/UserCapturer", xsrfHeader, `{"userID":2,"name":"Capturer"}`, 405, `You're not authorize to execute this method on this ressource.`)
do("POST", "/api/Capturer", xsrfHeader, `{"userID":2,"name":"Capturer"}`, 405, `You're not authorize to execute this method on this ressource.`)
// Get a capturer should fail with 405
do("GET", "/api/UserCapturer/1", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`)
do("GET", "/api/Capturer/1", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`)
// Get all the capturer should fail with 405
do("GET", "/api/UserCapturer/", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`)
do("GET", "/api/Capturer/", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`)
// Delete a capturer should fail
do("DELETE", "/api/UserCapturer/1", xsrfHeader, ``, 405, `You're not authorize to execute this method on this ressource.`)
do("DELETE", "/api/Capturer/1", xsrfHeader, ``, 405, `You're not authorize to execute this method on this ressource.`)
}
// Do a in memory login with an known admin
do("POST", "/Login", noH, `{"login": "admin","password": "password"}`, 200, "")
do("POST", "/Login", noH, `{"login": "visualizer","password": "password"}`, 200, "")
tests()
// Try to logout (must pass)
do("GET", "/Logout", noH, "", 200, "Logout OK")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment