diff --git a/internal/models/area.go b/internal/models/area.go index 978d84699d61306c6ca5a63cc1a351ad0a76ced4..ed4a68c9d66503edf1800f779349b5c0e314b690 100644 --- a/internal/models/area.go +++ b/internal/models/area.go @@ -14,17 +14,15 @@ func (d *DataHandler) HandleArea(w http.ResponseWriter, r *http.Request) { switch method := r.Method; method { case "GET": switch auth.GetLoggedUserTechnical(w, r).Role { - case "ADMIN": - d.getAreaAdmin(w, r, id) - case "CAPTURER", "VISUALIZER": - http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed) + case "ADMIN", "CAPTURER", "VISUALIZER": + d.getArea(w, r, id) default: http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError) } case "POST": switch auth.GetLoggedUserTechnical(w, r).Role { case "ADMIN": - d.postAreaAdmin(w, r) + d.postArea(w, r) case "CAPTURER", "VISUALIZER": http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed) default: @@ -34,7 +32,7 @@ func (d *DataHandler) HandleArea(w http.ResponseWriter, r *http.Request) { case "PUT": switch auth.GetLoggedUserTechnical(w, r).Role { case "ADMIN": - d.putAreaAdmin(w, r, id) + d.putArea(w, r, id) case "CAPTURER", "VISUALIZER": http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed) default: @@ -43,7 +41,7 @@ func (d *DataHandler) HandleArea(w http.ResponseWriter, r *http.Request) { case "DELETE": switch auth.GetLoggedUserTechnical(w, r).Role { case "ADMIN": - d.deleteAreaAdmin(w, r, id) + d.deleteArea(w, r, id) case "CAPTURER", "VISUALIZER": http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed) default: @@ -54,7 +52,7 @@ func (d *DataHandler) HandleArea(w http.ResponseWriter, r *http.Request) { } } -func (d *DataHandler) getAreaAdmin(w http.ResponseWriter, r *http.Request, id int) { +func (d *DataHandler) getArea(w http.ResponseWriter, r *http.Request, id int) { if id != 0 { var o Area if err := d.db.Preload("Sections").First(&o, id).Error; err != nil { @@ -69,7 +67,7 @@ func (d *DataHandler) getAreaAdmin(w http.ResponseWriter, r *http.Request, id in } } -func (d *DataHandler) postAreaAdmin(w http.ResponseWriter, r *http.Request) { +func (d *DataHandler) postArea(w http.ResponseWriter, r *http.Request) { var o Area err := json.NewDecoder(r.Body).Decode(&o) if err != nil { @@ -89,7 +87,7 @@ func (d *DataHandler) postAreaAdmin(w http.ResponseWriter, r *http.Request) { } -func (d *DataHandler) putAreaAdmin(w http.ResponseWriter, r *http.Request, id int) { +func (d *DataHandler) putArea(w http.ResponseWriter, r *http.Request, id int) { var o Area if err := d.db.Preload("Sections").First(&o, id).Error; err != nil { http.Error(w, ErrorIDIsMissing, http.StatusNotFound) @@ -109,7 +107,7 @@ func (d *DataHandler) putAreaAdmin(w http.ResponseWriter, r *http.Request, id in } -func (d *DataHandler) deleteAreaAdmin(w http.ResponseWriter, r *http.Request, id int) { +func (d *DataHandler) deleteArea(w http.ResponseWriter, r *http.Request, id int) { if id != 0 { var o Area if err := d.db.First(&o, id).Error; err != nil { diff --git a/internal/models/election.go b/internal/models/election.go index f415e973dc920b0afa085193203ab9bafd81e236..6e8ea3fb4bf55e88839b00dede1c074476de522d 100644 --- a/internal/models/election.go +++ b/internal/models/election.go @@ -14,10 +14,8 @@ func (d *DataHandler) HandleElection(w http.ResponseWriter, r *http.Request) { switch method := r.Method; method { case "GET": switch auth.GetLoggedUserTechnical(w, r).Role { - case "ADMIN": + case "ADMIN", "CAPTURER", "VISUALIZER": d.getElectionAdmin(w, r, id) - case "CAPTURER", "VISUALIZER": - http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed) default: http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError) } diff --git a/internal/rootmux/capturer_test.go b/internal/rootmux/capturer_test.go index 1bd6e863198cbe3695cfb0a708dd22d2eec97064..3c2e872b808d00a5cd3fc1036077578cea80cf89 100644 --- a/internal/rootmux/capturer_test.go +++ b/internal/rootmux/capturer_test.go @@ -37,10 +37,10 @@ func CapturerTests(t *testing.T) { // Create an election should fail with 405 do("POST", "/api/Election", xsrfHeader, `{"Name":"Grand Lyon 2020", "BallotType":"metropolitan-direct"}`, 405, `You're not authorize to execute this method on this ressource.`) - // Get an Election should fail with 405 - do("GET", "/api/Election/1", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) - // Get all the elections should fail with 405 - do("GET", "/api/Election/", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) + // Get an Election + do("GET", "/api/Election/1", xsrfHeader, "", 200, `{"ID":1,"Name":"Grand Lyon 2020","BallotType":"metropolitan-direct","Areas":[{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":null}],"Rounds":null}`) + // Get all the elections + do("GET", "/api/Election/", xsrfHeader, "", 200, `[{"ID":1,"Name":"Grand Lyon 2020","BallotType":"metropolitan-direct","Areas":[{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":null}],"Rounds":null}]`) // Update an election should fail with 405 do("PUT", "/api/Election/1", xsrfHeader, `{"Name":"Grand Lyon 2020", "BallotType":"metropolitan-direct"}`, 405, `You're not authorize to execute this method on this ressource.`) // Delete an election should fail with 405 @@ -48,10 +48,10 @@ func CapturerTests(t *testing.T) { // Create an area should fail with 405 do("POST", "/api/Area", xsrfHeader, `{"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1"}`, 405, `You're not authorize to execute this method on this ressource.`) - // Get an area should fail with 405 - do("GET", "/api/Area/1", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) - // Get all the areas should fail with 405 - do("GET", "/api/Area/", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) + // Get an area + do("GET", "/api/Area/1", xsrfHeader, "", 200, `{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":[{"ID":1,"AreaID":1,"Name":"Section 1","MapID":"1","Desks":null}]}`) + // Get all the areas + do("GET", "/api/Area/", xsrfHeader, "", 200, `[{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":[{"ID":1,"AreaID":1,"Name":"Section 1","MapID":"1","Desks":null}]}]`) // Update an area should fail with 405 do("PUT", "/api/Area/1", xsrfHeader, `{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1"}`, 405, `You're not authorize to execute this method on this ressource.`) // Delete an area should fail with 405 diff --git a/internal/rootmux/visualizer_test.go b/internal/rootmux/visualizer_test.go index f7c3289ca925b9c0651802147ed36d6b59ac6c2c..e956018f6953a8d98d1ec8120311fa56647be389 100644 --- a/internal/rootmux/visualizer_test.go +++ b/internal/rootmux/visualizer_test.go @@ -35,10 +35,10 @@ func VisualizerTests(t *testing.T) { // Create an election should fail with 405 do("POST", "/api/Election", xsrfHeader, `{"Name":"Grand Lyon 2020", "BallotType":"metropolitan-direct"}`, 405, `You're not authorize to execute this method on this ressource.`) - // Get an Election should fail with 405 - do("GET", "/api/Election/1", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) - // Get all the elections should fail with 405 - do("GET", "/api/Election/", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) + // Get an Election + do("GET", "/api/Election/1", xsrfHeader, "", 200, `{"ID":1,"Name":"Grand Lyon 2020","BallotType":"metropolitan-direct","Areas":[{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":null}],"Rounds":null}`) + // Get all the elections + do("GET", "/api/Election/", xsrfHeader, "", 200, `[{"ID":1,"Name":"Grand Lyon 2020","BallotType":"metropolitan-direct","Areas":[{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":null}],"Rounds":null}]`) // Update an election should fail with 405 do("PUT", "/api/Election/1", xsrfHeader, `{"Name":"Grand Lyon 2020", "BallotType":"metropolitan-direct"}`, 405, `You're not authorize to execute this method on this ressource.`) // Delete an election should fail with 405 @@ -46,10 +46,10 @@ func VisualizerTests(t *testing.T) { // Create an area should fail with 405 do("POST", "/api/Area", xsrfHeader, `{"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1"}`, 405, `You're not authorize to execute this method on this ressource.`) - // Get an area should fail with 405 - do("GET", "/api/Area/1", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) - // Get all the areas should fail with 405 - do("GET", "/api/Area/", xsrfHeader, "", 405, `You're not authorize to execute this method on this ressource.`) + // Get an area + do("GET", "/api/Area/1", xsrfHeader, "", 200, `{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":[{"ID":1,"AreaID":1,"Name":"Section 1","MapID":"1","Desks":null}]}`) + // Get all the areas + do("GET", "/api/Area/", xsrfHeader, "", 200, `[{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1","Sections":[{"ID":1,"AreaID":1,"Name":"Section 1","MapID":"1","Desks":null}]}]`) // Update an area should fail with 405 do("PUT", "/api/Area/1", xsrfHeader, `{"ID":1,"ElectionID":1,"Name":"Area 1","SeatNumber":9,"MapID":"1"}`, 405, `You're not authorize to execute this method on this ressource.`) // Delete an area should fail with 405