diff --git a/server/graph/schema.resolvers.go b/server/graph/schema.resolvers.go index e963623..9510800 100644 --- a/server/graph/schema.resolvers.go +++ b/server/graph/schema.resolvers.go @@ -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) { diff --git a/server/resolvers/token.go b/server/resolvers/session.go similarity index 96% rename from server/resolvers/token.go rename to server/resolvers/session.go index ba38f2f..e4b1a1c 100644 --- a/server/resolvers/token.go +++ b/server/resolvers/session.go @@ -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) diff --git a/server/test/forgot_password_test.go b/server/test/forgot_password_test.go index bcecfc3..2ba02ba 100644 --- a/server/test/forgot_password_test.go +++ b/server/test/forgot_password_test.go @@ -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) - }) - } -} diff --git a/server/test/login_test.go b/server/test/login_test.go index f9c359e..0c1879b 100644 --- a/server/test/login_test.go +++ b/server/test/login_test.go @@ -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) - }) - } -} diff --git a/server/test/resend_verify_email_test.go b/server/test/resend_verify_email_test.go index 50e1b97..65acae8 100644 --- a/server/test/resend_verify_email_test.go +++ b/server/test/resend_verify_email_test.go @@ -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) - }) - } -} diff --git a/server/test/reset_password_test.go b/server/test/reset_password_test.go index 2dcb8c3..0cf574d 100644 --- a/server/test/reset_password_test.go +++ b/server/test/reset_password_test.go @@ -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) - }) - } -} diff --git a/server/test/resolvers_test.go b/server/test/resolvers_test.go new file mode 100644 index 0000000..25be8a6 --- /dev/null +++ b/server/test/resolvers_test.go @@ -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) + }) + } +} diff --git a/server/test/signup_test.go b/server/test/signup_test.go index 9d934b0..e9db88d 100644 --- a/server/test/signup_test.go +++ b/server/test/signup_test.go @@ -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) - }) - } -} diff --git a/server/test/test.go b/server/test/test.go index 598ff7b..dbd86be 100644 --- a/server/test/test.go +++ b/server/test/test.go @@ -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, + } +} diff --git a/server/test/verify_email_test.go b/server/test/verify_email_test.go index d939b02..80e3d20 100644 --- a/server/test/verify_email_test.go +++ b/server/test/verify_email_test.go @@ -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) - }) - } -}