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

Commit a920a91

Browse files
committed
changed account and membership to get their dependencies from struct, made unit test pass
1 parent f13699f commit a920a91

File tree

14 files changed

+124
-109
lines changed

14 files changed

+124
-109
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ deploy:
1313
scp -qr ./templates/* sb-poc:/home/dstpierre/templates/
1414

1515
test:
16-
@JWT_SECRET=okdevmode go test -v --race --cover ./...
16+
@JWT_SECRET=okdevmode go test --race --cover ./...
1717

1818
docker:
1919
docker build . -t staticbackend:latest

account.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ var (
2727
letterRunes = []rune("abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679")
2828
)
2929

30-
type accounts struct{}
30+
type accounts struct {
31+
membership *membership
32+
}
3133

3234
func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
3335
var email string
@@ -145,7 +147,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
145147
db = client.Database(dbName)
146148
pw := randStringRunes(6)
147149

148-
if _, _, err := createAccountAndUser(db, email, pw, 100); err != nil {
150+
if _, _, err := a.membership.createAccountAndUser(db, email, pw, 100); err != nil {
149151
http.Error(w, err.Error(), http.StatusInternalServerError)
150152
return
151153
}

db_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ func dbPost(t *testing.T, hf func(http.ResponseWriter, *http.Request), repo stri
4040
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", tok))
4141

4242
stdAuth := []middleware.Middleware{
43-
middleware.WithDB(database.client),
44-
middleware.RequireAuth(database.client),
43+
middleware.WithDB(database.client, volatile),
44+
middleware.RequireAuth(database.client, volatile),
4545
}
4646
if params[0] {
4747
stdAuth = []middleware.Middleware{
48-
middleware.WithDB(client),
48+
middleware.WithDB(client, volatile),
4949
middleware.RequireRoot(client),
5050
}
5151
}
@@ -128,7 +128,7 @@ func TestDBListCollections(t *testing.T) {
128128
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rootToken))
129129

130130
stdRoot := []middleware.Middleware{
131-
middleware.WithDB(database.client),
131+
middleware.WithDB(database.client, volatile),
132132
middleware.RequireRoot(database.client),
133133
}
134134
h := middleware.Chain(http.HandlerFunc(database.listCollections), stdRoot...)

function/management.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func GetForExecution(db *mongo.Database, name string) (ExecData, error) {
8181

8282
ctx := context.Background()
8383
opt := &options.FindOneOptions{}
84-
opt.SetProjection(bson.M{"h": -1})
84+
opt.SetProjection(bson.M{"h": false})
8585

8686
sr := db.Collection("sb_functions").FindOne(ctx, filter, opt)
8787
if err := sr.Decode(&result); err != nil {

function/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (env *ExecutionEnvironment) addVolatileFunctions(vm *goja.Runtime) {
347347

348348
func (env *ExecutionEnvironment) complete(err error) {
349349
env.CurrentRun.Completed = time.Now()
350-
env.CurrentRun.Success = err == mongo.ErrNilCursor
350+
env.CurrentRun.Success = err == nil
351351

352352
env.CurrentRun.Output = append(env.CurrentRun.Output, "Function completed")
353353

function/scheduler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type MetaMessage struct {
4444
func (ts *TaskScheduler) Start() {
4545
tasks, err := ts.listTasks()
4646
if err != nil {
47-
log.Println("error loading tasks: %v", err)
47+
log.Println("error loading tasks: ", err)
4848
return
4949
}
5050

@@ -54,7 +54,7 @@ func (ts *TaskScheduler) Start() {
5454
for _, task := range tasks {
5555
_, err := ts.Scheduler.Cron(task.Interval).Tag(task.ID.Hex()).Do(ts.run, task)
5656
if err != nil {
57-
log.Println("error scheduling this task: %s -> %v", task.ID.Hex(), err)
57+
log.Printf("error scheduling this task: %s -> %v\n", task.ID.Hex(), err)
5858
}
5959
}
6060
}
@@ -123,7 +123,7 @@ func (ts *TaskScheduler) run(task Task) {
123123
}
124124

125125
if err := ts.Volatile.SetTyped("root:"+task.BaseName, auth); err != nil {
126-
log.Printf("error setting auth inside TaskScheduler.run: ", err)
126+
log.Println("error setting auth inside TaskScheduler.run: ", err)
127127
return
128128
}
129129
}

functions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package staticbackend
22

33
import (
4+
"fmt"
45
"net/http"
56
"staticbackend/db"
67
"staticbackend/function"
@@ -99,6 +100,9 @@ func (f *functions) exec(w http.ResponseWriter, r *http.Request) {
99100

100101
curDB := client.Database(conf.Name)
101102

103+
fmt.Println("DEBUG: search for name: ", data.FunctionName)
104+
fmt.Println("DEBUG: base: ", conf.Name)
105+
102106
fn, err := function.GetForExecution(curDB, data.FunctionName)
103107
if err != nil {
104108
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -111,6 +115,7 @@ func (f *functions) exec(w http.ResponseWriter, r *http.Request) {
111115
Base: f.base,
112116
Data: fn,
113117
}
118+
114119
if err := env.Execute(r); err != nil {
115120
http.Error(w, err.Error(), http.StatusInternalServerError)
116121
return

functions_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func TestFunctionsExecuteDBOperations(t *testing.T) {
1111
code := `
1212
log("works here");
13-
function handle() {
13+
function handle(body) {
1414
var o = {
1515
desc: "yep",
1616
done: false,
@@ -66,6 +66,7 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
6666
data := function.ExecData{
6767
FunctionName: "unittest",
6868
Code: code,
69+
TriggerTopic: "web",
6970
}
7071
addResp := dbPost(t, funexec.add, "", data, true)
7172
if addResp.StatusCode != http.StatusOK {
@@ -79,7 +80,7 @@ func TestFunctionsExecuteDBOperations(t *testing.T) {
7980
t.Errorf("add: expected status 200 got %s", addResp.Status)
8081
}
8182

82-
execResp := dbPost(t, funexec.exec, "", data)
83+
execResp := dbPost(t, funexec.exec, "", data, true)
8384
if execResp.StatusCode != http.StatusOK {
8485
b, err := io.ReadAll(execResp.Body)
8586
if err != nil {

main_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func TestMain(m *testing.M) {
4141
log.Fatal(err)
4242
}
4343

44-
deleteAndSetupTestAccount()
44+
volatile = cache.NewCache()
4545

46-
volatile := cache.NewCache()
46+
deleteAndSetupTestAccount()
4747

4848
hub := newHub(volatile)
4949
go hub.run()
@@ -107,8 +107,10 @@ func deleteAndSetupTestAccount() {
107107

108108
pubKey = base.ID.Hex()
109109

110+
m := &membership{volatile: volatile}
111+
110112
db := client.Database(dbName)
111-
token, dbToken, err := createAccountAndUser(db, admEmail, password, 100)
113+
token, dbToken, err := m.createAccountAndUser(db, admEmail, password, 100)
112114
if err != nil {
113115
log.Fatal(err)
114116
}
@@ -117,7 +119,7 @@ func deleteAndSetupTestAccount() {
117119

118120
rootToken = fmt.Sprintf("%s|%s|%s", dbToken.ID.Hex(), dbToken.AccountID.Hex(), dbToken.Token)
119121

120-
token, _, err = createUser(db, dbToken.AccountID, userEmail, userPassword, 0)
122+
token, _, err = m.createUser(db, dbToken.AccountID, userEmail, userPassword, 0)
121123
if err != nil {
122124
log.Fatal(err)
123125
}

0 commit comments

Comments
 (0)