fix(server): dynamodb tests + provider config
This commit is contained in:
parent
476bdf00fc
commit
8449821d1b
|
@ -6,4 +6,5 @@ SMTP_HOST=smtp.mailtrap.io
|
||||||
SMTP_PORT=2525
|
SMTP_PORT=2525
|
||||||
SMTP_USERNAME=test
|
SMTP_USERNAME=test
|
||||||
SMTP_PASSWORD=test
|
SMTP_PASSWORD=test
|
||||||
SENDER_EMAIL="info@authorizer.dev"
|
SENDER_EMAIL="info@authorizer.dev"
|
||||||
|
AWS_REGION=ap-south-1
|
|
@ -1,9 +1,6 @@
|
||||||
package dynamodb
|
package dynamodb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
|
@ -22,36 +19,29 @@ type provider struct {
|
||||||
// NewProvider returns a new Dynamo provider
|
// NewProvider returns a new Dynamo provider
|
||||||
func NewProvider() (*provider, error) {
|
func NewProvider() (*provider, error) {
|
||||||
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
||||||
awsRegion := os.Getenv(constants.EnvAwsRegion)
|
awsRegion := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AwsRegion
|
||||||
accessKey := os.Getenv(constants.EnvAwsAccessKeyID)
|
awsAccessKeyID := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AwsAccessKeyID
|
||||||
secretKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
awsSecretAccessKey := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AwsSecretAccessKey
|
||||||
|
|
||||||
config := aws.Config{
|
config := aws.Config{
|
||||||
MaxRetries: aws.Int(3),
|
MaxRetries: aws.Int(3),
|
||||||
CredentialsChainVerboseErrors: aws.Bool(true), // for full error logs
|
CredentialsChainVerboseErrors: aws.Bool(true), // for full error logs
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if awsRegion != "" {
|
if awsRegion != "" {
|
||||||
config.Region = aws.String(awsRegion)
|
config.Region = aws.String(awsRegion)
|
||||||
}
|
}
|
||||||
|
|
||||||
if accessKey == "" {
|
// custom awsAccessKeyID, awsSecretAccessKey took first priority, if not then fetch config from aws credentials
|
||||||
log.Debugf("%s not found", constants.EnvAwsAccessKeyID)
|
if awsAccessKeyID != "" && awsSecretAccessKey != "" {
|
||||||
return nil, fmt.Errorf("invalid aws credentials. %s not found", constants.EnvAwsAccessKeyID)
|
config.Credentials = credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, "")
|
||||||
}
|
|
||||||
|
|
||||||
if secretKey == "" {
|
|
||||||
log.Debugf("%s not found", constants.EnvAwsSecretAccessKey)
|
|
||||||
return nil, fmt.Errorf("invalid aws credentials. %s not found", constants.EnvAwsSecretAccessKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// custom accessKey, secretkey took first priority, if not then fetch config from aws credentials
|
|
||||||
if accessKey != "" && secretKey != "" {
|
|
||||||
config.Credentials = credentials.NewStaticCredentials(accessKey, secretKey, "")
|
|
||||||
} else if dbURL != "" {
|
} else if dbURL != "" {
|
||||||
// static config in case of testing or local-setup
|
// static config in case of testing or local-setup
|
||||||
config.Credentials = credentials.NewStaticCredentials("key", "key", "")
|
config.Credentials = credentials.NewStaticCredentials("key", "key", "")
|
||||||
config.Endpoint = aws.String(dbURL)
|
config.Endpoint = aws.String(dbURL)
|
||||||
|
} else {
|
||||||
|
log.Debugf("%s or %s or %s not found. Trying to load default credentials from aws config", constants.EnvAwsRegion, constants.EnvAwsAccessKeyID, constants.EnvAwsSecretAccessKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
session := session.Must(session.NewSession(&config))
|
session := session.Must(session.NewSession(&config))
|
||||||
|
|
|
@ -180,7 +180,7 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
|
||||||
|
|
||||||
for _, user := range allUsers {
|
for _, user := range allUsers {
|
||||||
err = UpdateByHashKey(userCollection, "id", user.ID, data)
|
err = UpdateByHashKey(userCollection, "id", user.ID, data)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
res = res + 1
|
res = res + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ func InitMemStore() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
redisURL := requiredEnvs.RedisURL
|
redisURL := requiredEnvs.RedisURL
|
||||||
if redisURL != "" && !requiredEnvs.disableRedisForEnv {
|
if redisURL != "" && !requiredEnvs.DisableRedisForEnv {
|
||||||
log.Info("Initializing Redis memory store")
|
log.Info("Initializing Redis memory store")
|
||||||
Provider, err = redis.NewRedisProvider(redisURL)
|
Provider, err = redis.NewRedisProvider(redisURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -27,7 +27,11 @@ type RequiredEnv struct {
|
||||||
DatabaseCertKey string `json:"DATABASE_CERT_KEY"`
|
DatabaseCertKey string `json:"DATABASE_CERT_KEY"`
|
||||||
DatabaseCACert string `json:"DATABASE_CA_CERT"`
|
DatabaseCACert string `json:"DATABASE_CA_CERT"`
|
||||||
RedisURL string `json:"REDIS_URL"`
|
RedisURL string `json:"REDIS_URL"`
|
||||||
disableRedisForEnv bool `json:"DISABLE_REDIS_FOR_ENV"`
|
DisableRedisForEnv bool `json:"DISABLE_REDIS_FOR_ENV"`
|
||||||
|
// AWS Related Envs
|
||||||
|
AwsRegion string `json:"AWS_REGION"`
|
||||||
|
AwsAccessKeyID string `json:"AWS_ACCESS_KEY_ID"`
|
||||||
|
AwsSecretAccessKey string `json:"AWS_SECRET_ACCESS_KEY"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequiredEnvObj is a simple in-memory store for sessions.
|
// RequiredEnvObj is a simple in-memory store for sessions.
|
||||||
|
@ -53,7 +57,8 @@ func (r *RequiredEnvStore) SetRequiredEnv(requiredEnv RequiredEnv) {
|
||||||
|
|
||||||
var RequiredEnvStoreObj *RequiredEnvStore
|
var RequiredEnvStoreObj *RequiredEnvStore
|
||||||
|
|
||||||
// InitRequiredEnv to initialize EnvData and through error if required env are not present
|
// InitRequiredEnv to initialize EnvData and throw error if required env are not present
|
||||||
|
// This includes env that only configurable via env vars and not the ui
|
||||||
func InitRequiredEnv() error {
|
func InitRequiredEnv() error {
|
||||||
envPath := os.Getenv(constants.EnvKeyEnvPath)
|
envPath := os.Getenv(constants.EnvKeyEnvPath)
|
||||||
|
|
||||||
|
@ -85,6 +90,9 @@ func InitRequiredEnv() error {
|
||||||
dbCACert := os.Getenv(constants.EnvKeyDatabaseCACert)
|
dbCACert := os.Getenv(constants.EnvKeyDatabaseCACert)
|
||||||
redisURL := os.Getenv(constants.EnvKeyRedisURL)
|
redisURL := os.Getenv(constants.EnvKeyRedisURL)
|
||||||
disableRedisForEnv := os.Getenv(constants.EnvKeyDisableRedisForEnv) == "true"
|
disableRedisForEnv := os.Getenv(constants.EnvKeyDisableRedisForEnv) == "true"
|
||||||
|
awsRegion := os.Getenv(constants.EnvAwsRegion)
|
||||||
|
awsAccessKeyID := os.Getenv(constants.EnvAwsAccessKeyID)
|
||||||
|
awsSecretAccessKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
||||||
|
|
||||||
if strings.TrimSpace(redisURL) == "" {
|
if strings.TrimSpace(redisURL) == "" {
|
||||||
if cli.ARG_REDIS_URL != nil && *cli.ARG_REDIS_URL != "" {
|
if cli.ARG_REDIS_URL != nil && *cli.ARG_REDIS_URL != "" {
|
||||||
|
@ -139,7 +147,10 @@ func InitRequiredEnv() error {
|
||||||
DatabaseCertKey: dbCertKey,
|
DatabaseCertKey: dbCertKey,
|
||||||
DatabaseCACert: dbCACert,
|
DatabaseCACert: dbCACert,
|
||||||
RedisURL: redisURL,
|
RedisURL: redisURL,
|
||||||
disableRedisForEnv: disableRedisForEnv,
|
DisableRedisForEnv: disableRedisForEnv,
|
||||||
|
AwsRegion: awsRegion,
|
||||||
|
AwsAccessKeyID: awsAccessKeyID,
|
||||||
|
AwsSecretAccessKey: awsSecretAccessKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
RequiredEnvStoreObj = &RequiredEnvStore{
|
RequiredEnvStoreObj = &RequiredEnvStore{
|
||||||
|
|
|
@ -2,6 +2,7 @@ package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -20,7 +21,7 @@ func TestResolvers(t *testing.T) {
|
||||||
constants.DbTypeArangodb: "http://localhost:8529",
|
constants.DbTypeArangodb: "http://localhost:8529",
|
||||||
constants.DbTypeMongodb: "mongodb://localhost:27017",
|
constants.DbTypeMongodb: "mongodb://localhost:27017",
|
||||||
constants.DbTypeScyllaDB: "127.0.0.1:9042",
|
constants.DbTypeScyllaDB: "127.0.0.1:9042",
|
||||||
constants.DbTypeDynamoDB: "http://127.0.0.1:8000",
|
constants.DbTypeDynamoDB: "http://0.0.0.0:8000",
|
||||||
}
|
}
|
||||||
|
|
||||||
testDBs := strings.Split(os.Getenv("TEST_DBS"), ",")
|
testDBs := strings.Split(os.Getenv("TEST_DBS"), ",")
|
||||||
|
@ -52,6 +53,12 @@ func TestResolvers(t *testing.T) {
|
||||||
os.Setenv(constants.EnvKeyDatabaseURL, dbURL)
|
os.Setenv(constants.EnvKeyDatabaseURL, dbURL)
|
||||||
os.Setenv(constants.EnvKeyDatabaseType, dbType)
|
os.Setenv(constants.EnvKeyDatabaseType, dbType)
|
||||||
os.Setenv(constants.EnvKeyDatabaseName, testDb)
|
os.Setenv(constants.EnvKeyDatabaseName, testDb)
|
||||||
|
|
||||||
|
if dbType == constants.DbTypeDynamoDB {
|
||||||
|
memorystore.Provider.UpdateEnvVariable(constants.EnvAwsRegion, "ap-south-1")
|
||||||
|
os.Setenv(constants.EnvAwsRegion, "ap-south-1")
|
||||||
|
}
|
||||||
|
|
||||||
memorystore.InitRequiredEnv()
|
memorystore.InitRequiredEnv()
|
||||||
|
|
||||||
err := db.InitDB()
|
err := db.InitDB()
|
||||||
|
@ -61,12 +68,15 @@ func TestResolvers(t *testing.T) {
|
||||||
|
|
||||||
// clean the persisted config for test to use fresh config
|
// clean the persisted config for test to use fresh config
|
||||||
envData, err := db.Provider.GetEnv(ctx)
|
envData, err := db.Provider.GetEnv(ctx)
|
||||||
if err == nil {
|
fmt.Println("envData", envData.ID, envData.EnvData)
|
||||||
|
if err == nil && envData.ID != "" {
|
||||||
envData.EnvData = ""
|
envData.EnvData = ""
|
||||||
_, err = db.Provider.UpdateEnv(ctx, envData)
|
_, err = db.Provider.UpdateEnv(ctx, envData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error updating env: %s", err.Error())
|
t.Errorf("Error updating env: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
t.Errorf("Error getting env: %s", err.Error())
|
||||||
}
|
}
|
||||||
err = env.PersistEnv()
|
err = env.PersistEnv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -84,7 +84,7 @@ func testSetup() TestSetup {
|
||||||
testData := TestData{
|
testData := TestData{
|
||||||
Email: fmt.Sprintf("%d_authorizer_tester@yopmail.com", time.Now().Unix()),
|
Email: fmt.Sprintf("%d_authorizer_tester@yopmail.com", time.Now().Unix()),
|
||||||
Password: "Test@123",
|
Password: "Test@123",
|
||||||
WebhookEndpoint: "https://62cbc6738042b16aa7c22df2.mockapi.io/api/v1/webhook",
|
WebhookEndpoint: "https://62f93101e05644803533cf36.mockapi.io/authorizer/webhook",
|
||||||
TestWebhookEventTypes: []string{constants.UserAccessEnabledWebhookEvent, constants.UserAccessRevokedWebhookEvent, constants.UserCreatedWebhookEvent, constants.UserDeletedWebhookEvent, constants.UserLoginWebhookEvent, constants.UserSignUpWebhookEvent},
|
TestWebhookEventTypes: []string{constants.UserAccessEnabledWebhookEvent, constants.UserAccessRevokedWebhookEvent, constants.UserCreatedWebhookEvent, constants.UserDeletedWebhookEvent, constants.UserLoginWebhookEvent, constants.UserSignUpWebhookEvent},
|
||||||
TestEmailTemplateEventTypes: []string{constants.VerificationTypeBasicAuthSignup, constants.VerificationTypeForgotPassword, constants.VerificationTypeMagicLinkLogin, constants.VerificationTypeUpdateEmail},
|
TestEmailTemplateEventTypes: []string{constants.VerificationTypeBasicAuthSignup, constants.VerificationTypeForgotPassword, constants.VerificationTypeMagicLinkLogin, constants.VerificationTypeUpdateEmail},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user