Skip to content

Commit 8449821

Browse files
committed
fix(server): dynamodb tests + provider config
1 parent 476bdf0 commit 8449821

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

.env.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ SMTP_HOST=smtp.mailtrap.io
66
SMTP_PORT=2525
77
SMTP_USERNAME=test
88
SMTP_PASSWORD=test
9-
SENDER_EMAIL="[email protected]"
9+
SENDER_EMAIL="[email protected]"
10+
AWS_REGION=ap-south-1

server/db/providers/dynamodb/provider.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package dynamodb
22

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/aws/aws-sdk-go/aws"
85
"github.com/aws/aws-sdk-go/aws/credentials"
96
"github.com/aws/aws-sdk-go/aws/session"
@@ -22,36 +19,29 @@ type provider struct {
2219
// NewProvider returns a new Dynamo provider
2320
func NewProvider() (*provider, error) {
2421
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
25-
awsRegion := os.Getenv(constants.EnvAwsRegion)
26-
accessKey := os.Getenv(constants.EnvAwsAccessKeyID)
27-
secretKey := os.Getenv(constants.EnvAwsSecretAccessKey)
22+
awsRegion := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AwsRegion
23+
awsAccessKeyID := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AwsAccessKeyID
24+
awsSecretAccessKey := memorystore.RequiredEnvStoreObj.GetRequiredEnv().AwsSecretAccessKey
2825

2926
config := aws.Config{
3027
MaxRetries: aws.Int(3),
3128
CredentialsChainVerboseErrors: aws.Bool(true), // for full error logs
29+
3230
}
3331

3432
if awsRegion != "" {
3533
config.Region = aws.String(awsRegion)
3634
}
3735

38-
if accessKey == "" {
39-
log.Debugf("%s not found", constants.EnvAwsAccessKeyID)
40-
return nil, fmt.Errorf("invalid aws credentials. %s not found", constants.EnvAwsAccessKeyID)
41-
}
42-
43-
if secretKey == "" {
44-
log.Debugf("%s not found", constants.EnvAwsSecretAccessKey)
45-
return nil, fmt.Errorf("invalid aws credentials. %s not found", constants.EnvAwsSecretAccessKey)
46-
}
47-
48-
// custom accessKey, secretkey took first priority, if not then fetch config from aws credentials
49-
if accessKey != "" && secretKey != "" {
50-
config.Credentials = credentials.NewStaticCredentials(accessKey, secretKey, "")
36+
// custom awsAccessKeyID, awsSecretAccessKey took first priority, if not then fetch config from aws credentials
37+
if awsAccessKeyID != "" && awsSecretAccessKey != "" {
38+
config.Credentials = credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, "")
5139
} else if dbURL != "" {
5240
// static config in case of testing or local-setup
5341
config.Credentials = credentials.NewStaticCredentials("key", "key", "")
5442
config.Endpoint = aws.String(dbURL)
43+
} else {
44+
log.Debugf("%s or %s or %s not found. Trying to load default credentials from aws config", constants.EnvAwsRegion, constants.EnvAwsAccessKeyID, constants.EnvAwsSecretAccessKey)
5545
}
5646

5747
session := session.Must(session.NewSession(&config))

server/db/providers/dynamodb/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
180180

181181
for _, user := range allUsers {
182182
err = UpdateByHashKey(userCollection, "id", user.ID, data)
183-
if err != nil {
183+
if err == nil {
184184
res = res + 1
185185
}
186186
}

server/memorystore/memory_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func InitMemStore() error {
5757
}
5858

5959
redisURL := requiredEnvs.RedisURL
60-
if redisURL != "" && !requiredEnvs.disableRedisForEnv {
60+
if redisURL != "" && !requiredEnvs.DisableRedisForEnv {
6161
log.Info("Initializing Redis memory store")
6262
Provider, err = redis.NewRedisProvider(redisURL)
6363
if err != nil {

server/memorystore/required_env_store.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ type RequiredEnv struct {
2727
DatabaseCertKey string `json:"DATABASE_CERT_KEY"`
2828
DatabaseCACert string `json:"DATABASE_CA_CERT"`
2929
RedisURL string `json:"REDIS_URL"`
30-
disableRedisForEnv bool `json:"DISABLE_REDIS_FOR_ENV"`
30+
DisableRedisForEnv bool `json:"DISABLE_REDIS_FOR_ENV"`
31+
// AWS Related Envs
32+
AwsRegion string `json:"AWS_REGION"`
33+
AwsAccessKeyID string `json:"AWS_ACCESS_KEY_ID"`
34+
AwsSecretAccessKey string `json:"AWS_SECRET_ACCESS_KEY"`
3135
}
3236

3337
// RequiredEnvObj is a simple in-memory store for sessions.
@@ -53,7 +57,8 @@ func (r *RequiredEnvStore) SetRequiredEnv(requiredEnv RequiredEnv) {
5357

5458
var RequiredEnvStoreObj *RequiredEnvStore
5559

56-
// InitRequiredEnv to initialize EnvData and through error if required env are not present
60+
// InitRequiredEnv to initialize EnvData and throw error if required env are not present
61+
// This includes env that only configurable via env vars and not the ui
5762
func InitRequiredEnv() error {
5863
envPath := os.Getenv(constants.EnvKeyEnvPath)
5964

@@ -85,6 +90,9 @@ func InitRequiredEnv() error {
8590
dbCACert := os.Getenv(constants.EnvKeyDatabaseCACert)
8691
redisURL := os.Getenv(constants.EnvKeyRedisURL)
8792
disableRedisForEnv := os.Getenv(constants.EnvKeyDisableRedisForEnv) == "true"
93+
awsRegion := os.Getenv(constants.EnvAwsRegion)
94+
awsAccessKeyID := os.Getenv(constants.EnvAwsAccessKeyID)
95+
awsSecretAccessKey := os.Getenv(constants.EnvAwsSecretAccessKey)
8896

8997
if strings.TrimSpace(redisURL) == "" {
9098
if cli.ARG_REDIS_URL != nil && *cli.ARG_REDIS_URL != "" {
@@ -139,7 +147,10 @@ func InitRequiredEnv() error {
139147
DatabaseCertKey: dbCertKey,
140148
DatabaseCACert: dbCACert,
141149
RedisURL: redisURL,
142-
disableRedisForEnv: disableRedisForEnv,
150+
DisableRedisForEnv: disableRedisForEnv,
151+
AwsRegion: awsRegion,
152+
AwsAccessKeyID: awsAccessKeyID,
153+
AwsSecretAccessKey: awsSecretAccessKey,
143154
}
144155

145156
RequiredEnvStoreObj = &RequiredEnvStore{

server/test/resolvers_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"strings"
78
"testing"
@@ -20,7 +21,7 @@ func TestResolvers(t *testing.T) {
2021
constants.DbTypeArangodb: "http://localhost:8529",
2122
constants.DbTypeMongodb: "mongodb://localhost:27017",
2223
constants.DbTypeScyllaDB: "127.0.0.1:9042",
23-
constants.DbTypeDynamoDB: "http://127.0.0.1:8000",
24+
constants.DbTypeDynamoDB: "http://0.0.0.0:8000",
2425
}
2526

2627
testDBs := strings.Split(os.Getenv("TEST_DBS"), ",")
@@ -52,6 +53,12 @@ func TestResolvers(t *testing.T) {
5253
os.Setenv(constants.EnvKeyDatabaseURL, dbURL)
5354
os.Setenv(constants.EnvKeyDatabaseType, dbType)
5455
os.Setenv(constants.EnvKeyDatabaseName, testDb)
56+
57+
if dbType == constants.DbTypeDynamoDB {
58+
memorystore.Provider.UpdateEnvVariable(constants.EnvAwsRegion, "ap-south-1")
59+
os.Setenv(constants.EnvAwsRegion, "ap-south-1")
60+
}
61+
5562
memorystore.InitRequiredEnv()
5663

5764
err := db.InitDB()
@@ -61,12 +68,15 @@ func TestResolvers(t *testing.T) {
6168

6269
// clean the persisted config for test to use fresh config
6370
envData, err := db.Provider.GetEnv(ctx)
64-
if err == nil {
71+
fmt.Println("envData", envData.ID, envData.EnvData)
72+
if err == nil && envData.ID != "" {
6573
envData.EnvData = ""
6674
_, err = db.Provider.UpdateEnv(ctx, envData)
6775
if err != nil {
6876
t.Errorf("Error updating env: %s", err.Error())
6977
}
78+
} else if err != nil {
79+
t.Errorf("Error getting env: %s", err.Error())
7080
}
7181
err = env.PersistEnv()
7282
if err != nil {

server/test/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func testSetup() TestSetup {
8484
testData := TestData{
8585
Email: fmt.Sprintf("%[email protected]", time.Now().Unix()),
8686
Password: "Test@123",
87-
WebhookEndpoint: "https://62cbc6738042b16aa7c22df2.mockapi.io/api/v1/webhook",
87+
WebhookEndpoint: "https://62f93101e05644803533cf36.mockapi.io/authorizer/webhook",
8888
TestWebhookEventTypes: []string{constants.UserAccessEnabledWebhookEvent, constants.UserAccessRevokedWebhookEvent, constants.UserCreatedWebhookEvent, constants.UserDeletedWebhookEvent, constants.UserLoginWebhookEvent, constants.UserSignUpWebhookEvent},
8989
TestEmailTemplateEventTypes: []string{constants.VerificationTypeBasicAuthSignup, constants.VerificationTypeForgotPassword, constants.VerificationTypeMagicLinkLogin, constants.VerificationTypeUpdateEmail},
9090
}

0 commit comments

Comments
 (0)