Fix modifiers

This commit is contained in:
Andrea Fazzi 2020-01-24 10:55:01 +01:00
parent ca17d60709
commit 29da5958f8
5 changed files with 94 additions and 65 deletions

View file

@ -72,8 +72,8 @@ func requestToken(handlers *Handlers, username string, password string) string {
}
func deleteParticipant(id uint) {
req, err := handlers.NewDeleteRequest(&orm.Participant{}, fmt.Sprintf("/participants/%d/delete", id), "json")
func deleteParticipant(id uint) int {
req, err := handlers.NewDeleteRequest(&orm.Participant{}, fmt.Sprintf("/participants/%d/delete", id), "html")
if err != nil {
panic(err)
}
@ -88,6 +88,28 @@ func deleteParticipant(id uint) {
router := mux.NewRouter()
router.Handle("/participants/{id}/delete", handlers.Delete(&orm.Participant{}))
router.ServeHTTP(rr, req)
return rr.Code
}
func deleteSchool(id uint) int {
req, err := handlers.NewDeleteRequest(&orm.School{}, fmt.Sprintf("/schools/%d/delete", id), "html")
if err != nil {
panic(err)
}
req, err = login(req, handlers, "admin", "admin")
if err != nil {
panic(err)
}
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/schools/{id}/delete", handlers.Delete(&orm.School{}))
router.ServeHTTP(rr, req)
return rr.Code
}
func TestRunner(t *testing.T) {
@ -250,34 +272,36 @@ func (t *testSuite) TestSchoolSubscriptionForm() {
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/create/", handlers.Read(&orm.School{}))
router.Handle("/schools/create", handlers.Create(&orm.School{}))
router.ServeHTTP(rr, req)
t.Equal(http.StatusOK, rr.Code)
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
log.Fatal(err)
}
expected := []string{
"Denominazione dell'istituto",
"Codice meccanografico",
"Indirizzo dell'istituto",
"Regione",
"Indirizzo email",
"Nome del referente di sede",
"Cognome del referente di sede",
"Nome del responsabile di gara",
"Cognome del responsabile di gara",
}
ok := true
doc.Find(".control-label").Each(func(i int, s *goquery.Selection) {
t.Equal(expected[i], s.Text())
if t.Failed() {
ok = false
if !t.Failed() {
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
log.Fatal(err)
}
})
t.True(ok)
expected := []string{
"Denominazione dell'istituto",
"Codice meccanografico",
"Indirizzo dell'istituto",
"Regione",
"Indirizzo email",
"Nome del referente di sede",
"Cognome del referente di sede",
"Nome del responsabile di gara",
"Cognome del responsabile di gara",
}
ok := true
doc.Find(".control-label").Each(func(i int, s *goquery.Selection) {
t.Equal(expected[i], s.Text())
if t.Failed() {
ok = false
}
})
t.True(ok)
}
}
@ -298,7 +322,7 @@ func (t *testSuite) TestSchoolSubscription() {
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/create/", handlers.Create(&orm.School{}))
router.Handle("/schools/create/", handlers.Create(&orm.School{}))
router.ServeHTTP(rr, req)
t.Equal(http.StatusSeeOther, rr.Code)
@ -358,8 +382,10 @@ func (t *testSuite) TestSchoolSubscription() {
t.Equal(http.StatusSeeOther, rr.Code)
deleteParticipant(1001)
t.Equal(http.StatusOK, deleteParticipant(1001))
}
t.Equal(http.StatusOK, deleteSchool(501))
}

View file

@ -211,7 +211,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
return nil, errors.CategoryExists
}
participant.UserModifierCreate = NewUserModifierCreate(db, r)
participant.UserModifierCreate = NewUserModifierCreate(r)
participant, err = CreateParticipant(db, participant)
if err != nil {
@ -226,7 +226,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
return nil, err
}
response.UserModifierCreate = NewUserModifierCreate(db, r)
response.UserModifierCreate = NewUserModifierCreate(r)
if err := db._db.Save(&response).Error; err != nil {
return nil, err
@ -261,6 +261,9 @@ func (model *Participant) Read(db *Database, args map[string]string, w http.Resp
if err := db._db.Preload("User").Preload("School").Preload("Responses").Preload("Contests").Preload("Category").First(&participant, id).Error; err != nil {
return nil, err
}
participant.UserModifierCreate.get(db)
participant.UserModifierUpdate.get(db)
}
return &participant, nil
@ -366,7 +369,7 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re
return nil, err
}
participant.(*Participant).UserModifierUpdate = NewUserModifierUpdate(db, r)
participant.(*Participant).UserModifierUpdate = NewUserModifierUpdate(r)
_, err = SaveParticipant(db, participant)
if err != nil {
@ -390,7 +393,7 @@ func (model *Participant) Update(db *Database, args map[string]string, w http.Re
return nil, err
}
response.UserModifierUpdate = NewUserModifierUpdate(db, r)
response.UserModifierUpdate = NewUserModifierUpdate(r)
if err := db._db.Save(&response).Error; err != nil {
return nil, err

View file

@ -94,7 +94,7 @@ func (model *Response) Create(db *Database, args map[string]string, w http.Respo
return nil, err
}
response.UserModifierCreate = NewUserModifierCreate(db, r)
response.UserModifierCreate = NewUserModifierCreate(r)
response, err = CreateResponse(db, response)
if err != nil {
@ -211,7 +211,7 @@ func (model *Response) Update(db *Database, args map[string]string, w http.Respo
return nil, err
}
response.(*Response).UserModifierUpdate = NewUserModifierUpdate(db, r)
response.(*Response).UserModifierUpdate = NewUserModifierUpdate(r)
_, err = SaveResponse(db, response)
if err != nil {

View file

@ -2,7 +2,6 @@ package orm
import (
"fmt"
"log"
"net/http"
"strings"
"time"
@ -171,7 +170,7 @@ func (model *School) Create(db *Database, args map[string]string, w http.Respons
return nil, err
}
school.UserModifierCreate = NewUserModifierCreate(db, r)
school.UserModifierCreate = NewUserModifierCreate(r)
school.mailSender = mail.NewMailSender(db.Config, mailBody)
school, err = CreateSchool(db, school)
@ -193,7 +192,6 @@ func (model *School) Read(db *Database, args map[string]string, w http.ResponseW
}
if err := db._db.Preload("User").Preload("Region").Preload("Participants.Category").Preload("Participants").First(&school, id).Error; err != nil {
log.Println("ARGS", err)
return nil, err
}
@ -248,7 +246,7 @@ func (model *School) Update(db *Database, args map[string]string, w http.Respons
return nil, err
}
school.(*School).UserModifierUpdate = NewUserModifierUpdate(db, r)
school.(*School).UserModifierUpdate = NewUserModifierUpdate(r)
_, err = SaveSchool(db, school)
if err != nil {

View file

@ -1,6 +1,7 @@
package orm
import (
"errors"
"net/http"
"github.com/dgrijalva/jwt-go"
@ -18,7 +19,7 @@ type UserModifierCreate struct {
CreatorRole string
CreatorIP string
db *Database
CreatorUser *User
}
type UserModifierUpdate struct {
@ -26,10 +27,10 @@ type UserModifierUpdate struct {
UpdaterRole string
UpdaterIP string
db *Database
UpdaterUser *User
}
func NewUserModifierCreate(db *Database, r *http.Request) *UserModifierCreate {
func NewUserModifierCreate(r *http.Request) *UserModifierCreate {
var claims jwt.MapClaims
if r.Context().Value("user") != nil {
@ -43,35 +44,36 @@ func NewUserModifierCreate(db *Database, r *http.Request) *UserModifierCreate {
}
}
func (um *UserModifierCreate) CreatedBy() (*UserAction, error) {
action := new(UserAction)
func (um *UserModifierCreate) CreatedBy() *User {
return um.CreatorUser
}
func (um *UserModifierCreate) get(db *Database) error {
switch um.CreatorRole {
case "participant":
var participant Participant
if err := um.db._db.Preload("User").First(&participant, um.CreatorID).Error; err != nil {
return nil, err
if err := db._db.Preload("User").First(&participant, um.CreatorID).Error; err != nil {
return err
}
action.User = *participant.User
um.CreatorUser = participant.User
case "school":
var school School
if err := um.db._db.Preload("User").First(&school, um.CreatorID).Error; err != nil {
return nil, err
if err := db._db.Preload("User").First(&school, um.CreatorID).Error; err != nil {
return err
}
action.User = *school.User
um.CreatorUser = school.User
case "subscriber":
action.User = SubscriberUser
um.CreatorUser = &SubscriberUser
case "administrator":
action.User = AdministratorUser
um.CreatorUser = &AdministratorUser
default:
return nil, nil
return errors.New("Undefined user!")
}
return action, nil
return nil
}
func NewUserModifierUpdate(db *Database, r *http.Request) *UserModifierUpdate {
func NewUserModifierUpdate(r *http.Request) *UserModifierUpdate {
var claims jwt.MapClaims
if r.Context().Value("user") != nil {
@ -84,30 +86,30 @@ func NewUserModifierUpdate(db *Database, r *http.Request) *UserModifierUpdate {
}
}
func (um *UserModifierUpdate) UpdatedBy() (*UserAction, error) {
action := new(UserAction)
func (um *UserModifierUpdate) UpdatedBy() *User {
return um.UpdaterUser
}
func (um *UserModifierUpdate) get(db *Database) (*User, error) {
switch um.UpdaterRole {
case "participant":
var participant Participant
if err := um.db._db.Preload("User").First(&participant, um.UpdaterID).Error; err != nil {
if err := db._db.Preload("User").First(&participant, um.UpdaterID).Error; err != nil {
return nil, err
}
action.User = *participant.User
return participant.User, nil
case "school":
var school School
if err := um.db._db.Preload("User").First(&school, um.UpdaterID).Error; err != nil {
if err := db._db.Preload("User").First(&school, um.UpdaterID).Error; err != nil {
return nil, err
}
action.User = *school.User
return school.User, nil
case "subscriber":
action.User = SubscriberUser
return &SubscriberUser, nil
case "administrator":
action.User = AdministratorUser
return &AdministratorUser, nil
default:
return nil, nil
return nil, errors.New("Undefined user!")
}
return action, nil
}