Files
authorizer/server/test/test.go

106 lines
3.3 KiB
Go
Raw Normal View History

package test
import (
"context"
2022-01-08 18:16:26 +05:30
"fmt"
"net/http"
"net/http/httptest"
2022-01-08 18:16:26 +05:30
"time"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/env"
"github.com/authorizerdev/authorizer/server/handlers"
2022-05-27 23:20:38 +05:30
"github.com/authorizerdev/authorizer/server/memorystore"
"github.com/authorizerdev/authorizer/server/middlewares"
"github.com/gin-gonic/gin"
)
// common user data to share across tests
type TestData struct {
2021-12-23 14:17:44 +05:30
Email string
Password string
}
type TestSetup struct {
GinEngine *gin.Engine
GinContext *gin.Context
Server *httptest.Server
TestInfo TestData
}
2021-12-23 14:17:44 +05:30
func cleanData(email string) {
2022-05-11 20:25:57 +05:30
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeBasicAuthSignup)
if err == nil {
err = db.Provider.DeleteVerificationRequest(verificationRequest)
}
2021-12-23 14:17:44 +05:30
2022-05-11 20:25:57 +05:30
verificationRequest, err = db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeForgotPassword)
if err == nil {
err = db.Provider.DeleteVerificationRequest(verificationRequest)
}
2021-12-23 14:17:44 +05:30
2022-05-11 20:25:57 +05:30
verificationRequest, err = db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeUpdateEmail)
if err == nil {
err = db.Provider.DeleteVerificationRequest(verificationRequest)
}
2021-12-23 14:17:44 +05:30
2022-05-11 20:25:57 +05:30
verificationRequest, err = db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeMagicLinkLogin)
if err == nil {
err = db.Provider.DeleteVerificationRequest(verificationRequest)
}
2022-04-22 19:56:55 +05:30
2022-05-11 20:25:57 +05:30
dbUser, err := db.Provider.GetUserByEmail(email)
if err == nil {
db.Provider.DeleteUser(dbUser)
db.Provider.DeleteSession(dbUser.ID)
}
2021-12-23 14:17:44 +05:30
}
2021-12-24 06:27:39 +05:30
func createContext(s TestSetup) (*http.Request, context.Context) {
req, _ := http.NewRequest(
"POST",
"http://"+s.Server.Listener.Addr().String()+"/graphql",
nil,
)
ctx := context.WithValue(req.Context(), "GinContextKey", s.GinContext)
s.GinContext.Request = req
return req, ctx
}
func testSetup() TestSetup {
testData := TestData{
2022-01-08 18:16:26 +05:30
Email: fmt.Sprintf("%d_authorizer_tester@yopmail.com", time.Now().Unix()),
Password: "Test@123",
}
2022-05-30 09:19:55 +05:30
memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnvPath, "../../.env.sample")
2022-05-27 23:20:38 +05:30
memorystore.InitMemStore()
2022-05-30 09:19:55 +05:30
memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpHost, "smtp.yopmail.com")
memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpPort, "2525")
memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpUsername, "lakhan@yopmail.com")
memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpPassword, "test")
memorystore.Provider.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySenderEmail, "info@yopmail.com")
memorystore.Provider.UpdateEnvVariable(constants.SliceStoreIdentifier, constants.EnvKeyProtectedRoles, []string{"admin"})
2022-05-27 23:20:38 +05:30
memorystore.InitMemStore()
2022-02-28 07:55:01 +05:30
db.InitDB()
2022-02-26 09:44:55 +05:30
env.InitAllEnv()
w := httptest.NewRecorder()
c, r := gin.CreateTestContext(w)
r.Use(middlewares.GinContextToContextMiddleware())
r.Use(middlewares.CORSMiddleware())
r.POST("/graphql", handlers.GraphqlHandler())
server := httptest.NewServer(r)
return TestSetup{
GinEngine: r,
GinContext: c,
Server: server,
TestInfo: testData,
}
}