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 10 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
55 changes: 39 additions & 16 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ import (

// ServerConfiguration type defines the server configurations
type ServerConfiguration struct {
Debug bool
Host string
Port string
Timezone string
AllowedHosts string
Environment string
SentryDSN string
HostDomain string
Debug bool
Host string
Port string
Timezone string
AllowedHosts string
Environment string
SentryDSN string
HostDomain string
CurrenciesCacheDuration int
InstitutionsCacheDuration int
PubKeyCacheDuration int
RateLimitUnauthenticated int
RateLimitAuthenticated int
chibie marked this conversation as resolved.
Show resolved Hide resolved
}

// ServerConfig sets the server configuration
Expand All @@ -27,19 +32,37 @@ func ServerConfig() *ServerConfiguration {
viper.SetDefault("ALLOWED_HOSTS", "*")
viper.SetDefault("ENVIRONMENT", "local")
viper.SetDefault("SENTRY_DSN", "")
viper.SetDefault("CURRENCIES_CACHE_DURATION", 24)
viper.SetDefault("INSTITUTIONS_CACHE_DURATION", 24)
viper.SetDefault("PUBKEY_CACHE_DURATION", 365)
viper.SetDefault("RATE_LIMIT_UNAUTHENTICATED", 5)
viper.SetDefault("RATE_LIMIT_AUTHENTICATED", 50)

return &ServerConfiguration{
Debug: viper.GetBool("DEBUG"),
Host: viper.GetString("SERVER_HOST"),
Port: viper.GetString("SERVER_PORT"),
Timezone: viper.GetString("SERVER_TIMEZONE"),
AllowedHosts: viper.GetString("ALLOWED_HOSTS"),
Environment: viper.GetString("ENVIRONMENT"),
SentryDSN: viper.GetString("SENTRY_DSN"),
HostDomain: viper.GetString("HOST_DOMAIN"),
Debug: viper.GetBool("DEBUG"),
Host: viper.GetString("SERVER_HOST"),
Port: viper.GetString("SERVER_PORT"),
Timezone: viper.GetString("SERVER_TIMEZONE"),
AllowedHosts: viper.GetString("ALLOWED_HOSTS"),
Environment: viper.GetString("ENVIRONMENT"),
SentryDSN: viper.GetString("SENTRY_DSN"),
HostDomain: viper.GetString("HOST_DOMAIN"),
CurrenciesCacheDuration: viper.GetInt("CURRENCIES_CACHE_DURATION"),
InstitutionsCacheDuration: viper.GetInt("INSTITUTIONS_CACHE_DURATION"),
PubKeyCacheDuration: viper.GetInt("PUBKEY_CACHE_DURATION"),
RateLimitUnauthenticated: viper.GetInt("RATE_LIMIT_UNAUTHENTICATED"),
RateLimitAuthenticated: viper.GetInt("RATE_LIMIT_AUTHENTICATED"),
}
chibie marked this conversation as resolved.
Show resolved Hide resolved
}

// Reload reinitializes the configuration using the current environment variables
func Reload() (ServerConfiguration, error) {
if err := SetupConfig(); err != nil {
return ServerConfiguration{}, fmt.Errorf("config Reload() error: %s", err)
}
return *ServerConfig(), nil // Return the updated config
}

chibie marked this conversation as resolved.
Show resolved Hide resolved
func init() {
if err := SetupConfig(); err != nil {
panic(fmt.Sprintf("config SetupConfig() error: %s", err))
Expand Down
3 changes: 2 additions & 1 deletion controllers/accounts/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (ctrl *AuthController) Register(ctx *gin.Context) {

if serverConf.Environment != "production" {
userCreate = userCreate.
SetIsEmailVerified(true)
SetIsEmailVerified(true).
SetHasEarlyAccess(true)
}

user, err := userCreate.Save(ctx)
Expand Down
65 changes: 65 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,67 @@ 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)

// Reload and update serverConf correctly
// newConf, err := config.Reload()
// assert.NoError(t, err)
// serverConf := &newConf
chibie marked this conversation as resolved.
Show resolved Hide resolved

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