Change field changer to use email of current user (#46)

This commit is contained in:
itsscb 2023-09-29 00:22:43 +02:00 committed by GitHub
parent 8ccc74b677
commit 79f4acaa58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 41 deletions

View File

@ -48,7 +48,7 @@ func (server *Server) createAccount(ctx *gin.Context) {
Zip: req.Zip, Zip: req.Zip,
Street: req.Street, Street: req.Street,
Country: req.Country, Country: req.Country,
Creator: "system", Creator: req.Email,
Phone: sql.NullString{ Phone: sql.NullString{
Valid: req.Phone != "", Valid: req.Phone != "",
String: req.Phone, String: req.Phone,
@ -146,8 +146,7 @@ func (server *Server) listAccounts(ctx *gin.Context) {
type updateAccountPrivacyRequest struct { type updateAccountPrivacyRequest struct {
ID int64 `binding:"required" json:"ID"` ID int64 `binding:"required" json:"ID"`
Changer string `binding:"required" json:"changer"` PrivacyAccepted *bool `binding:"required" json:"privacy_accepted"`
PrivacyAccepted bool `json:"privacy_accepted"`
} }
func (server *Server) updateAccountPrivacy(ctx *gin.Context) { func (server *Server) updateAccountPrivacy(ctx *gin.Context) {
@ -170,7 +169,11 @@ func (server *Server) updateAccountPrivacy(ctx *gin.Context) {
return return
} }
account, err = server.store.UpdateAccountPrivacyTx(ctx, db.UpdateAccountPrivacyTxParams(req)) account, err = server.store.UpdateAccountPrivacyTx(ctx, db.UpdateAccountPrivacyTxParams{
ID: req.ID,
Changer: authPayload.Email,
PrivacyAccepted: *req.PrivacyAccepted,
})
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return return
@ -181,7 +184,6 @@ func (server *Server) updateAccountPrivacy(ctx *gin.Context) {
type updateAccountRequest struct { type updateAccountRequest struct {
ID int64 `binding:"required" json:"ID"` ID int64 `binding:"required" json:"ID"`
Changer string `binding:"required" json:"changer"`
Passwordhash string `json:"passwordhash"` Passwordhash string `json:"passwordhash"`
Firstname string `json:"firstname"` Firstname string `json:"firstname"`
Lastname string `json:"lastname"` Lastname string `json:"lastname"`
@ -216,7 +218,7 @@ func (server *Server) updateAccount(ctx *gin.Context) {
arg := db.UpdateAccountTxParams{ arg := db.UpdateAccountTxParams{
ID: req.ID, ID: req.ID,
Changer: req.Changer, Changer: authPayload.Email,
Passwordhash: sql.NullString{ Passwordhash: sql.NullString{
String: req.Passwordhash, String: req.Passwordhash,
Valid: req.Passwordhash != "", Valid: req.Passwordhash != "",

View File

@ -30,7 +30,7 @@ func TestCreateAccountAPI(t *testing.T) {
body gin.H body gin.H
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker) setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
buildStubs func(store *mockdb.MockStore) buildStubs func(store *mockdb.MockStore)
checkResponse func(recoder *httptest.ResponseRecorder) checkResponse func(recorder *httptest.ResponseRecorder)
}{ }{
{ {
name: "OK", name: "OK",
@ -46,7 +46,7 @@ func TestCreateAccountAPI(t *testing.T) {
"street": account.Street, "street": account.Street,
"country": account.Country, "country": account.Country,
"phone": account.Phone.String, "phone": account.Phone.String,
"creator": account.Creator, "creator": account.Email,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute) addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute)
@ -64,7 +64,7 @@ func TestCreateAccountAPI(t *testing.T) {
Street: account.Street, Street: account.Street,
Country: account.Country, Country: account.Country,
Phone: account.Phone, Phone: account.Phone,
Creator: account.Creator, Creator: account.Email,
} }
store.EXPECT(). store.EXPECT().
@ -91,7 +91,7 @@ func TestCreateAccountAPI(t *testing.T) {
// "street": account.Street, // "street": account.Street,
// "country": account.Country, // "country": account.Country,
// "phone": account.Phone.String, // "phone": account.Phone.String,
// "creator": account.Creator, // "creator": account.Email,
// }, // },
// setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { // setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
// }, // },
@ -136,7 +136,7 @@ func TestCreateAccountAPI(t *testing.T) {
"street": account.Street, "street": account.Street,
"country": account.Country, "country": account.Country,
"phone": account.Phone.String, "phone": account.Phone.String,
"creator": account.Creator, "creator": account.Email,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute) addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute)
@ -191,7 +191,7 @@ func TestGetAccountAPI(t *testing.T) {
accountID int64 accountID int64
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker) setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
buildStubs func(store *mockdb.MockStore) buildStubs func(store *mockdb.MockStore)
checkResponse func(t *testing.T, recoder *httptest.ResponseRecorder) checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
}{ }{
{ {
name: "OK", name: "OK",
@ -316,8 +316,6 @@ func TestGetAccountAPI(t *testing.T) {
func TestUpdateAccountTxAPI(t *testing.T) { func TestUpdateAccountTxAPI(t *testing.T) {
account := randomAccount() account := randomAccount()
changer := util.RandomName()
// newPassword := util.RandomString(30)
newLastname := util.RandomName() newLastname := util.RandomName()
testCases := []struct { testCases := []struct {
@ -326,7 +324,7 @@ func TestUpdateAccountTxAPI(t *testing.T) {
accountID string accountID string
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker) setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
buildStubs func(store *mockdb.MockStore) buildStubs func(store *mockdb.MockStore)
checkResponse func(recoder *httptest.ResponseRecorder) checkResponse func(recorder *httptest.ResponseRecorder)
}{ }{
// { // {
// name: "OK_PasswordHash", // name: "OK_PasswordHash",
@ -373,7 +371,6 @@ func TestUpdateAccountTxAPI(t *testing.T) {
body: gin.H{ body: gin.H{
"id": account.ID, "id": account.ID,
"lastname": newLastname, "lastname": newLastname,
"changer": changer,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute) addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute)
@ -385,7 +382,7 @@ func TestUpdateAccountTxAPI(t *testing.T) {
Valid: true, Valid: true,
String: newLastname, String: newLastname,
}, },
Changer: changer, Changer: account.Email,
} }
store.EXPECT(). store.EXPECT().
@ -408,7 +405,6 @@ func TestUpdateAccountTxAPI(t *testing.T) {
body: gin.H{ body: gin.H{
"id": account.ID, "id": account.ID,
"lastname": newLastname, "lastname": newLastname,
"changer": changer,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
}, },
@ -490,7 +486,7 @@ func TestListAccountsAPI(t *testing.T) {
query Query query Query
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker) setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
buildStubs func(store *mockdb.MockStore) buildStubs func(store *mockdb.MockStore)
checkResponse func(recoder *httptest.ResponseRecorder) checkResponse func(recorder *httptest.ResponseRecorder)
}{ }{
{ {
name: "OK", name: "OK",
@ -629,20 +625,18 @@ func TestListAccountsAPI(t *testing.T) {
func TestUpdateAccountPrivacyTxAPI(t *testing.T) { func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
account := randomAccount() account := randomAccount()
changer := util.RandomName()
testCases := []struct { testCases := []struct {
name string name string
body gin.H body gin.H
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker) setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
buildStubs func(store *mockdb.MockStore) buildStubs func(store *mockdb.MockStore)
checkResponse func(recoder *httptest.ResponseRecorder) checkResponse func(recorder *httptest.ResponseRecorder)
}{ }{
{ {
name: "OK", name: "OK",
body: gin.H{ body: gin.H{
"id": account.ID, "id": account.ID,
"changer": changer,
"privacy_accepted": true, "privacy_accepted": true,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
@ -652,13 +646,13 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
arg := db.UpdateAccountPrivacyTxParams{ arg := db.UpdateAccountPrivacyTxParams{
ID: account.ID, ID: account.ID,
PrivacyAccepted: true, PrivacyAccepted: true,
Changer: changer, Changer: account.Email,
} }
account2 := account account2 := account
account2.PrivacyAccepted.Valid = true account2.PrivacyAccepted.Valid = true
account2.PrivacyAccepted.Bool = true account2.PrivacyAccepted.Bool = true
account2.Changer = changer account2.Changer = account.Email
store.EXPECT(). store.EXPECT().
GetAccount(gomock.Any(), gomock.Eq(account.ID)). GetAccount(gomock.Any(), gomock.Eq(account.ID)).
@ -670,9 +664,55 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
Times(1). Times(1).
Return(account2, nil) Return(account2, nil)
}, },
checkResponse: func(recoder *httptest.ResponseRecorder) { checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recoder.Code) require.Equal(t, http.StatusOK, recorder.Code)
data, err := io.ReadAll(recoder.Body) data, err := io.ReadAll(recorder.Body)
require.NoError(t, err)
var getAccount db.Account
err = json.Unmarshal(data, &getAccount)
require.NoError(t, err)
require.Equal(t, account.ID, getAccount.ID)
require.Equal(t, true, getAccount.PrivacyAccepted.Bool)
require.Equal(t, true, getAccount.PrivacyAccepted.Valid)
require.WithinDuration(t, timestamp, getAccount.PrivacyAcceptedDate.Time, time.Second)
},
},
{
name: "OK",
body: gin.H{
"id": account.ID,
"privacy_accepted": true,
},
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, account.Email, time.Minute)
},
buildStubs: func(store *mockdb.MockStore) {
arg := db.UpdateAccountPrivacyTxParams{
ID: account.ID,
PrivacyAccepted: true,
Changer: account.Email,
}
account2 := account
account2.PrivacyAccepted.Valid = true
account2.PrivacyAccepted.Bool = true
account2.Changer = account.Email
store.EXPECT().
GetAccount(gomock.Any(), gomock.Eq(account.ID)).
Times(1).
Return(account, nil)
store.EXPECT().
UpdateAccountPrivacyTx(gomock.Any(), gomock.Eq(arg)).
Times(1).
Return(account2, nil)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
data, err := io.ReadAll(recorder.Body)
require.NoError(t, err) require.NoError(t, err)
var getAccount db.Account var getAccount db.Account
@ -689,7 +729,6 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
name: "Revoked", name: "Revoked",
body: gin.H{ body: gin.H{
"id": account.ID, "id": account.ID,
"changer": changer,
"privacy_accepted": false, "privacy_accepted": false,
}, },
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) { setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
@ -699,7 +738,7 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
arg := db.UpdateAccountPrivacyTxParams{ arg := db.UpdateAccountPrivacyTxParams{
ID: account.ID, ID: account.ID,
PrivacyAccepted: false, PrivacyAccepted: false,
Changer: changer, Changer: account.Email,
} }
account2 := account account2 := account
@ -707,7 +746,7 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
account2.PrivacyAccepted.Bool = false account2.PrivacyAccepted.Bool = false
account2.PrivacyAcceptedDate.Valid = true account2.PrivacyAcceptedDate.Valid = true
account2.PrivacyAcceptedDate.Time = time.Time{} account2.PrivacyAcceptedDate.Time = time.Time{}
account2.Changer = changer account2.Changer = account.Email
store.EXPECT(). store.EXPECT().
GetAccount(gomock.Any(), gomock.Eq(account.ID)). GetAccount(gomock.Any(), gomock.Eq(account.ID)).
@ -719,9 +758,9 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
Times(1). Times(1).
Return(account2, nil) Return(account2, nil)
}, },
checkResponse: func(recoder *httptest.ResponseRecorder) { checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recoder.Code) require.Equal(t, http.StatusOK, recorder.Code)
data, err := io.ReadAll(recoder.Body) data, err := io.ReadAll(recorder.Body)
require.NoError(t, err) require.NoError(t, err)
var getAccount db.Account var getAccount db.Account
@ -744,15 +783,15 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
}, },
buildStubs: func(store *mockdb.MockStore) { buildStubs: func(store *mockdb.MockStore) {
store.EXPECT(). store.EXPECT().
GetAccount(gomock.Any(), gomock.Any()). GetAccount(gomock.Any(), gomock.Eq(account.ID)).
Times(0) Times(0)
store.EXPECT(). store.EXPECT().
UpdateAccountPrivacyTx(gomock.Any(), gomock.Any()). UpdateAccountPrivacyTx(gomock.Any(), gomock.Any()).
Times(0) Times(0)
}, },
checkResponse: func(recoder *httptest.ResponseRecorder) { checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusBadRequest, recoder.Code) require.Equal(t, http.StatusBadRequest, recorder.Code)
}, },
}, },
} }
@ -775,6 +814,7 @@ func TestUpdateAccountPrivacyTxAPI(t *testing.T) {
// Marshal body data to JSON // Marshal body data to JSON
data, err := json.Marshal(tc.body) data, err := json.Marshal(tc.body)
require.NoError(t, err) require.NoError(t, err)
fmt.Println("privacy revoked", "body", string(data))
url := "/accounts/privacy" url := "/accounts/privacy"
request, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data)) request, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
@ -791,12 +831,13 @@ func randomAccount() db.Account {
password := util.RandomString(6) password := util.RandomString(6)
hashedPassword, _ := util.HashPassword(password) hashedPassword, _ := util.HashPassword(password)
email := util.RandomEmail()
acc := db.Account{ acc := db.Account{
ID: util.RandomInt(1, 1000), ID: util.RandomInt(1, 1000),
Passwordhash: hashedPassword, Passwordhash: hashedPassword,
Firstname: util.RandomName(), Firstname: util.RandomName(),
Lastname: util.RandomName(), Lastname: util.RandomName(),
Email: util.RandomEmail(), Email: email,
PrivacyAccepted: sql.NullBool{ PrivacyAccepted: sql.NullBool{
Valid: true, Valid: true,
Bool: true, Bool: true,
@ -813,8 +854,8 @@ func randomAccount() db.Account {
Street: util.RandomName(), Street: util.RandomName(),
City: util.RandomName(), City: util.RandomName(),
Country: util.RandomName(), Country: util.RandomName(),
Creator: "system", Creator: email,
Changer: util.RandomName(), Changer: email,
Created: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC), Created: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
Changed: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC), Changed: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
Birthday: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Birthday: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
@ -839,7 +880,7 @@ func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Accoun
require.Equal(t, account.Street, getAccount.Street) require.Equal(t, account.Street, getAccount.Street)
require.Equal(t, account.Country, getAccount.Country) require.Equal(t, account.Country, getAccount.Country)
require.Equal(t, account.Zip, getAccount.Zip) require.Equal(t, account.Zip, getAccount.Zip)
require.Equal(t, account.Creator, getAccount.Creator) require.Equal(t, account.Email, getAccount.Creator)
require.Equal(t, account.PrivacyAccepted, getAccount.PrivacyAccepted) require.Equal(t, account.PrivacyAccepted, getAccount.PrivacyAccepted)
// require.WithinDuration(t, account.PrivacyAcceptedDate.Time, getAccount.PrivacyAcceptedDate.Time, time.Minute) // require.WithinDuration(t, account.PrivacyAcceptedDate.Time, getAccount.PrivacyAcceptedDate.Time, time.Minute)
} }

View File

@ -0,0 +1 @@
f6dc25b460fbfd04f2d7e3aaf87fa1e934d8a4ed210726921aa1b51274a9bc58