Skip to content

Commit bedc3d0

Browse files
committed
fix: arangodb get one queries
1 parent 1398762 commit bedc3d0

File tree

17 files changed

+110
-73
lines changed

17 files changed

+110
-73
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ cmd:
66
clean:
77
rm -rf build
88
test:
9-
cd server && go test ./...
9+
cd server && go clean --testcache && go test ./...

server/constants/constants.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package constants
33
var (
44
ADMIN_SECRET = ""
55
ENV = ""
6+
ENV_PATH = ""
67
VERSION = ""
78
DATABASE_TYPE = ""
89
DATABASE_URL = ""
@@ -21,9 +22,9 @@ var (
2122
IS_PROD = false
2223
COOKIE_NAME = ""
2324
RESET_PASSWORD_URL = ""
24-
DISABLE_EMAIL_VERIFICATION = "false"
25-
DISABLE_BASIC_AUTHENTICATION = "false"
26-
DISABLE_MAGIC_LOGIN = "false"
25+
DISABLE_EMAIL_VERIFICATION = false
26+
DISABLE_BASIC_AUTHENTICATION = false
27+
DISABLE_MAGIC_LOGIN = false
2728

2829
// ROLES
2930
ROLES = []string{}

server/db/user.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (mgr *manager) GetUserByEmail(email string) (User, error) {
148148
}
149149

150150
if IsArangoDB {
151-
query := fmt.Sprintf("FOR d in %s FILTER d.email == @email LIMIT 1 RETURN d", Collections.User)
151+
query := fmt.Sprintf("FOR d in %s FILTER d.email == @email RETURN d", Collections.User)
152152
bindVars := map[string]interface{}{
153153
"email": email,
154154
}
@@ -160,10 +160,14 @@ func (mgr *manager) GetUserByEmail(email string) (User, error) {
160160
defer cursor.Close()
161161

162162
for {
163-
_, err := cursor.ReadDocument(nil, &user)
164-
if driver.IsNoMoreDocuments(err) {
163+
if !cursor.HasMore() {
164+
if user.Key == "" {
165+
return user, fmt.Errorf("user not found")
166+
}
165167
break
166-
} else if err != nil {
168+
}
169+
_, err := cursor.ReadDocument(nil, &user)
170+
if err != nil {
167171
return user, err
168172
}
169173
}
@@ -201,10 +205,14 @@ func (mgr *manager) GetUserByID(id string) (User, error) {
201205
}
202206

203207
for {
204-
_, err := cursor.ReadDocument(nil, &user)
205-
if driver.IsNoMoreDocuments(err) {
208+
if !cursor.HasMore() {
209+
if user.Key == "" {
210+
return user, fmt.Errorf("user not found")
211+
}
206212
break
207-
} else if err != nil {
213+
}
214+
_, err := cursor.ReadDocument(nil, &user)
215+
if err != nil {
208216
return user, err
209217
}
210218
}

server/db/verificationRequests.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,14 @@ func (mgr *manager) GetVerificationByToken(token string) (VerificationRequest, e
120120
defer cursor.Close()
121121

122122
for {
123-
_, err := cursor.ReadDocument(nil, &verification)
124-
125-
if driver.IsNoMoreDocuments(err) {
123+
if !cursor.HasMore() {
124+
if verification.Key == "" {
125+
return verification, fmt.Errorf("verification request not found")
126+
}
126127
break
127-
} else if err != nil {
128+
}
129+
_, err := cursor.ReadDocument(nil, &verification)
130+
if err != nil {
128131
return verification, err
129132
}
130133
}
@@ -157,14 +160,16 @@ func (mgr *manager) GetVerificationByEmail(email string) (VerificationRequest, e
157160
defer cursor.Close()
158161

159162
for {
160-
_, err := cursor.ReadDocument(nil, &verification)
161-
162-
if driver.IsNoMoreDocuments(err) {
163+
if !cursor.HasMore() {
164+
if verification.Key == "" {
165+
return verification, fmt.Errorf("verification request not found")
166+
}
163167
break
164-
} else if err != nil {
168+
}
169+
_, err := cursor.ReadDocument(nil, &verification)
170+
if err != nil {
165171
return verification, err
166172
}
167-
168173
}
169174
}
170175

server/env.go renamed to server/env/env.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package env
22

33
import (
44
"flag"
@@ -13,7 +13,7 @@ import (
1313

1414
// build variables
1515
var (
16-
Version string
16+
VERSION string
1717
ARG_DB_URL *string
1818
ARG_DB_TYPE *string
1919
ARG_AUTHORIZER_URL *string
@@ -22,23 +22,25 @@ var (
2222

2323
// InitEnv -> to initialize env and through error if required env are not present
2424
func InitEnv() {
25-
envPath := `.env`
25+
if constants.ENV_PATH == "" {
26+
constants.ENV_PATH = `.env`
27+
}
2628
ARG_DB_URL = flag.String("database_url", "", "Database connection string")
2729
ARG_DB_TYPE = flag.String("database_type", "", "Database type, possible values are postgres,mysql,sqlite")
2830
ARG_AUTHORIZER_URL = flag.String("authorizer_url", "", "URL for authorizer instance, eg: https://xyz.herokuapp.com")
2931
ARG_ENV_FILE = flag.String("env_file", "", "Env file path")
3032

3133
flag.Parse()
3234
if *ARG_ENV_FILE != "" {
33-
envPath = *ARG_ENV_FILE
35+
constants.ENV_PATH = *ARG_ENV_FILE
3436
}
3537

36-
err := godotenv.Load(envPath)
38+
err := godotenv.Load(constants.ENV_PATH)
3739
if err != nil {
38-
log.Println("error loading .env file")
40+
log.Printf("error loading %s file", constants.ENV_PATH)
3941
}
4042

41-
constants.VERSION = Version
43+
constants.VERSION = VERSION
4244
constants.ADMIN_SECRET = os.Getenv("ADMIN_SECRET")
4345
constants.ENV = os.Getenv("ENV")
4446
constants.DATABASE_TYPE = os.Getenv("DATABASE_TYPE")
@@ -63,9 +65,9 @@ func InitEnv() {
6365
constants.TWITTER_CLIENT_ID = os.Getenv("TWITTER_CLIENT_ID")
6466
constants.TWITTER_CLIENT_SECRET = os.Getenv("TWITTER_CLIENT_SECRET")
6567
constants.RESET_PASSWORD_URL = strings.TrimPrefix(os.Getenv("RESET_PASSWORD_URL"), "/")
66-
constants.DISABLE_BASIC_AUTHENTICATION = os.Getenv("DISABLE_BASIC_AUTHENTICATION")
67-
constants.DISABLE_EMAIL_VERIFICATION = os.Getenv("DISABLE_EMAIL_VERIFICATION")
68-
constants.DISABLE_MAGIC_LOGIN = os.Getenv("DISABLE_MAGIC_LOGIN")
68+
constants.DISABLE_BASIC_AUTHENTICATION = os.Getenv("DISABLE_BASIC_AUTHENTICATION") == "true"
69+
constants.DISABLE_EMAIL_VERIFICATION = os.Getenv("DISABLE_EMAIL_VERIFICATION") == "true"
70+
constants.DISABLE_MAGIC_LOGIN = os.Getenv("DISABLE_MAGIC_LOGIN") == "true"
6971
constants.JWT_ROLE_CLAIM = os.Getenv("JWT_ROLE_CLAIM")
7072

7173
if constants.ADMIN_SECRET == "" {
@@ -128,21 +130,14 @@ func InitEnv() {
128130
constants.COOKIE_NAME = "authorizer"
129131
}
130132

131-
if constants.DISABLE_BASIC_AUTHENTICATION == "" {
132-
constants.DISABLE_BASIC_AUTHENTICATION = "false"
133-
}
134-
135-
if constants.DISABLE_MAGIC_LOGIN == "" {
136-
constants.DISABLE_MAGIC_LOGIN = "false"
137-
}
138-
139133
if constants.SMTP_HOST == "" || constants.SENDER_EMAIL == "" || constants.SENDER_PASSWORD == "" {
140-
constants.DISABLE_EMAIL_VERIFICATION = "true"
141-
} else if constants.DISABLE_EMAIL_VERIFICATION == "" {
142-
constants.DISABLE_EMAIL_VERIFICATION = "false"
134+
constants.DISABLE_EMAIL_VERIFICATION = true
135+
constants.DISABLE_MAGIC_LOGIN = true
143136
}
144137

145-
log.Println("email verification disabled:", constants.DISABLE_EMAIL_VERIFICATION)
138+
if constants.DISABLE_EMAIL_VERIFICATION {
139+
constants.DISABLE_MAGIC_LOGIN = true
140+
}
146141

147142
rolesSplit := strings.Split(os.Getenv("ROLES"), ",")
148143
roles := []string{}

server/env/env_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package env
2+
3+
import (
4+
"testing"
5+
6+
"github.com/authorizerdev/authorizer/server/constants"
7+
"github.com/authorizerdev/authorizer/server/enum"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestEnvs(t *testing.T) {
12+
constants.ENV_PATH = "../../.env.sample"
13+
InitEnv()
14+
15+
assert.Equal(t, constants.ADMIN_SECRET, "admin")
16+
assert.Equal(t, constants.ENV, "production")
17+
assert.Equal(t, constants.DATABASE_URL, "data.db")
18+
assert.Equal(t, constants.DATABASE_TYPE, enum.Sqlite.String())
19+
assert.True(t, constants.DISABLE_EMAIL_VERIFICATION)
20+
assert.True(t, constants.DISABLE_MAGIC_LOGIN)
21+
assert.False(t, constants.DISABLE_BASIC_AUTHENTICATION)
22+
assert.Equal(t, constants.JWT_TYPE, "HS256")
23+
assert.Equal(t, constants.JWT_SECRET, "random_string")
24+
assert.Equal(t, constants.JWT_ROLE_CLAIM, "role")
25+
assert.EqualValues(t, constants.ROLES, []string{"user"})
26+
assert.EqualValues(t, constants.DEFAULT_ROLES, []string{"user"})
27+
assert.EqualValues(t, constants.PROTECTED_ROLES, []string{"admin"})
28+
assert.EqualValues(t, constants.ALLOWED_ORIGINS, []string{"*"})
29+
}

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2121
github.com/modern-go/reflect2 v1.0.1 // indirect
2222
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
23+
github.com/stretchr/testify v1.7.0 // indirect
2324
github.com/ugorji/go v1.2.6 // indirect
2425
github.com/vektah/gqlparser/v2 v2.1.0
2526
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519

server/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
321321
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
322322
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
323323
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
324+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
324325
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
325326
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
326327
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

server/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/authorizerdev/authorizer/server/constants"
88
"github.com/authorizerdev/authorizer/server/db"
9+
"github.com/authorizerdev/authorizer/server/env"
910
"github.com/authorizerdev/authorizer/server/handlers"
1011
"github.com/authorizerdev/authorizer/server/oauth"
1112
"github.com/authorizerdev/authorizer/server/session"
@@ -48,7 +49,7 @@ func CORSMiddleware() gin.HandlerFunc {
4849
}
4950

5051
func main() {
51-
InitEnv()
52+
env.InitEnv()
5253
db.InitDB()
5354
session.InitSession()
5455
oauth.InitOAuth()

server/resolvers/forgotPassword.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func ForgotPassword(ctx context.Context, params model.ForgotPasswordInput) (*mod
2020
if err != nil {
2121
return res, err
2222
}
23-
if constants.DISABLE_BASIC_AUTHENTICATION == "true" {
23+
if constants.DISABLE_BASIC_AUTHENTICATION {
2424
return res, fmt.Errorf(`basic authentication is disabled for this instance`)
2525
}
2626
host := gc.Request.Host

0 commit comments

Comments
 (0)