Skip to content

Commit

Permalink
auth:bugfix - removing unnecessary log of duplicate user (ZupIT#552)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Martins <nathan.martins@zup.com.br>
  • Loading branch information
nathanmartinszup authored Feb 14, 2022
1 parent c28a644 commit 46dc2dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 10 additions & 6 deletions auth/config/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,23 @@ func (c *Config) GetDefaultUserData() (*accountEntities.Account, error) {
if c.DefaultUserData == enums.DefaultUserData {
logger.LogWarn(enums.MessageWarningDefaultUser)
}

if err := json.Unmarshal([]byte(c.DefaultUserData), &account); err != nil {
return nil, err
}

return account, nil
}

func (c *Config) GetApplicationAdminData() (*accountEntities.Account, error) {
account := &accountEntities.Account{}

logger.LogWarn(fmt.Sprintf(enums.MessageFailedToFormatAppAdminValue, enums.EnvApplicationAdminData))

if c.ApplicationAdminData == enums.ApplicationAdminDefaultData {
logger.LogWarn(enums.MessageWarningDefaultApplicationAdmin)
}

if err := json.Unmarshal([]byte(c.ApplicationAdminData), account); err != nil {
logger.LogWarn(fmt.Sprintf(enums.MessageFailedToFormatAppAdminValue, enums.EnvApplicationAdminData))
return nil, err
}

Expand Down Expand Up @@ -212,10 +214,12 @@ func (c *Config) createApplicationAdminUser() {
}

func (c *Config) createAccount(account *accountEntities.Account) {
err := c.databaseWrite.Create(account, accountEnums.DatabaseTableAccount).GetError()
if err != nil {
c.checkCreateAccountErrors(err, account)
return
_, err := c.getAccountByEmail(account.Email)
if err != nil && err == databaseEnums.ErrorNotFoundRecords {
if err := c.databaseWrite.Create(account, accountEnums.DatabaseTableAccount).GetError(); err != nil {
c.checkCreateAccountErrors(err, account)
return
}
}

logger.LogInfo(fmt.Sprintf(enums.MessageUserCreateWithSuccess, account.Username, account.Email))
Expand Down
3 changes: 3 additions & 0 deletions auth/config/app/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
func getMockedConnection() *database.Connection {
databaseMock := &database.Mock{}
databaseMock.On("Create").Return(&response.Response{})
databaseMock.On("Find").Return(&response.Response{})

return &database.Connection{Read: databaseMock, Write: databaseMock}
}
Expand Down Expand Up @@ -142,6 +143,8 @@ func TestNewAuthAppConfig(t *testing.T) {
databaseMock := &database.Mock{}
databaseMock.On("Create").Once().Return(
response.NewResponse(0, errors.New(enums.DuplicatedAccount), nil))
databaseMock.On("Find").Once().
Return(response.NewResponse(0, databaseEnums.ErrorNotFoundRecords, nil))
databaseMock.On("Find").Return(&response.Response{})
databaseMock.On("Delete").Return(&response.Response{})
databaseMock.On("Create").Return(&response.Response{})
Expand Down

0 comments on commit 46dc2dc

Please sign in to comment.