feat: add tests for all resolvers

This commit is contained in:
Lakhan Samani 2021-12-24 06:27:39 +05:30
parent 6e9370458b
commit 30cde3e521
21 changed files with 577 additions and 189 deletions

View File

@ -8,11 +8,7 @@
- [x] Check extra data in oauth profile and save accordingly - [x] Check extra data in oauth profile and save accordingly
- [x] Update all the resolver to make them compatible with schema changes - [x] Update all the resolver to make them compatible with schema changes
- [x] Update JWT claims - [x] Update JWT claims
- [] Write integration tests for all resolvers - [x] Write integration tests for all resolvers
- [] Update doc
- [] Rename all schemas + vars
- [] Update JS lib
- [] Update react lib
## Feature Multiple sessions ## Feature Multiple sessions

View File

@ -113,7 +113,8 @@ func UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model
} }
newEmail := strings.ToLower(*params.Email) newEmail := strings.ToLower(*params.Email)
// check if user with new email exists // check if user with new email exists
_, err = db.Mgr.GetUserByEmail(newEmail) _, err := db.Mgr.GetUserByEmail(newEmail)
// err = nil means user exists // err = nil means user exists
if err == nil { if err == nil {
return res, fmt.Errorf("user with this email address already exists") return res, fmt.Errorf("user with this email address already exists")

View File

@ -15,8 +15,9 @@ func TestCors(t *testing.T) {
defer s.Server.Close() defer s.Server.Close()
client := &http.Client{} client := &http.Client{}
s.Req.Header.Add("Origin", allowedOrigin) req, _ := createContext(s)
res, _ := client.Do(s.Req) req.Header.Add("Origin", allowedOrigin)
res, _ := client.Do(req)
// You should get your origin (or a * depending on your config) if the // You should get your origin (or a * depending on your config) if the
// passed origin is allowed. // passed origin is allowed.

View 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)
})
}

View File

@ -11,14 +11,16 @@ import (
) )
func forgotPasswordTest(s TestSetup, t *testing.T) { 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 email := "forgot_password." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{ _, err := resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
}) })
_, err = resolvers.ForgotPassword(s.Ctx, model.ForgotPasswordInput{ _, err = resolvers.ForgotPassword(ctx, model.ForgotPasswordInput{
Email: email, Email: email,
}) })
assert.Nil(t, err, "no errors for forgot password") assert.Nil(t, err, "no errors for forgot password")
@ -29,4 +31,5 @@ func forgotPasswordTest(s TestSetup, t *testing.T) {
assert.Equal(t, verificationRequest.Identifier, enum.ForgotPassword.String()) assert.Equal(t, verificationRequest.Identifier, enum.ForgotPassword.String())
cleanData(email) cleanData(email)
})
} }

View File

@ -11,14 +11,16 @@ import (
) )
func loginTests(s TestSetup, t *testing.T) { func loginTests(s TestSetup, t *testing.T) {
t.Run(`should login`, func(t *testing.T) {
_, ctx := createContext(s)
email := "login." + s.TestInfo.Email email := "login." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{ _, err := resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
}) })
_, err = resolvers.Login(s.Ctx, model.LoginInput{ _, err = resolvers.Login(ctx, model.LoginInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
}) })
@ -26,30 +28,31 @@ func loginTests(s TestSetup, t *testing.T) {
assert.NotNil(t, err, "should fail because email is not verified") assert.NotNil(t, err, "should fail because email is not verified")
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String()) verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.BasicAuthSignup.String())
resolvers.VerifyEmail(s.Ctx, model.VerifyEmailInput{ resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
Token: verificationRequest.Token, Token: verificationRequest.Token,
}) })
_, err = resolvers.Login(s.Ctx, model.LoginInput{ _, err = resolvers.Login(ctx, model.LoginInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
Roles: []string{"test"}, Roles: []string{"test"},
}) })
assert.NotNil(t, err, "invalid roles") assert.NotNil(t, err, "invalid roles")
_, err = resolvers.Login(s.Ctx, model.LoginInput{ _, err = resolvers.Login(ctx, model.LoginInput{
Email: email, Email: email,
Password: s.TestInfo.Password + "s", Password: s.TestInfo.Password + "s",
}) })
assert.NotNil(t, err, "invalid password") assert.NotNil(t, err, "invalid password")
loginRes, err := resolvers.Login(s.Ctx, model.LoginInput{ loginRes, err := resolvers.Login(ctx, model.LoginInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
}) })
assert.Nil(t, err, "login successful") assert.Nil(t, err, "login successful")
assert.Nil(t, loginRes.AccessToken, "access token should not be empty") assert.NotNil(t, loginRes.AccessToken, "access token should not be empty")
cleanData(email) cleanData(email)
})
} }

