2021-12-23 05:01:52 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-08 12:46:26 +00:00
|
|
|
"fmt"
|
2021-12-23 05:01:52 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-01-08 12:46:26 +00:00
|
|
|
"time"
|
2021-12-23 05:01:52 +00:00
|
|
|
|
|
|
|
"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"
|
2021-12-23 05:01:52 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/handlers"
|
|
|
|
"github.com/authorizerdev/authorizer/server/middlewares"
|
2022-01-22 19:54:41 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/sessionstore"
|
2021-12-23 05:01:52 +00:00
|
|
|
"github.com/gin-contrib/location"
|
|
|
|
"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
|
2021-12-23 05:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
func testSetup() TestSetup {
|
|
|
|
testData := TestData{
|
2022-01-08 12:46:26 +00:00
|
|
|
Email: fmt.Sprintf("%d_authorizer_tester@yopmail.com", time.Now().Unix()),
|
2021-12-23 08:47:44 +00:00
|
|
|
Password: "test",
|
2021-12-23 05:01:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:22:37 +00:00
|
|
|
envstore.EnvInMemoryStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnvPath, "../../.env.sample")
|
2022-01-21 08:04:04 +00:00
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
env.InitEnv()
|
2022-01-22 19:54:41 +00:00
|
|
|
sessionstore.InitSession()
|
2021-12-23 05:01:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return TestSetup{
|
|
|
|
GinEngine: r,
|
|
|
|
GinContext: c,
|
|
|
|
Server: server,
|
|
|
|
TestInfo: testData,
|
|
|
|
}
|
|
|
|
}
|