fix test cases for user data
This commit is contained in:
@@ -63,7 +63,7 @@ func (p *provider) UpsertOTP(ctx context.Context, otpParam *models.OTP) (*models
|
||||
return otp, err
|
||||
}
|
||||
} else {
|
||||
query := fmt.Sprintf(`UPDATE auth._default.%s SET otp = '%s', expires_at = %d, updated_at = %d WHERE id = '%s'`, models.Collections.OTP, otp.Otp, otp.ExpiresAt, otp.UpdatedAt, otp.ID)
|
||||
query := fmt.Sprintf(`UPDATE auth._default.%s SET otp="%s", expires_at=%d, updated_at=%d WHERE _id="%s"`, models.Collections.OTP, otp.Otp, otp.ExpiresAt, otp.UpdatedAt, otp.ID)
|
||||
scope := p.db.Scope("_default")
|
||||
_, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
if err != nil {
|
||||
|
@@ -1,12 +1,10 @@
|
||||
package couchbase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/db/models"
|
||||
"github.com/authorizerdev/authorizer/server/memorystore"
|
||||
"github.com/couchbase/gocb/v2"
|
||||
)
|
||||
@@ -19,12 +17,12 @@ type provider struct {
|
||||
// NewProvider returns a new SQL provider
|
||||
// TODO change following provider to new db provider
|
||||
func NewProvider() (*provider, error) {
|
||||
scopeName := os.Getenv(constants.EnvCouchbaseScope)
|
||||
// scopeName := os.Getenv(constants.EnvCouchbaseScope)
|
||||
bucketName := os.Getenv(constants.EnvCouchbaseBucket)
|
||||
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
||||
userName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseUsername
|
||||
password := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabasePassword
|
||||
|
||||
fmt.Println("dbURL", dbURL, userName, password)
|
||||
opts := gocb.ClusterOptions{
|
||||
Username: userName,
|
||||
Password: password,
|
||||
@@ -35,20 +33,28 @@ func NewProvider() (*provider, error) {
|
||||
return nil, err
|
||||
}
|
||||
bucket := cluster.Bucket(bucketName)
|
||||
// fmt.Println("1 called in oprovuider")
|
||||
|
||||
v := reflect.ValueOf(models.Collections)
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
user := gocb.CollectionSpec{
|
||||
Name: field.String(),
|
||||
ScopeName: scopeName,
|
||||
}
|
||||
collectionOpts := gocb.CreateCollectionOptions{
|
||||
Context: context.TODO(),
|
||||
}
|
||||
_ = bucket.Collections().CreateCollection(user, &collectionOpts)
|
||||
}
|
||||
// v := reflect.ValueOf(models.Collections)
|
||||
// fmt.Println("called in v", v)
|
||||
|
||||
// for i := 0; i < v.NumField(); i++ {
|
||||
|
||||
// field := v.Field(i)
|
||||
// fmt.Println("called in v", field)
|
||||
|
||||
// user := gocb.CollectionSpec{
|
||||
// Name: field.String(),
|
||||
// ScopeName: scopeName,
|
||||
// }
|
||||
// collectionOpts := gocb.CreateCollectionOptions{
|
||||
// Context: context.TODO(),
|
||||
// }
|
||||
// err = bucket.Collections().CreateCollection(user, &collectionOpts)
|
||||
// fmt.Println("2 called in oprovuider", err)
|
||||
|
||||
// }
|
||||
// fmt.Println("called in oprovuider")
|
||||
return &provider{
|
||||
db: bucket,
|
||||
}, nil
|
||||
|
@@ -80,9 +80,12 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
|
||||
paginationClone := pagination
|
||||
|
||||
inventoryScope := p.db.Scope("_default")
|
||||
userQuery := fmt.Sprintf("SELECT * FROM auth._default.%s ORDER BY id OFFSET %d LIMIT %d", models.Collections.User, paginationClone.Offset, paginationClone.Limit)
|
||||
userQuery := fmt.Sprintf("SELECT _id, email, email_verified_at, `password`, signup_methods, given_name, family_name, middle_name, nickname, birthdate, phone_number, phone_number_verified_at, picture, roles, revoked_timestamp, is_multi_factor_auth_enabled, created_at, updated_at FROM auth._default.%s ORDER BY id OFFSET %d LIMIT %d", models.Collections.User, paginationClone.Offset, paginationClone.Limit)
|
||||
|
||||
queryResult, err := inventoryScope.Query(userQuery, &gocb.QueryOptions{})
|
||||
queryResult, err := inventoryScope.Query(userQuery, &gocb.QueryOptions{
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
Context: ctx,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -113,7 +116,10 @@ func (p *provider) GetUserByEmail(ctx context.Context, email string) (models.Use
|
||||
user := models.User{}
|
||||
scope := p.db.Scope("_default")
|
||||
query := fmt.Sprintf("SELECT _id, email, email_verified_at, `password`, signup_methods, given_name, family_name, middle_name, nickname, birthdate, phone_number, phone_number_verified_at, picture, roles, revoked_timestamp, is_multi_factor_auth_enabled, created_at, updated_at FROM auth._default.%s WHERE email = '%s' LIMIT 1", models.Collections.User, email)
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
Context: ctx,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return user, err
|
||||
@@ -131,7 +137,10 @@ func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, err
|
||||
user := models.User{}
|
||||
scope := p.db.Scope("_default")
|
||||
query := fmt.Sprintf("SELECT _id, email, email_verified_at, `password`, signup_methods, given_name, family_name, middle_name, nickname, birthdate, phone_number, phone_number_verified_at, picture, roles, revoked_timestamp, is_multi_factor_auth_enabled, created_at, updated_at FROM auth._default.%s WHERE _id = '%s' LIMIT 1", models.Collections.User, id)
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
Context: ctx,
|
||||
})
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
@@ -150,7 +159,7 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
|
||||
data["updated_at"] = time.Now().Unix()
|
||||
inventoryScope := p.db.Scope("_default")
|
||||
|
||||
updateFields := ""
|
||||
upf := ""
|
||||
for key, value := range data {
|
||||
if key == "_id" {
|
||||
continue
|
||||
@@ -161,30 +170,37 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
|
||||
}
|
||||
|
||||
if value == nil {
|
||||
updateFields += fmt.Sprintf("%s = null,", key)
|
||||
upf += fmt.Sprintf("%s = null,", key)
|
||||
continue
|
||||
}
|
||||
|
||||
valueType := reflect.TypeOf(value)
|
||||
if valueType.Name() == "string" {
|
||||
updateFields += fmt.Sprintf("%s = '%s', ", key, value.(string))
|
||||
upf += fmt.Sprintf("%s = '%s', ", key, value.(string))
|
||||
} else {
|
||||
updateFields += fmt.Sprintf("%s = %v, ", key, value)
|
||||
upf += fmt.Sprintf("%s = %v, ", key, value)
|
||||
}
|
||||
}
|
||||
|
||||
updateFields := removeLastRune(upf)
|
||||
if ids != nil && len(ids) > 0 {
|
||||
for _, v := range ids {
|
||||
userQuery := fmt.Sprintf("UPDATE auth._default.%s SET %s WHERE id = '%s'", models.Collections.User, updateFields, v)
|
||||
userQuery := fmt.Sprintf("UPDATE auth._default.%s SET %s WHERE _id = '%s'", models.Collections.User, updateFields, v)
|
||||
|
||||
_, err := inventoryScope.Query(userQuery, &gocb.QueryOptions{})
|
||||
_, err := inventoryScope.Query(userQuery, &gocb.QueryOptions{
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
Context: ctx,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
userQuery := fmt.Sprintf("UPDATE auth._default.%s SET %s WHERE id IS NOT NULL", models.Collections.User, updateFields)
|
||||
_, err := inventoryScope.Query(userQuery, &gocb.QueryOptions{})
|
||||
userQuery := fmt.Sprintf("UPDATE auth._default.%s SET %s WHERE _id IS NOT NULL", models.Collections.User, updateFields)
|
||||
_, err := inventoryScope.Query(userQuery, &gocb.QueryOptions{
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
Context: ctx,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -192,3 +208,7 @@ func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{},
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeLastRune(s string) string {
|
||||
return s[:len(s)-2]
|
||||
}
|
||||
|
@@ -43,6 +43,7 @@ func (p *provider) GetVerificationRequestByToken(ctx context.Context, token stri
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
PositionalParameters: []interface{}{token},
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -66,6 +67,7 @@ func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email stri
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
PositionalParameters: []interface{}{email, identifier},
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
verificationRequest := models.VerificationRequest{}
|
||||
|
||||
@@ -88,7 +90,10 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination mode
|
||||
paginationClone := pagination
|
||||
|
||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM auth._default.%s OFFSET %d LIMIT %d", models.Collections.VerificationRequest, paginationClone.Offset, paginationClone.Limit)
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@@ -79,7 +79,10 @@ func (p *provider) UpdateWebhook(ctx context.Context, webhook models.Webhook) (*
|
||||
updateFields = strings.TrimSuffix(updateFields, ",")
|
||||
|
||||
query := fmt.Sprintf("UPDATE auth._default.%s SET %s WHERE _id = '%s'", models.Collections.Webhook, updateFields, webhook.ID)
|
||||
_, err = scope.Query(query, &gocb.QueryOptions{})
|
||||
_, err = scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -95,7 +98,10 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
||||
paginationClone := pagination
|
||||
|
||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM auth._default.%s OFFSET %d LIMIT %d", models.Collections.Webhook, paginationClone.Offset, paginationClone.Limit)
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -124,7 +130,10 @@ func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model
|
||||
var webhook models.Webhook
|
||||
scope := p.db.Scope("_default")
|
||||
query := fmt.Sprintf(`SELECT _id, event_name, endpoint, headers, enabled, created_at, updated_at FROM auth._default.%s WHERE _id = '%s' LIMIT 1`, models.Collections.Webhook, webhookID)
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -142,7 +151,10 @@ func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string)
|
||||
var webhook models.Webhook
|
||||
scope := p.db.Scope("_default")
|
||||
query := fmt.Sprintf(`SELECT _id, event_name, endpoint, headers, enabled, created_at, updated_at FROM auth._default.%s WHERE event_name = '%s' LIMIT 1`, models.Collections.Webhook, eventName)
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
q, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -158,12 +170,26 @@ func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string)
|
||||
|
||||
// DeleteWebhook to delete webhook
|
||||
func (p *provider) DeleteWebhook(ctx context.Context, webhook *model.Webhook) error {
|
||||
fmt.Println("trying to dlete webhooks logs", webhook.EventName)
|
||||
scope := p.db.Scope("_default")
|
||||
removeOpt := gocb.RemoveOptions{
|
||||
Context: ctx,
|
||||
}
|
||||
_, err := p.db.Collection(models.Collections.Webhook).Remove(webhook.ID, &removeOpt)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`DELETE FROM auth._default.%s WHERE webhook_id=%s`, models.Collections.WebhookLog, webhook.ID)
|
||||
fmt.Println("")
|
||||
_, err = scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -39,7 +39,10 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Paginat
|
||||
scope := p.db.Scope("_default")
|
||||
paginationClone := pagination
|
||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM auth._default.%s OFFSET %d LIMIT %d", models.Collections.Env, paginationClone.Offset, paginationClone.Limit)
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{})
|
||||
queryResult, err := scope.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user