authorizer/server/test/test.go

102 lines
3.1 KiB
Go
Raw Normal View History

package test
import (
"context"
2022-01-08 12:46:26 +00:00
"fmt"
"net/http"
"net/http/httptest"
2022-01-08 12:46:26 +00:00
"time"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/env"
2022-01-17 06:02:13 +00:00
"github.com/authorizerdev/authorizer/server/envstore"
"github.com/authorizerdev/authorizer/server/handlers"
"github.com/authorizerdev/authorizer/server/middlewares"
"github.com/authorizerdev/authorizer/server/sessionstore"
"github.com/gin-gonic/gin"
)
// common user data to share across tests
type TestData struct {
2021-12-23 08:47:44 +00:00
Email string
Password string
}
type TestSetup struct {
GinEngine *gin.Engine
GinContext *gin.Context
Server *httptest.Server
TestInfo TestData
}
2021-12-23 08:47:44 +00:00
func cleanData(email string) {
2022-01-21 08:04:04 +00:00
verificationRequest, err := db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeBasicAuthSignup)
2021-12-23 08:47:44 +00:00
if err == nil {
2022-01-21 08:04:04 +00:00
err = db.Provider.DeleteVerificationRequest(verificationRequest)
2021-12-23 08:47:44 +00:00
}
2022-01-21 08:04:04 +00:00
verificationRequest, err = db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeForgotPassword)
2021-12-23 08:47:44 +00:00
if err == nil {
2022-01-21 08:04:04 +00:00
err = db.Provider.DeleteVerificationRequest(verificationRequest)
2021-12-23 08:47:44 +00:00
}
2022-01-21 08:04:04 +00:00
verificationRequest, err = db.Provider.GetVerificationRequestByEmail(email, constants.VerificationTypeUpdateEmail)
2021-12-23 08:47:44 +00:00
if err == nil {
2022-01-21 08:04:04 +00:00
err = db.Provider.DeleteVerificationRequest(verificationRequest)
2021-12-23 08:47:44 +00:00
}
2022-01-21 08:06:19 +00:00
dbUser, err := db.Provider.GetUserByEmail(email)
if err == nil {
db.Provider.DeleteUser(dbUser)
db.Provider.DeleteSession(dbUser.ID)
}
2021-12-23 08:47:44 +00:00
}
2021-12-24 00:57:39 +00:00
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 12:46:26 +00:00
Email: fmt.Sprintf("%d_authorizer_tester@yopmail.com", time.Now().Unix()),
Password: "Test@123",
}
2022-02-28 02:25:01 +00:00
envstore.EnvStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnvPath, "../../.env.sample")
2022-03-02 12:12:31 +00:00
env.InitRequiredEnv()
2022-02-28 02:25:01 +00:00
envstore.EnvStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpHost, "smtp.yopmail.com")
envstore.EnvStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpPort, "2525")
envstore.EnvStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpUsername, "lakhan@yopmail.com")
envstore.EnvStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySmtpPassword, "test")
envstore.EnvStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeySenderEmail, "info@yopmail.com")
envstore.EnvStoreObj.UpdateEnvVariable(constants.SliceStoreIdentifier, constants.EnvKeyProtectedRoles, []string{"admin"})
db.InitDB()
2022-02-26 04:14:55 +00:00
env.InitAllEnv()
sessionstore.InitSession()
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,
}
}