Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-enable early access flag for users in non-production environments #412

Merged
merged 12 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions controllers/accounts/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ func (ctrl *AuthController) Register(ctx *gin.Context) {
SetPassword(payload.Password).
SetScope(scope)

// Checking the environment to set the user as verified and give early access
if serverConf.Environment != "production" {
userCreate = userCreate.
SetIsEmailVerified(true)
SetIsEmailVerified(true).
SetHasEarlyAccess(true)
}

user, err := userCreate.Save(ctx)
Expand Down Expand Up @@ -260,7 +262,7 @@ func (ctrl *AuthController) Login(ctx *gin.Context) {

// Check if user has early access
environment := serverConf.Environment
if !user.HasEarlyAccess && (environment == "production" || environment == "staging") {
if !user.HasEarlyAccess && (environment == "production") {
u.APIResponse(ctx, http.StatusUnauthorized, "error",
"Your early access request is still pending", nil,
)
Expand Down
60 changes: 60 additions & 0 deletions controllers/accounts/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strings"
"testing"
Expand All @@ -13,6 +15,7 @@ import (
"github.com/google/uuid"
"github.com/jarcoal/httpmock"
_ "github.com/mattn/go-sqlite3"
"github.com/paycrest/aggregator/config"
"github.com/paycrest/aggregator/ent"
"github.com/paycrest/aggregator/routers/middleware"
svc "github.com/paycrest/aggregator/services"
Expand All @@ -32,6 +35,7 @@ import (
)

func TestAuth(t *testing.T) {

// setup httpmock
httpmock.Activate()
defer httpmock.Deactivate()
Expand Down Expand Up @@ -433,6 +437,62 @@ func TestAuth(t *testing.T) {
assert.Contains(t, errorMap, "message")
})

t.Run("testing UserEarlyAccess", func(t *testing.T) {
tests := []struct {
name string
environment string
expectedEarlyAccess bool
}{
{
name: "Non-production environment (development)",
environment: "development",
expectedEarlyAccess: true,
},
{
name: "Non-production environment (staging)",
environment: "staging",
expectedEarlyAccess: true,
},
{
name: "Production environment",
environment: "production",
expectedEarlyAccess: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("ENVIRONMENT", tt.environment)

serverConf := config.ServerConfiguration{Environment: tt.environment}

hasEarlyAccess := serverConf.Environment != "production"

ctx := context.Background()
user, err := client.User.Create().
SetFirstName("Ike").
SetLastName("Ayo").
SetEmail(fmt.Sprintf("test-%[email protected]", tt.environment)).
SetPassword("password").
SetScope("sender").
SetHasEarlyAccess(hasEarlyAccess).
Save(ctx)
assert.NoError(t, err)

createdUser, err := client.User.Get(ctx, user.ID)
if err != nil {
t.Fatal("Failed to fetch user from database:", err)
}
assert.Equal(t, tt.expectedEarlyAccess, createdUser.HasEarlyAccess, "unexpected HasEarlyAccess for environment %s", tt.environment)

// Cleanup
err = client.User.DeleteOne(user).Exec(ctx)
assert.NoError(t, err)
})

}
})

})

t.Run("ConfirmEmail", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ent/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (User) Fields() []ent.Field {
field.String("scope"),
field.Bool("is_email_verified").
Default(false),
field.Bool("has_early_access").
field.Bool("has_early_access"). // has_early_access is "false" by default
Default(false),
}
}
Expand Down
Loading