View 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.MagicLink.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)
})
}

View 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.MagicLink.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
View 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)
})
}

View 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)
})
}

View File

@ -10,14 +10,16 @@ import (
) )
func resendVerifyEmailTests(s TestSetup, t *testing.T) { 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 email := "resend_verify_email." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{ _, err := resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
}) })
_, err = resolvers.ResendVerifyEmail(s.Ctx, model.ResendVerifyEmailInput{ _, err = resolvers.ResendVerifyEmail(ctx, model.ResendVerifyEmailInput{
Email: email, Email: email,
Identifier: enum.BasicAuthSignup.String(), Identifier: enum.BasicAuthSignup.String(),
}) })
@ -25,4 +27,5 @@ func resendVerifyEmailTests(s TestSetup, t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
cleanData(email) cleanData(email)
})
} }

View File

@ -11,14 +11,16 @@ import (
) )
func resetPasswordTest(s TestSetup, t *testing.T) { func resetPasswordTest(s TestSetup, t *testing.T) {
t.Run(`should reset password`, func(t *testing.T) {
email := "reset_password." + s.TestInfo.Email email := "reset_password." + s.TestInfo.Email
_, err := resolvers.Signup(s.Ctx, model.SignUpInput{ _, ctx := createContext(s)
_, err := resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
}) })
_, err = resolvers.ForgotPassword(s.Ctx, model.ForgotPasswordInput{ _, err = resolvers.ForgotPassword(ctx, model.ForgotPasswordInput{
Email: email, Email: email,
}) })
assert.Nil(t, err, "no errors for forgot password") assert.Nil(t, err, "no errors for forgot password")
@ -26,7 +28,7 @@ func resetPasswordTest(s TestSetup, t *testing.T) {
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.ForgotPassword.String()) verificationRequest, err := db.Mgr.GetVerificationByEmail(email, enum.ForgotPassword.String())
assert.Nil(t, err, "should get forgot password request") assert.Nil(t, err, "should get forgot password request")
_, err = resolvers.ResetPassword(s.Ctx, model.ResetPasswordInput{ _, err = resolvers.ResetPassword(ctx, model.ResetPasswordInput{
Token: verificationRequest.Token, Token: verificationRequest.Token,
Password: "test1", Password: "test1",
ConfirmPassword: "test", ConfirmPassword: "test",
@ -34,7 +36,7 @@ func resetPasswordTest(s TestSetup, t *testing.T) {
assert.NotNil(t, err, "passowrds don't match") assert.NotNil(t, err, "passowrds don't match")
_, err = resolvers.ResetPassword(s.Ctx, model.ResetPasswordInput{ _, err = resolvers.ResetPassword(ctx, model.ResetPasswordInput{
Token: verificationRequest.Token, Token: verificationRequest.Token,
Password: "test1", Password: "test1",
ConfirmPassword: "test1", ConfirmPassword: "test1",
@ -43,4 +45,5 @@ func resetPasswordTest(s TestSetup, t *testing.T) {
assert.Nil(t, err, "password changed successfully") assert.Nil(t, err, "password changed successfully")
cleanData(email) cleanData(email)
})
} }

View File

@ -1,7 +1,6 @@
package test package test
import ( import (
"log"
"testing" "testing"
"github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/constants"
@ -16,21 +15,33 @@ func TestResolvers(t *testing.T) {
enum.Mongodb.String(): "mongodb://localhost:27017", enum.Mongodb.String(): "mongodb://localhost:27017",
} }
log.Println("==== Testing resolvers =====")
for dbType, dbURL := range databases { for dbType, dbURL := range databases {
constants.DATABASE_URL = dbURL constants.DATABASE_URL = dbURL
constants.DATABASE_TYPE = dbType constants.DATABASE_TYPE = dbType
db.InitDB() db.InitDB()
s := testSetup() s := testSetup()
defer s.Server.Close() defer s.Server.Close()
t.Run("running test cases for "+dbType, func(t *testing.T) {
t.Run("should pass tests for "+dbType, func(t *testing.T) {
loginTests(s, t) loginTests(s, t)
signupTests(s, t) signupTests(s, t)
forgotPasswordTest(s, t) forgotPasswordTest(s, t)
resendVerifyEmailTests(s, t) resendVerifyEmailTests(s, t)
resetPasswordTest(s, t) resetPasswordTest(s, t)
verifyEmailTest(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)
}) })
} }
} }

View 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)
})
}

View File

@ -11,15 +11,17 @@ import (
) )
func signupTests(s TestSetup, t *testing.T) { 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 email := "signup." + s.TestInfo.Email
res, err := resolvers.Signup(s.Ctx, model.SignUpInput{ res, err := resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password + "s", ConfirmPassword: s.TestInfo.Password + "s",
}) })
assert.NotNil(t, err, "invalid password errors") assert.NotNil(t, err, "invalid password errors")
res, err = resolvers.Signup(s.Ctx, model.SignUpInput{ res, err = resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
@ -29,7 +31,7 @@ func signupTests(s TestSetup, t *testing.T) {
assert.Equal(t, email, user.Email) assert.Equal(t, email, user.Email)
assert.Nil(t, res.AccessToken, "access token should be nil") assert.Nil(t, res.AccessToken, "access token should be nil")
res, err = resolvers.Signup(s.Ctx, model.SignUpInput{ res, err = resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
@ -41,4 +43,5 @@ func signupTests(s TestSetup, t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, email, verificationRequest.Email) assert.Equal(t, email, verificationRequest.Email)
cleanData(email) cleanData(email)
})
} }

View File

@ -2,7 +2,6 @@ package test
import ( import (
"context" "context"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -26,9 +25,7 @@ type TestData struct {
type TestSetup struct { type TestSetup struct {
GinEngine *gin.Engine GinEngine *gin.Engine
GinContext *gin.Context GinContext *gin.Context
Ctx context.Context
Server *httptest.Server Server *httptest.Server
Req *http.Request
TestInfo TestData TestInfo TestData
} }
@ -49,19 +46,22 @@ func cleanData(email string) {
} }
dbUser, err := db.Mgr.GetUserByEmail(email) dbUser, err := db.Mgr.GetUserByEmail(email)
if err != nil { if err == nil {
log.Println("error getting user:", err) db.Mgr.DeleteUser(dbUser)
} else { db.Mgr.DeleteUserSession(dbUser.ID)
err = db.Mgr.DeleteUser(dbUser) }
if err != nil {
log.Println("error deleting user:", err)
} }
err = db.Mgr.DeleteUserSession(dbUser.ID) func createContext(s TestSetup) (*http.Request, context.Context) {
if err != nil { req, _ := http.NewRequest(
log.Println("error deleting user session:", err) "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 { func testSetup() TestSetup {
@ -84,21 +84,10 @@ func testSetup() TestSetup {
server := httptest.NewServer(r) 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{ return TestSetup{
GinEngine: r, GinEngine: r,
GinContext: c, GinContext: c,
Ctx: ctx,
Server: server, Server: server,
Req: req,
TestInfo: testData, TestInfo: testData,
} }
} }

View 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)
})
}

View 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)
})
}

33
server/test/users_test.go Normal file
View 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)
})
}

View 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)
})
}

View File

@ -11,8 +11,10 @@ import (
) )
func verifyEmailTest(s TestSetup, t *testing.T) { 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 email := "verify_email." + s.TestInfo.Email
res, err := resolvers.Signup(s.Ctx, model.SignUpInput{ res, err := resolvers.Signup(ctx, model.SignUpInput{
Email: email, Email: email,
Password: s.TestInfo.Password, Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password, ConfirmPassword: s.TestInfo.Password,
@ -25,11 +27,12 @@ func verifyEmailTest(s TestSetup, t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, email, verificationRequest.Email) assert.Equal(t, email, verificationRequest.Email)
verifyRes, err := resolvers.VerifyEmail(s.Ctx, model.VerifyEmailInput{ verifyRes, err := resolvers.VerifyEmail(ctx, model.VerifyEmailInput{
Token: verificationRequest.Token, Token: verificationRequest.Token,
}) })
assert.Nil(t, err) assert.Nil(t, err)
assert.NotEqual(t, verifyRes.AccessToken, "", "access token should not be empty") assert.NotEqual(t, verifyRes.AccessToken, "", "access token should not be empty")
cleanData(email) cleanData(email)
})
} }