From a2942006cac095a29a8715cc61bb9567239a4181 Mon Sep 17 00:00:00 2001 From: Romain Broussard Date: Mon, 5 Feb 2024 12:26:16 +0100 Subject: [PATCH] feat: manage case where account is created but sending confirmation mail fails (#9) * chore: Improve lint section of Makefile * chore: remove unused function * feat: manage case where account is created but sending confirmation mail fails --- Makefile | 5 ++++- pkg/accounts/accounts.go | 26 +++++++++++++++----------- pkg/accounts/accounts_test.go | 4 ++-- pkg/helper/helper.go | 10 ---------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 68be80d..0861b85 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,7 @@ build: test go build lint: - docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.52 golangci-lint run -v + @echo "Running staticcheck..." + @staticcheck ./... + @echo "Running golangci-lint..." + @golangci-lint run --timeout=5m --out-format=colored-line-number \ No newline at end of file diff --git a/pkg/accounts/accounts.go b/pkg/accounts/accounts.go index 4dac37a..9d02308 100644 --- a/pkg/accounts/accounts.go +++ b/pkg/accounts/accounts.go @@ -48,50 +48,54 @@ func Register(c *gin.Context) { user.Created_at = time.Now().Truncate(time.Second) user.Updated_at = time.Now().Truncate(time.Second) - err := saveUser(user) + err, emailSended := registerUser(user) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - c.JSON(http.StatusOK, gin.H{"message": "registration success"}) + if !emailSended { + c.JSON(http.StatusAccepted, gin.H{"message": "registration succeed but failed to send confirmation email"}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "registration succeed, please check your email to confirm your account"}) } -func saveUser(u dataset.User) error { +func registerUser(u dataset.User) (error, bool) { var id int confirmationCode, err := helper.GenerateRandomCode(30) if err != nil { - return fmt.Errorf("failed to generate confirmation code: %w", err) + return fmt.Errorf("failed to generate confirmation code: %w", err), false } err = database.Db().QueryRow("INSERT INTO account (username, email, firstname, lastname, role, status, confirmation_code, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8, $9) RETURNING id;", u.Username, u.Email, u.Firstname, u.Lastname, u.Role, u.Status, confirmationCode, u.Created_at, u.Updated_at).Scan(&id) if err != nil { - return fmt.Errorf("failed to insert user: %w", err) + return fmt.Errorf("failed to insert user: %w", err), false } u.ID = uint(id) hashedPassword, err := security.HashPassword(u.Password) if err != nil { - return fmt.Errorf("failed to hash password: %w", err) + return fmt.Errorf("failed to hash password: %w", err), false } err = database.Db().QueryRow("INSERT INTO password (user_id, password, updated_at) VALUES ($1,$2,$3) RETURNING id;", id, hashedPassword, u.Updated_at).Scan(&id) if err != nil { - return fmt.Errorf("failed to insert password: %w", err) + return fmt.Errorf("failed to insert password: %w", err), false } err = sendConfirmationEmail(u, confirmationCode) if err != nil { - // deactivate email sending error throwing for now - // return fmt.Errorf("failed to send confirmation email: %w", err) - return nil + // we haven't succed to send the email but the user is created + return nil, false } - return nil + return nil, true } // Send confirmation email diff --git a/pkg/accounts/accounts_test.go b/pkg/accounts/accounts_test.go index 66233cc..ab97c5b 100644 --- a/pkg/accounts/accounts_test.go +++ b/pkg/accounts/accounts_test.go @@ -380,7 +380,7 @@ func TestRegister(t *testing.T) { newAccount := dataset.RegisterInput{ Username: fmt.Sprintf("user-%s", random.UniqueId()), Password: "password", - Email: "jane.doe@pmp.com", + Email: "jane.doe@exemple.com", Firstname: "Jane", Lastname: "Doe", } @@ -404,7 +404,7 @@ func TestRegister(t *testing.T) { router.ServeHTTP(w, req) // Check the HTTP status code - if w.Code != http.StatusOK { + if w.Code != http.StatusOK && w.Code != http.StatusAccepted { t.Errorf("Expected status code %d but got %d", http.StatusOK, w.Code) } diff --git a/pkg/helper/helper.go b/pkg/helper/helper.go index 3e2c983..157cb00 100644 --- a/pkg/helper/helper.go +++ b/pkg/helper/helper.go @@ -7,7 +7,6 @@ import ( "strconv" "github.com/Angak0k/pimpmypack/pkg/dataset" - "github.com/joho/godotenv" ) func StringToUint(s string) (uint, error) { @@ -30,15 +29,6 @@ func ConvertWeightUnit(unit string) string { return "METRIC" } -func EnvInit() error { - // Load environment variables from .env file - err := godotenv.Load() - if err != nil { - return err - } - return nil -} - func FinUserIDByUsername(users []dataset.User, username string) uint { // Find a user ID by username for _, user := range users {