fix: create common resolver test suite

This commit is contained in:
Lakhan Samani 2021-12-23 14:17:44 +05:30
parent beae4502d4
commit 6e9370458b
10 changed files with 86 additions and 264 deletions

View File

@ -60,7 +60,7 @@ func (r *queryResolver) Meta(ctx context.Context) (*model.Meta, error) {
}
func (r *queryResolver) Session(ctx context.Context, roles []string) (*model.AuthResponse, error) {
return resolvers.Token(ctx, roles)
return resolvers.Session(ctx, roles)
}
func (r *queryResolver) Profile(ctx context.Context) (*model.User, error) {

View File

@ -13,7 +13,7 @@ import (
"github.com/authorizerdev/authorizer/server/utils"
)
func Token(ctx context.Context, roles []string) (*model.AuthResponse, error) {
func Session(ctx context.Context, roles []string) (*model.AuthResponse, error) {
var res *model.AuthResponse
gc, err := utils.GinContextFromContext(ctx)

View File

@ -3,7 +3,6 @@ package test
import (
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
"github.com/authorizerdev/authorizer/server/graph/model"
@ -11,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)
func commonForgotPasswordTest(s TestSetup, t *testing.T) {
func forgotPasswordTest(s TestSetup, t *testing.T) {
email := "forgot_password." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{
Email: email,
@ -31,35 +30,3 @@ func commonForgotPasswordTest(s TestSetup, t *testing.T) {
cleanData(email)
}
func TestForgotPassword(t *testing.T) {
s := testSetup()
defer s.Server.Close()
if s.TestInfo.ShouldExecuteForSQL {
t.Run("forgot password for sql dbs should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.SQL
constants.DATABASE_TYPE = enum.Sqlite.String()
db.InitDB()
commonForgotPasswordTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForArango {
t.Run("forgot password for arangodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.ArangoDB
constants.DATABASE_TYPE = enum.Arangodb.String()
db.InitDB()
commonForgotPasswordTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForMongo {
t.Run("forgot password for mongodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.MongoDB
constants.DATABASE_TYPE = enum.Mongodb.String()
db.InitDB()
commonForgotPasswordTest(s, t)
})
}
}

View File

@ -1,10 +1,8 @@
package test
import (
"log"
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
"github.com/authorizerdev/authorizer/server/graph/model"
@ -12,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)
func commonLoginTest(s TestSetup, t *testing.T) {
func loginTests(s TestSetup, t *testing.T) {
email := "login." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{
Email: email,
@ -50,41 +48,8 @@ func commonLoginTest(s TestSetup, t *testing.T) {
Password: s.TestInfo.Password,
})
log.Println("=> access token:", loginRes.AccessToken)
assert.Nil(t, err, "login successful")
assert.NotNil(t, loginRes.AccessToken, "access token should not be empty")
assert.Nil(t, loginRes.AccessToken, "access token should not be empty")
cleanData(email)
}
func TestLogin(t *testing.T) {
s := testSetup()
defer s.Server.Close()
if s.TestInfo.ShouldExecuteForSQL {
t.Run("login for sql dbs should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.SQL
constants.DATABASE_TYPE = enum.Sqlite.String()
db.InitDB()
commonLoginTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForArango {
t.Run("login for arangodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.ArangoDB
constants.DATABASE_TYPE = enum.Arangodb.String()
db.InitDB()
commonLoginTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForMongo {
t.Run("login for mongodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.MongoDB
constants.DATABASE_TYPE = enum.Mongodb.String()
db.InitDB()
commonLoginTest(s, t)
})
}
}

View File

@ -3,15 +3,13 @@ package test
import (
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
"github.com/authorizerdev/authorizer/server/graph/model"
"github.com/authorizerdev/authorizer/server/resolvers"
"github.com/stretchr/testify/assert"
)
func commonResendVerifyEmailTest(s TestSetup, t *testing.T) {
func resendVerifyEmailTests(s TestSetup, t *testing.T) {
email := "resend_verify_email." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{
Email: email,
@ -28,35 +26,3 @@ func commonResendVerifyEmailTest(s TestSetup, t *testing.T) {
cleanData(email)
}
func TestResendVerifyEmail(t *testing.T) {
s := testSetup()
defer s.Server.Close()
if s.TestInfo.ShouldExecuteForSQL {
t.Run("resend verify email for sql dbs should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.SQL
constants.DATABASE_TYPE = enum.Sqlite.String()
db.InitDB()
commonResendVerifyEmailTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForArango {
t.Run("resend verify email for arangodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.ArangoDB
constants.DATABASE_TYPE = enum.Arangodb.String()
db.InitDB()
commonResendVerifyEmailTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForMongo {
t.Run("resend verify email for mongodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.MongoDB
constants.DATABASE_TYPE = enum.Mongodb.String()
db.InitDB()
commonResendVerifyEmailTest(s, t)
})
}
}

View File

@ -3,7 +3,6 @@ package test
import (
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
"github.com/authorizerdev/authorizer/server/graph/model"
@ -11,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)
func commonResetPasswordTest(s TestSetup, t *testing.T) {
func resetPasswordTest(s TestSetup, t *testing.T) {
email := "reset_password." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{
Email: email,
@ -45,35 +44,3 @@ func commonResetPasswordTest(s TestSetup, t *testing.T) {
cleanData(email)
}
func TestResetPassword(t *testing.T) {
s := testSetup()
defer s.Server.Close()
if s.TestInfo.ShouldExecuteForSQL {
t.Run("reset password for sql dbs should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.SQL
constants.DATABASE_TYPE = enum.Sqlite.String()
db.InitDB()
commonResetPasswordTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForArango {
t.Run("reset password for arangodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.ArangoDB
constants.DATABASE_TYPE = enum.Arangodb.String()
db.InitDB()
commonResetPasswordTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForMongo {
t.Run("reset password for mongodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.MongoDB
constants.DATABASE_TYPE = enum.Mongodb.String()
db.InitDB()
commonResetPasswordTest(s, t)
})
}
}

View File

@ -0,0 +1,36 @@
package test
import (
"log"
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
)
func TestResolvers(t *testing.T) {
databases := map[string]string{
enum.Sqlite.String(): "../../data.db",
enum.Arangodb.String(): "http://root:root@localhost:8529",
enum.Mongodb.String(): "mongodb://localhost:27017",
}
log.Println("==== Testing resolvers =====")
for dbType, dbURL := range databases {
constants.DATABASE_URL = dbURL
constants.DATABASE_TYPE = dbType
db.InitDB()
s := testSetup()
defer s.Server.Close()
t.Run("running test cases for "+dbType, func(t *testing.T) {
loginTests(s, t)
signupTests(s, t)
forgotPasswordTest(s, t)
resendVerifyEmailTests(s, t)
resetPasswordTest(s, t)
verifyEmailTest(s, t)
})
}
}

View File

@ -3,7 +3,6 @@ package test
import (
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
"github.com/authorizerdev/authorizer/server/graph/model"
@ -11,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)
func commonSignupTest(s TestSetup, t *testing.T) {
func signupTests(s TestSetup, t *testing.T) {
email := "signup." + s.TestInfo.Email
res, err := resolvers.Signup(s.Ctx, model.SignUpInput{
Email: email,
@ -43,35 +42,3 @@ func commonSignupTest(s TestSetup, t *testing.T) {
assert.Equal(t, email, verificationRequest.Email)
cleanData(email)
}
func TestSignUp(t *testing.T) {
s := testSetup()
defer s.Server.Close()
if s.TestInfo.ShouldExecuteForSQL {
t.Run("signup for sql dbs should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.SQL
constants.DATABASE_TYPE = enum.Sqlite.String()
db.InitDB()
commonSignupTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForArango {
t.Run("signup for arangodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.ArangoDB
constants.DATABASE_TYPE = enum.Arangodb.String()
db.InitDB()
commonSignupTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForMongo {
t.Run("signup for mongodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.MongoDB
constants.DATABASE_TYPE = enum.Mongodb.String()
db.InitDB()
commonSignupTest(s, t)
})
}
}

View File

@ -19,14 +19,8 @@ import (
// common user data to share across tests
type TestData struct {
Email string
Password string
SQL string
MongoDB string
ArangoDB string
ShouldExecuteForSQL bool
ShouldExecuteForArango bool
ShouldExecuteForMongo bool
Email string
Password string
}
type TestSetup struct {
@ -38,52 +32,6 @@ type TestSetup struct {
TestInfo TestData
}
func testSetup() TestSetup {
testData := TestData{
Email: "authorizer_tester@yopmail.com",
Password: "test",
SQL: "../../data.db",
ArangoDB: "http://root:root@localhost:8529",
MongoDB: "mongodb://localhost:27017",
ShouldExecuteForSQL: true,
ShouldExecuteForArango: true,
ShouldExecuteForMongo: true,
}
constants.ENV_PATH = "../../.env.sample"
constants.DATABASE_URL = testData.SQL
env.InitEnv()
session.InitSession()
w := httptest.NewRecorder()
c, r := gin.CreateTestContext(w)
r.Use(location.Default())
r.Use(middlewares.GinContextToContextMiddleware())
r.Use(middlewares.CORSMiddleware())
r.POST("/graphql", handlers.GraphqlHandler())
server := httptest.NewServer(r)
req, _ := http.NewRequest(
"POST",
"http://"+server.Listener.Addr().String()+"/graphql",
nil,
)
req.Header.Add("x-authorizer-admin-secret", constants.ADMIN_SECRET)
c.Request = req
ctx := context.WithValue(req.Context(), "GinContextKey", c)
return TestSetup{
GinEngine: r,
GinContext: c,
Ctx: ctx,
Server: server,
Req: req,
TestInfo: testData,
}
}
func cleanData(email string) {
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
if err == nil {
@ -115,3 +63,42 @@ func cleanData(email string) {
}
}
}
func testSetup() TestSetup {
testData := TestData{
Email: "authorizer_tester@yopmail.com",
Password: "test",
}
constants.ENV_PATH = "../../.env.sample"
env.InitEnv()
session.InitSession()
w := httptest.NewRecorder()
c, r := gin.CreateTestContext(w)
r.Use(location.Default())
r.Use(middlewares.GinContextToContextMiddleware())
r.Use(middlewares.CORSMiddleware())
r.POST("/graphql", handlers.GraphqlHandler())
server := httptest.NewServer(r)
req, _ := http.NewRequest(
"POST",
"http://"+server.Listener.Addr().String()+"/graphql",
nil,
)
req.Header.Add("x-authorizer-admin-secret", constants.ADMIN_SECRET)
c.Request = req
ctx := context.WithValue(req.Context(), "GinContextKey", c)
return TestSetup{
GinEngine: r,
GinContext: c,
Ctx: ctx,
Server: server,
Req: req,
TestInfo: testData,
}
}

View File

@ -3,7 +3,6 @@ package test
import (
"testing"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/enum"
"github.com/authorizerdev/authorizer/server/graph/model"
@ -11,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)
func commonVerifyEmailTest(s TestSetup, t *testing.T) {
func verifyEmailTest(s TestSetup, t *testing.T) {
email := "verify_email." + s.TestInfo.Email
res, err := resolvers.Signup(s.Ctx, model.SignUpInput{
Email: email,
@ -34,35 +33,3 @@ func commonVerifyEmailTest(s TestSetup, t *testing.T) {
cleanData(email)
}
func TestVerifyEmail(t *testing.T) {
s := testSetup()
defer s.Server.Close()
if s.TestInfo.ShouldExecuteForSQL {
t.Run("verify email for sql dbs should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.SQL
constants.DATABASE_TYPE = enum.Sqlite.String()
db.InitDB()
commonVerifyEmailTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForArango {
t.Run("verify email for arangodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.ArangoDB
constants.DATABASE_TYPE = enum.Arangodb.String()
db.InitDB()
commonVerifyEmailTest(s, t)
})
}
if s.TestInfo.ShouldExecuteForMongo {
t.Run("verify email for mongodb should pass", func(t *testing.T) {
constants.DATABASE_URL = s.TestInfo.MongoDB
constants.DATABASE_TYPE = enum.Mongodb.String()
db.InitDB()
commonVerifyEmailTest(s, t)
})
}
}