fix: move test to __test__ folder
This commit is contained in:
27
server/__test__/cors_test.go
Normal file
27
server/__test__/cors_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCors(t *testing.T) {
|
||||
allowedOrigin := "http://localhost:8080" // The allowed origin that you want to check
|
||||
notAllowedOrigin := "http://myapp.com"
|
||||
|
||||
s := testSetup()
|
||||
defer s.Server.Close()
|
||||
client := &http.Client{}
|
||||
|
||||
req, _ := createContext(s)
|
||||
req.Header.Add("Origin", allowedOrigin)
|
||||
res, _ := client.Do(req)
|
||||
|
||||
// You should get your origin (or a * depending on your config) if the
|
||||
// passed origin is allowed.
|
||||
o := res.Header.Get("Access-Control-Allow-Origin")
|
||||
assert.NotEqual(t, o, notAllowedOrigin, "Origins should not match")
|
||||
assert.Equal(t, o, allowedOrigin, "Origins do match")
|
||||
}
|
34
server/__test__/delete_user_test.go
Normal file
34
server/__test__/delete_user_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/graph/model"
|
||||
"github.com/authorizerdev/authorizer/server/resolvers"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func deleteUserTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should delete users with admin secret only`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "delete_user." + s.TestInfo.Email
|
||||
resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err := resolvers.DeleteUser(ctx, model.DeleteUserInput{
|
||||
Email: email,
|
||||
})
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
req.Header.Add("x-authorizer-admin-secret", constants.ADMIN_SECRET)
|
||||
_, err = resolvers.DeleteUser(ctx, model.DeleteUserInput{
|
||||
Email: email,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
25
server/__test__/env_test.go
Normal file
25
server/__test__/env_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEnvs(t *testing.T) {
|
||||
constants.ENV_PATH = "../../.env.sample"
|
||||
|
||||
assert.Equal(t, constants.ADMIN_SECRET, "admin")
|
||||
assert.Equal(t, constants.ENV, "production")
|
||||
assert.False(t, constants.DISABLE_EMAIL_VERIFICATION)
|
||||
assert.False(t, constants.DISABLE_MAGIC_LINK_LOGIN)
|
||||
assert.False(t, constants.DISABLE_BASIC_AUTHENTICATION)
|
||||
assert.Equal(t, constants.JWT_TYPE, "HS256")
|
||||
assert.Equal(t, constants.JWT_SECRET, "random_string")
|
||||
assert.Equal(t, constants.JWT_ROLE_CLAIM, "role")
|
||||
assert.EqualValues(t, constants.ROLES, []string{"user"})
|
||||
assert.EqualValues(t, constants.DEFAULT_ROLES, []string{"user"})
|
||||
assert.EqualValues(t, constants.PROTECTED_ROLES, []string{"admin"})
|
||||
assert.EqualValues(t, constants.ALLOWED_ORIGINS, []string{"*"})
|
||||
}
|
35
server/__test__/forgot_password_test.go
Normal file
35
server/__test__/forgot_password_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 forgotPasswordTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should run forgot password`, func(t *testing.T) {
|
||||
_, ctx := createContext(s)
|
||||
email := "forgot_password." + s.TestInfo.Email
|
||||
_, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err = resolvers.ForgotPassword(ctx, model.ForgotPasswordInput{
|
||||
Email: email,
|
||||
})
|
||||
assert.Nil(t, err, "no errors for forgot password")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.ForgotPassword.String())
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, verificationRequest.Identifier, enum.ForgotPassword.String())
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
58
server/__test__/login_test.go
Normal file
58
server/__test__/login_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 loginTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should login`, func(t *testing.T) {
|
||||
_, ctx := createContext(s)
|
||||
email := "login." + s.TestInfo.Email
|
||||
_, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err = resolvers.Login(ctx, model.LoginInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
assert.NotNil(t, err, "should fail because email is not verified")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
|
||||
_, err = resolvers.Login(ctx, model.LoginInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
Roles: []string{"test"},
|
||||
})
|
||||
assert.NotNil(t, err, "invalid roles")
|
||||
|
||||
_, err = resolvers.Login(ctx, model.LoginInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password + "s",
|
||||
})
|
||||
assert.NotNil(t, err, "invalid password")
|
||||
|
||||
loginRes, err := resolvers.Login(ctx, model.LoginInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
assert.Nil(t, err, "login successful")
|
||||
assert.NotNil(t, loginRes.AccessToken, "access token should not be empty")
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
35
server/__test__/logout_test.go
Normal file
35
server/__test__/logout_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 logoutTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should logout user`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "logout." + s.TestInfo.Email
|
||||
|
||||
_, err := resolvers.MagicLinkLogin(ctx, model.MagicLinkLoginInput{
|
||||
Email: email,
|
||||
})
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.MagicLinkLogin.String())
|
||||
verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
|
||||
token := *verifyRes.AccessToken
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
_, err = resolvers.Logout(ctx)
|
||||
assert.Nil(t, err)
|
||||
_, err = resolvers.Profile(ctx)
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
35
server/__test__/magic_link_login_test.go
Normal file
35
server/__test__/magic_link_login_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 magicLinkLoginTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should login with magic link`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "magic_link_login." + s.TestInfo.Email
|
||||
|
||||
_, err := resolvers.MagicLinkLogin(ctx, model.MagicLinkLoginInput{
|
||||
Email: email,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.MagicLinkLogin.String())
|
||||
verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
|
||||
token := *verifyRes.AccessToken
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
_, err = resolvers.Profile(ctx)
|
||||
assert.Nil(t, err)
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
23
server/__test__/meta_test.go
Normal file
23
server/__test__/meta_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/resolvers"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func metaTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should get meta information`, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
meta, err := resolvers.Meta(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, meta.IsFacebookLoginEnabled)
|
||||
assert.False(t, meta.IsGoogleLoginEnabled)
|
||||
assert.False(t, meta.IsGithubLoginEnabled)
|
||||
assert.True(t, meta.IsEmailVerificationEnabled)
|
||||
assert.True(t, meta.IsBasicAuthenticationEnabled)
|
||||
assert.True(t, meta.IsMagicLinkLoginEnabled)
|
||||
})
|
||||
}
|
42
server/__test__/profile_test.go
Normal file
42
server/__test__/profile_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 profileTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should get profile only with token`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "profile." + s.TestInfo.Email
|
||||
|
||||
resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err := resolvers.Profile(ctx)
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
|
||||
token := *verifyRes.AccessToken
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
profileRes, err := resolvers.Profile(ctx)
|
||||
assert.Nil(t, err)
|
||||
|
||||
newEmail := *&profileRes.Email
|
||||
assert.Equal(t, email, newEmail, "emails should be equal")
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
31
server/__test__/resend_verify_email_test.go
Normal file
31
server/__test__/resend_verify_email_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 resendVerifyEmailTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should resend verification email`, func(t *testing.T) {
|
||||
_, ctx := createContext(s)
|
||||
email := "resend_verify_email." + s.TestInfo.Email
|
||||
_, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err = resolvers.ResendVerifyEmail(ctx, model.ResendVerifyEmailInput{
|
||||
Email: email,
|
||||
Identifier: enum.BasicAuthSignup.String(),
|
||||
})
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
49
server/__test__/reset_password_test.go
Normal file
49
server/__test__/reset_password_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 resetPasswordTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should reset password`, func(t *testing.T) {
|
||||
email := "reset_password." + s.TestInfo.Email
|
||||
_, ctx := createContext(s)
|
||||
_, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err = resolvers.ForgotPassword(ctx, model.ForgotPasswordInput{
|
||||
Email: email,
|
||||
})
|
||||
assert.Nil(t, err, "no errors for forgot password")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.ForgotPassword.String())
|
||||
assert.Nil(t, err, "should get forgot password request")
|
||||
|
||||
_, err = resolvers.ResetPassword(ctx, model.ResetPasswordInput{
|
||||
Token: verificationRequest.Token,
|
||||
Password: "test1",
|
||||
ConfirmPassword: "test",
|
||||
})
|
||||
|
||||
assert.NotNil(t, err, "passowrds don't match")
|
||||
|
||||
_, err = resolvers.ResetPassword(ctx, model.ResetPasswordInput{
|
||||
Token: verificationRequest.Token,
|
||||
Password: "test1",
|
||||
ConfirmPassword: "test1",
|
||||
})
|
||||
|
||||
assert.Nil(t, err, "password changed successfully")
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
47
server/__test__/resolvers_test.go
Normal file
47
server/__test__/resolvers_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"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",
|
||||
}
|
||||
|
||||
for dbType, dbURL := range databases {
|
||||
constants.DATABASE_URL = dbURL
|
||||
constants.DATABASE_TYPE = dbType
|
||||
db.InitDB()
|
||||
|
||||
s := testSetup()
|
||||
defer s.Server.Close()
|
||||
|
||||
t.Run("should pass tests for "+dbType, func(t *testing.T) {
|
||||
loginTests(s, t)
|
||||
signupTests(s, t)
|
||||
forgotPasswordTest(s, t)
|
||||
resendVerifyEmailTests(s, t)
|
||||
resetPasswordTest(s, t)
|
||||
verifyEmailTest(s, t)
|
||||
sessionTests(s, t)
|
||||
profileTests(s, t)
|
||||
updateProfileTests(s, t)
|
||||
magicLinkLoginTests(s, t)
|
||||
logoutTests(s, t)
|
||||
metaTests(s, t)
|
||||
|
||||
// admin tests
|
||||
verificationRequestsTest(s, t)
|
||||
usersTest(s, t)
|
||||
deleteUserTest(s, t)
|
||||
updateUserTest(s, t)
|
||||
})
|
||||
}
|
||||
}
|
42
server/__test__/session_test.go
Normal file
42
server/__test__/session_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 sessionTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should allow access to profile with session only`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "session." + s.TestInfo.Email
|
||||
|
||||
resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
_, err := resolvers.Session(ctx, []string{})
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
|
||||
token := *verifyRes.AccessToken
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
sessionRes, err := resolvers.Session(ctx, []string{})
|
||||
assert.Nil(t, err)
|
||||
|
||||
newToken := *sessionRes.AccessToken
|
||||
assert.Equal(t, token, newToken, "tokens should be equal")
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
47
server/__test__/signup_test.go
Normal file
47
server/__test__/signup_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 signupTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should complete the signup and check duplicates`, func(t *testing.T) {
|
||||
_, ctx := createContext(s)
|
||||
email := "signup." + s.TestInfo.Email
|
||||
res, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password + "s",
|
||||
})
|
||||
assert.NotNil(t, err, "invalid password errors")
|
||||
|
||||
res, err = resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
user := *res.User
|
||||
assert.Equal(t, email, user.Email)
|
||||
assert.Nil(t, res.AccessToken, "access token should be nil")
|
||||
|
||||
res, err = resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
assert.NotNil(t, err, "should throw duplicate email error")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, email, verificationRequest.Email)
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
93
server/__test__/test.go
Normal file
93
server/__test__/test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/db"
|
||||
"github.com/authorizerdev/authorizer/server/enum"
|
||||
"github.com/authorizerdev/authorizer/server/env"
|
||||
"github.com/authorizerdev/authorizer/server/handlers"
|
||||
"github.com/authorizerdev/authorizer/server/middlewares"
|
||||
"github.com/authorizerdev/authorizer/server/session"
|
||||
"github.com/gin-contrib/location"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// common user data to share across tests
|
||||
type TestData struct {
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
|
||||
type TestSetup struct {
|
||||
GinEngine *gin.Engine
|
||||
GinContext *gin.Context
|
||||
Server *httptest.Server
|
||||
TestInfo TestData
|
||||
}
|
||||
|
||||
func cleanData(email string) {
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
if err == nil {
|
||||
err = db.Mgr.DeleteVerificationRequest(verificationRequest)
|
||||
}
|
||||
|
||||
verificationRequest, err = db.Mgr.GetVerificationByEmail(email, enum.ForgotPassword.String())
|
||||
if err == nil {
|
||||
err = db.Mgr.DeleteVerificationRequest(verificationRequest)
|
||||
}
|
||||
|
||||
verificationRequest, err = db.Mgr.GetVerificationByEmail(email, enum.UpdateEmail.String())
|
||||
if err == nil {
|
||||
err = db.Mgr.DeleteVerificationRequest(verificationRequest)
|
||||
}
|
||||
|
||||
dbUser, err := db.Mgr.GetUserByEmail(email)
|
||||
if err == nil {
|
||||
db.Mgr.DeleteUser(dbUser)
|
||||
db.Mgr.DeleteUserSession(dbUser.ID)
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
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)
|
||||
|
||||
return TestSetup{
|
||||
GinEngine: r,
|
||||
GinContext: c,
|
||||
Server: server,
|
||||
TestInfo: testData,
|
||||
}
|
||||
}
|
53
server/__test__/update_profile_test.go
Normal file
53
server/__test__/update_profile_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 updateProfileTests(s TestSetup, t *testing.T) {
|
||||
t.Run(`should update the profile with access token only`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "update_profile." + s.TestInfo.Email
|
||||
|
||||
resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
fName := "samani"
|
||||
_, err := resolvers.UpdateProfile(ctx, model.UpdateProfileInput{
|
||||
FamilyName: &fName,
|
||||
})
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
|
||||
token := *verifyRes.AccessToken
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
_, err = resolvers.UpdateProfile(ctx, model.UpdateProfileInput{
|
||||
FamilyName: &fName,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
newEmail := "new_" + email
|
||||
_, err = resolvers.UpdateProfile(ctx, model.UpdateProfileInput{
|
||||
Email: &newEmail,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
_, err = resolvers.Profile(ctx)
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
cleanData(newEmail)
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
40
server/__test__/update_user_test.go
Normal file
40
server/__test__/update_user_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/graph/model"
|
||||
"github.com/authorizerdev/authorizer/server/resolvers"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func updateUserTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should update the user with admin secret only`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "update_user." + s.TestInfo.Email
|
||||
signupRes, _ := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
user := *signupRes.User
|
||||
adminRole := "admin"
|
||||
userRole := "user"
|
||||
newRoles := []*string{&adminRole, &userRole}
|
||||
_, err := resolvers.UpdateUser(ctx, model.UpdateUserInput{
|
||||
ID: user.ID,
|
||||
Roles: newRoles,
|
||||
})
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
req.Header.Add("x-authorizer-admin-secret", constants.ADMIN_SECRET)
|
||||
_, err = resolvers.UpdateUser(ctx, model.UpdateUserInput{
|
||||
ID: user.ID,
|
||||
Roles: newRoles,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
27
server/__test__/urls_test.go
Normal file
27
server/__test__/urls_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetHostName(t *testing.T) {
|
||||
authorizer_url := "http://test.herokuapp.com:80"
|
||||
|
||||
host, port := utils.GetHostParts(authorizer_url)
|
||||
expectedHost := "test.herokuapp.com"
|
||||
|
||||
assert.Equal(t, host, expectedHost, "hostname should be equal")
|
||||
assert.Equal(t, port, "80", "port should be 80")
|
||||
}
|
||||
|
||||
func TestGetDomainName(t *testing.T) {
|
||||
authorizer_url := "http://test.herokuapp.com"
|
||||
|
||||
got := utils.GetDomainName(authorizer_url)
|
||||
want := "herokuapp.com"
|
||||
|
||||
assert.Equal(t, got, want, "domain name should be equal")
|
||||
}
|
33
server/__test__/users_test.go
Normal file
33
server/__test__/users_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/graph/model"
|
||||
"github.com/authorizerdev/authorizer/server/resolvers"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func usersTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should get users list with admin secret only`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
email := "users." + s.TestInfo.Email
|
||||
resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
users, err := resolvers.Users(ctx)
|
||||
assert.NotNil(t, err, "unauthorized")
|
||||
|
||||
req.Header.Add("x-authorizer-admin-secret", constants.ADMIN_SECRET)
|
||||
users, err = resolvers.Users(ctx)
|
||||
assert.Nil(t, err)
|
||||
rLen := len(users)
|
||||
assert.GreaterOrEqual(t, rLen, 1)
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
43
server/__test__/validator_test.go
Normal file
43
server/__test__/validator_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/enum"
|
||||
"github.com/authorizerdev/authorizer/server/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsValidEmail(t *testing.T) {
|
||||
validEmail := "lakhan@gmail.com"
|
||||
invalidEmail1 := "lakhan"
|
||||
invalidEmail2 := "lakhan.me"
|
||||
|
||||
assert.True(t, utils.IsValidEmail(validEmail), "it should be valid email")
|
||||
assert.False(t, utils.IsValidEmail(invalidEmail1), "it should be invalid email")
|
||||
assert.False(t, utils.IsValidEmail(invalidEmail2), "it should be invalid email")
|
||||
}
|
||||
|
||||
func TestIsValidOrigin(t *testing.T) {
|
||||
// don't use portocal(http/https) for ALLOWED_ORIGINS while testing,
|
||||
// as we trim them off while running the main function
|
||||
constants.ALLOWED_ORIGINS = []string{"localhost:8080", "*.google.com", "*.google.in", "*abc.*"}
|
||||
|
||||
assert.False(t, utils.IsValidOrigin("http://myapp.com"), "it should be invalid origin")
|
||||
assert.False(t, utils.IsValidOrigin("http://appgoogle.com"), "it should be invalid origin")
|
||||
assert.True(t, utils.IsValidOrigin("http://app.google.com"), "it should be valid origin")
|
||||
assert.False(t, utils.IsValidOrigin("http://app.google.ind"), "it should be invalid origin")
|
||||
assert.True(t, utils.IsValidOrigin("http://app.google.in"), "it should be valid origin")
|
||||
assert.True(t, utils.IsValidOrigin("http://xyx.abc.com"), "it should be valid origin")
|
||||
assert.True(t, utils.IsValidOrigin("http://xyx.abc.in"), "it should be valid origin")
|
||||
assert.True(t, utils.IsValidOrigin("http://xyxabc.in"), "it should be valid origin")
|
||||
assert.True(t, utils.IsValidOrigin("http://localhost:8080"), "it should be valid origin")
|
||||
}
|
||||
|
||||
func TestIsValidIdentifier(t *testing.T) {
|
||||
assert.False(t, utils.IsValidVerificationIdentifier("test"), "it should be invalid identifier")
|
||||
assert.True(t, utils.IsValidVerificationIdentifier(enum.BasicAuthSignup.String()), "it should be valid identifier")
|
||||
assert.True(t, utils.IsValidVerificationIdentifier(enum.UpdateEmail.String()), "it should be valid identifier")
|
||||
assert.True(t, utils.IsValidVerificationIdentifier(enum.ForgotPassword.String()), "it should be valid identifier")
|
||||
}
|
35
server/__test__/verification_requests_test.go
Normal file
35
server/__test__/verification_requests_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/graph/model"
|
||||
"github.com/authorizerdev/authorizer/server/resolvers"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func verificationRequestsTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should get verification requests with admin secret only`, func(t *testing.T) {
|
||||
req, ctx := createContext(s)
|
||||
|
||||
email := "verification_requests." + s.TestInfo.Email
|
||||
resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
requests, err := resolvers.VerificationRequests(ctx)
|
||||
assert.NotNil(t, err, "unauthorizer")
|
||||
|
||||
req.Header.Add("x-authorizer-admin-secret", constants.ADMIN_SECRET)
|
||||
requests, err = resolvers.VerificationRequests(ctx)
|
||||
|
||||
assert.Nil(t, err)
|
||||
rLen := len(requests)
|
||||
assert.GreaterOrEqual(t, rLen, 1)
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
38
server/__test__/verify_email_test.go
Normal file
38
server/__test__/verify_email_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"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 verifyEmailTest(s TestSetup, t *testing.T) {
|
||||
t.Run(`should verify email`, func(t *testing.T) {
|
||||
_, ctx := createContext(s)
|
||||
email := "verify_email." + s.TestInfo.Email
|
||||
res, err := resolvers.Signup(ctx, model.SignUpInput{
|
||||
Email: email,
|
||||
Password: s.TestInfo.Password,
|
||||
ConfirmPassword: s.TestInfo.Password,
|
||||
})
|
||||
|
||||
user := *res.User
|
||||
assert.Equal(t, email, user.Email)
|
||||
assert.Nil(t, res.AccessToken, "access token should be nil")
|
||||
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, email, verificationRequest.Email)
|
||||
|
||||
verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
|
||||
Token: verificationRequest.Token,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.NotEqual(t, verifyRes.AccessToken, "", "access token should not be empty")
|
||||
|
||||
cleanData(email)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user