Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dstpierre committed Aug 31, 2023
2 parents 069528b + e69f1f3 commit 162b9ae
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 5 deletions.
8 changes: 5 additions & 3 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestCacheSubscribeOnDBEvent(t *testing.T) {
t.Fatal(err.Error())
}

payload := model.Command{Type: model.MsgTypeDBUpdated, Data: document, Channel: "random_cahn"}
payload := model.Command{Type: model.MsgTypeDBUpdated, Data: document, Channel: "random_cahn", Base: "unittest"}

go tc.cache.Subscribe(receiver, "token", payload.Channel, closeCn)

Expand Down Expand Up @@ -186,7 +186,7 @@ func TestCachePublishDocument(t *testing.T) {
t.Fatal(err.Error())
}

payload := model.Command{Type: model.MsgTypeDBUpdated, Data: document, Channel: "random_cahn"}
payload := model.Command{Type: model.MsgTypeDBUpdated, Data: document, Channel: "random_cahn", Base: "unittest"}

// convert to map for simulation of real usage
var documentMap map[string]interface{}
Expand All @@ -203,8 +203,10 @@ func TestCachePublishDocument(t *testing.T) {
defer timer.Stop()
select {
case res := <-receiver:
if res != payload {
if res.Type != payload.Type || res.Channel != payload.Channel {
t.Error("Incorrect message is received")
t.Log(res)
t.Log(payload)
}
break
case <-timer.C:
Expand Down
9 changes: 9 additions & 0 deletions database/memory/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func TestUserEmailExists(t *testing.T) {
t.Errorf("email should exists")
}
}

func TestAccountList(t *testing.T) {
accts, err := datastore.ListAccounts(confDBName)
if err != nil {
t.Fatal(err)
} else if len(accts) == 0 {
t.Errorf("expected at least 1 account, got %d", len(accts))
}
}
19 changes: 19 additions & 0 deletions database/memory/sb.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ func (m *Memory) GetTenantByStripeID(stripeID string) (cus model.Tenant, err err
return
}

func (m *Memory) GetTenantByEmail(email string) (cus model.Tenant, err error) {
list, err := all[model.Tenant](m, "sb", "customers")
if err != nil {
return
}

results := filter(list, func(x model.Tenant) bool {
return strings.EqualFold(x.Email, email)
})

if len(results) != 1 {
err = fmt.Errorf("cannot find customer by email %s", email)
return
}

cus = results[0]
return
}

func (m *Memory) ActivateTenant(tenantID string, active bool) error {
var cus model.Tenant
if err := getByID(m, "sb", "customers", tenantID, &cus); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions database/memory/sb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,12 @@ func TestEnableExternalLogins(t *testing.T) {
t.Errorf("expected 'key' and 'secret' got %v", decrypted["twitter"])
}
}

func TestGetTenantByEmail(t *testing.T) {
cus, err := datastore.GetTenantByEmail(adminEmail)
if err != nil {
t.Fatal(err)
} else if cus.Email != adminEmail {
t.Errorf("expected same email for found customer")
}
}
9 changes: 9 additions & 0 deletions database/mongo/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func TestUserEmailExists(t *testing.T) {
t.Errorf("email should exists")
}
}

func TestAccountList(t *testing.T) {
accts, err := datastore.ListAccounts(confDBName)
if err != nil {
t.Fatal(err)
} else if len(accts) == 0 {
t.Errorf("expected at least 1 account, got %d", len(accts))
}
}
15 changes: 15 additions & 0 deletions database/mongo/sb.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ func (mg *Mongo) GetTenantByStripeID(stripeID string) (cus model.Tenant, err err
return
}

func (mg *Mongo) GetTenantByEmail(email string) (cus model.Tenant, err error) {
db := mg.Client.Database("sbsys")

var acct LocalCustomer
sr := db.Collection("accounts").FindOne(mg.Ctx, bson.M{"email": email})
if err = sr.Decode(&acct); err != nil {
return
} else if err = sr.Err(); err != nil {
return
}

cus = fromLocalCustomer(acct)
return
}

func (mg *Mongo) IncrementMonthlyEmailSent(baseID string) error {
db := mg.Client.Database("sbsys")

Expand Down
9 changes: 9 additions & 0 deletions database/mongo/sb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,12 @@ func TestEnableExternalLogins(t *testing.T) {
t.Errorf("expected 'key' and 'secret' got %v", decrypted["twitter"])
}
}

func TestGetTenantByEmail(t *testing.T) {
cus, err := datastore.GetTenantByEmail(adminEmail)
if err != nil {
t.Fatal(err)
} else if cus.Email != adminEmail {
t.Errorf("expected same email for found customer")
}
}
2 changes: 2 additions & 0 deletions database/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Persister interface {
ListDatabases() ([]model.DatabaseConfig, error)
// IncrementMonthlyEmailSent increments the monthly email sending counter
IncrementMonthlyEmailSent(baseID string) error
// GetTenantByEmail finds a tenant by its main account email
GetTenantByEmail(email string) (cus model.Tenant, err error)
// GetTenantByStripeID finds a tenant by its Stripe customer ID
GetTenantByStripeID(stripeID string) (cus model.Tenant, err error)
// ActivateTenant turns the IsActive flag for the tenant and its database
Expand Down
2 changes: 1 addition & 1 deletion database/postgresql/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (pg *PostgreSQL) GetRootForBase(dbName string) (tok model.User, err error)
func (pg *PostgreSQL) ListAccounts(dbName string) ([]model.Account, error) {
qry := fmt.Sprintf(`
SELECT *
FROM %s_sb.accounts
FROM %s.sb_accounts
ORDER BY created DESC;
`, dbName)

Expand Down
9 changes: 9 additions & 0 deletions database/postgresql/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func TestUserEmailExists(t *testing.T) {
t.Errorf("email should exists")
}
}

func TestAccountList(t *testing.T) {
accts, err := datastore.ListAccounts(confDBName)
if err != nil {
t.Fatal(err)
} else if len(accts) == 0 {
t.Errorf("expected at least 1 account, got %d", len(accts))
}
}
11 changes: 11 additions & 0 deletions database/postgresql/sb.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ func (pg *PostgreSQL) GetTenantByStripeID(stripeID string) (cus model.Tenant, er
return
}

func (pg *PostgreSQL) GetTenantByEmail(email string) (cus model.Tenant, err error) {
row := pg.DB.QueryRow(`
SELECT *
FROM sb.customers
WHERE email = $1
`, email)

err = scanCustomer(row, &cus)
return
}

func (pg *PostgreSQL) ActivateTenant(tenantID string, active bool) error {
tx, err := pg.DB.Begin()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions database/postgresql/sb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,12 @@ func TestEnableExternalLogins(t *testing.T) {
t.Errorf("expected 'key' and 'secret' got %v", decrypted["twitter"])
}
}

func TestGetTenantByEmail(t *testing.T) {
cus, err := datastore.GetTenantByEmail(adminEmail)
if err != nil {
t.Fatal(err)
} else if cus.Email != adminEmail {
t.Errorf("expected same email for found customer")
}
}
9 changes: 9 additions & 0 deletions database/sqlite/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ func TestUserEmailExists(t *testing.T) {
t.Errorf("email should exists")
}
}

func TestAccountList(t *testing.T) {
accts, err := datastore.ListAccounts(confDBName)
if err != nil {
t.Fatal(err)
} else if len(accts) == 0 {
t.Errorf("expected at least 1 account, got %d", len(accts))
}
}
11 changes: 11 additions & 0 deletions database/sqlite/sb.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ func (sl *SQLite) GetTenantByStripeID(stripeID string) (cus model.Tenant, err er
return
}

func (sl *SQLite) GetTenantByEmail(email string) (cus model.Tenant, err error) {
row := sl.DB.QueryRow(`
SELECT *
FROM sb_customers
WHERE email = $1
`, email)

err = scanCustomer(row, &cus)
return
}

func (sl *SQLite) ActivateTenant(tenantID string, active bool) error {
tx, err := sl.DB.Begin()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions database/sqlite/sb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,12 @@ func TestEnableExternalLogins(t *testing.T) {
t.Errorf("expected 'key' and 'secret' got %v", decrypted["twitter"])
}
}

func TestGetTenantByEmail(t *testing.T) {
cus, err := datastore.GetTenantByEmail(adminEmail)
if err != nil {
t.Fatal(err)
} else if cus.Email != adminEmail {
t.Errorf("expected same email for found customer")
}
}
7 changes: 7 additions & 0 deletions function/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ func (sub *Subscriber) handleRealtimeEvents(msg model.Command) {

var ids []string

// for msg type error, we do nothing
if msg.Type == model.MsgTypeError {
sub.Log.Err(err).Msg("receiving msg of type error")
return
}

key := fmt.Sprintf("%s:%s", exe.BaseName, msg.Channel)
if err := sub.PubSub.GetTyped(key, &ids); err != nil {
funcs, err := exe.DataStore.ListFunctionsByTrigger(exe.BaseName, msg.Channel)
if err != nil {
sub.Log.Error().Err(err).Msg("error getting functions by trigger")
sub.Log.Debug().Msg("channgel: " + msg.Channel + " type: " + msg.Type)
return
}

Expand Down
12 changes: 11 additions & 1 deletion functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ func TestFunctionTriggerByPublishingMsg(t *testing.T) {
}

func TestFunctionWithVolatilizerHelpers(t *testing.T) {
// Remove the counter
toRemove, err := backend.Cache.Inc("some-counter", 1)
if err != nil {
t.Fatal(err)
} else if toRemove > 0 {
if _, err := backend.Cache.Dec("some-counter", toRemove); err != nil {
t.Fatal(err)
}
}

code := `
function handle(channel, type, body) {
// set some value in the cache
Expand All @@ -463,7 +473,7 @@ func TestFunctionWithVolatilizerHelpers(t *testing.T) {
return;
}
cacheSet()
// cacheSet()
res = inc("some-counter", 10);
if (!res.ok) {
Expand Down

0 comments on commit 162b9ae

Please sign in to comment.