Skip to content

Commit

Permalink
Merge branch 'main' into f/writable-organizations-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Aug 20, 2024
2 parents a7faa02 + 370f20c commit 6174cc7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 39 deletions.
9 changes: 6 additions & 3 deletions api/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ This endpoint only returns the addresses of the organizations where the current
```json
{
"email": "[email protected]",
"fullName": "Steve Urkel",
"firstName": "Steve",
"lastName": "Urkel",
"password": "secretpass1234"
}
```
Expand Down Expand Up @@ -220,7 +221,8 @@ This endpoint only returns the addresses of the organizations where the current
```json
{
"email": "[email protected]",
"fullName": "steve_urkel",
"firstName": "Steve",
"lastName": "Urkel",
"organizations": [
{
"role": "admin",
Expand Down Expand Up @@ -259,7 +261,8 @@ This endpoint only returns the addresses of the organizations where the current
```json
{
"email": "[email protected]",
"fullName": "Steve Urkel",
"firstName": "Steve",
"lastName": "Urkel",
}
```

Expand Down
3 changes: 2 additions & 1 deletion api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type UserOrganization struct {
type UserInfo struct {
Email string `json:"email,omitempty"`
Password string `json:"password,omitempty"`
FullName string `json:"fullName,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
Organizations []*UserOrganization `json:"organizations"`
}

Expand Down
38 changes: 26 additions & 12 deletions api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,24 @@ func (a *API) registerHandler(w http.ResponseWriter, r *http.Request) {
ErrPasswordTooShort.Write(w)
return
}
// check the full name is not empty
if userInfo.FullName == "" {
ErrMalformedBody.Withf("full name is empty").Write(w)
// check the first name is not empty
if userInfo.FirstName == "" {
ErrMalformedBody.Withf("first name is empty").Write(w)
return
}
// check the last name is not empty
if userInfo.LastName == "" {
ErrMalformedBody.Withf("last name is empty").Write(w)
return
}
// hash the password
hPassword := hashPassword(userInfo.Password)
// add the user to the database
if err := a.db.SetUser(&db.User{
Email: userInfo.Email,
FullName: userInfo.FullName,
Password: hex.EncodeToString(hPassword),
Email: userInfo.Email,
FirstName: userInfo.FirstName,
LastName: userInfo.LastName,
Password: hex.EncodeToString(hPassword),
}); err != nil {
log.Warnw("could not create user", "error", err)
ErrGenericInternalServerError.Write(w)
Expand Down Expand Up @@ -86,7 +92,8 @@ func (a *API) userInfoHandler(w http.ResponseWriter, r *http.Request) {
// return the user information
httpWriteJSON(w, UserInfo{
Email: user.Email,
FullName: user.FullName,
FirstName: user.FirstName,
LastName: user.LastName,
Organizations: userOrgs,
})
}
Expand Down Expand Up @@ -120,11 +127,18 @@ func (a *API) updateUserInfoHandler(w http.ResponseWriter, r *http.Request) {
user.Email = userInfo.Email
updateUser = true
}
// check the full name is not empty
if userInfo.FullName != "" {
// update the user full name and set the flag to true to update the user
// info
user.FullName = userInfo.FullName
// check the first name is not empty
if userInfo.FirstName != "" {
// update the user first name and set the flag to true to update the
// user info
user.FirstName = userInfo.FirstName
updateUser = true
}
// check the last name is not empty
if userInfo.LastName != "" {
// update the user last name and set the flag to true to update the
// user info
user.LastName = userInfo.LastName
updateUser = true
}
// update the user information if needed
Expand Down
3 changes: 2 additions & 1 deletion db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type User struct {
ID uint64 `json:"id" bson:"_id"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
FullName string `json:"fullName" bson:"fullName"`
FirstName string `json:"firstName" bson:"firstName"`
LastName string `json:"lastName" bson:"lastName"`
Organizations []OrganizationMember `json:"organizations" bson:"organizations"`
}

Expand Down
54 changes: 32 additions & 22 deletions db/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

const (
testUserEmail = "[email protected]"
testUserPass = "testPassword"
testUserFullName = "User Name"
testUserEmail = "[email protected]"
testUserPass = "testPassword"
testUserFirstName = "User"
testUserLastName = "Name"
)

func TestUserByEmail(t *testing.T) {
Expand All @@ -25,17 +26,19 @@ func TestUserByEmail(t *testing.T) {
c.Assert(err, qt.Equals, ErrNotFound)
// create a new user with the email
c.Assert(db.SetUser(&User{
Email: testUserEmail,
Password: testUserPass,
FullName: testUserFullName,
Email: testUserEmail,
Password: testUserPass,
FirstName: testUserFirstName,
LastName: testUserLastName,
}), qt.IsNil)
// test found user
user, err = db.UserByEmail(testUserEmail)
c.Assert(err, qt.IsNil)
c.Assert(user, qt.Not(qt.IsNil))
c.Assert(user.Email, qt.Equals, testUserEmail)
c.Assert(user.Password, qt.Equals, testUserPass)
c.Assert(user.FullName, qt.Equals, testUserFullName)
c.Assert(user.FirstName, qt.Equals, testUserFirstName)
c.Assert(user.LastName, qt.Equals, testUserLastName)
}

func TestUser(t *testing.T) {
Expand All @@ -52,9 +55,10 @@ func TestUser(t *testing.T) {
c.Assert(err, qt.Equals, ErrNotFound)
// create a new user with the ID
c.Assert(db.SetUser(&User{
Email: testUserEmail,
Password: testUserPass,
FullName: testUserFullName,
Email: testUserEmail,
Password: testUserPass,
FirstName: testUserFirstName,
LastName: testUserLastName,
}), qt.IsNil)
// get the user ID
user, err = db.UserByEmail(testUserEmail)
Expand All @@ -66,7 +70,8 @@ func TestUser(t *testing.T) {
c.Assert(user, qt.Not(qt.IsNil))
c.Assert(user.Email, qt.Equals, testUserEmail)
c.Assert(user.Password, qt.Equals, testUserPass)
c.Assert(user.FullName, qt.Equals, testUserFullName)
c.Assert(user.FirstName, qt.Equals, testUserFirstName)
c.Assert(user.LastName, qt.Equals, testUserLastName)
}

func TestSetUser(t *testing.T) {
Expand All @@ -78,9 +83,10 @@ func TestSetUser(t *testing.T) {
c := qt.New(t)
// trying to create a new user with invalid email
user := &User{
Email: "invalid-email",
Password: testUserPass,
FullName: testUserFullName,
Email: "invalid-email",
Password: testUserPass,
FirstName: testUserFirstName,
LastName: testUserLastName,
}
c.Assert(db.SetUser(user), qt.IsNotNil)
// trying to update a non existing user
Expand All @@ -92,16 +98,17 @@ func TestSetUser(t *testing.T) {
// create a new user
c.Assert(db.SetUser(user), qt.IsNil)
// update the user
newFullName := "New User Name"
user.FullName = newFullName
newFirstName := "New User"
user.FirstName = newFirstName
c.Assert(db.SetUser(user), qt.IsNil)
// get the user
user, err := db.UserByEmail(user.Email)
c.Assert(err, qt.IsNil)
c.Assert(user, qt.Not(qt.IsNil))
c.Assert(user.Email, qt.Equals, testUserEmail)
c.Assert(user.Password, qt.Equals, testUserPass)
c.Assert(user.FullName, qt.Equals, newFullName)
c.Assert(user.FirstName, qt.Equals, newFirstName)
c.Assert(user.LastName, qt.Equals, testUserLastName)
}

func TestDelUser(t *testing.T) {
Expand All @@ -113,9 +120,10 @@ func TestDelUser(t *testing.T) {
c := qt.New(t)
// create a new user
user := &User{
Email: testUserEmail,
Password: testUserPass,
FullName: testUserFullName,
Email: testUserEmail,
Password: testUserPass,
FirstName: testUserFirstName,
LastName: testUserLastName,
}
c.Assert(db.SetUser(user), qt.IsNil)
// get the user
Expand Down Expand Up @@ -148,8 +156,10 @@ func TestIsMemberOf(t *testing.T) {
c := qt.New(t)
// create a new user with some organizations
user := &User{
Email: testUserEmail,
Password: testUserPass,
Email: testUserEmail,
Password: testUserPass,
FirstName: testUserFirstName,
LastName: testUserLastName,
Organizations: []OrganizationMember{
{Address: "adminOrg", Role: AdminRole},
{Address: "managerOrg", Role: ManagerRole},
Expand Down

0 comments on commit 6174cc7

Please sign in to comment.