authorizer/server/test/login_test.go

60 lines
1.6 KiB
Go
Raw Normal View History

package test
import (
"testing"
2022-01-17 06:02:13 +00:00
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/db"
"github.com/authorizerdev/authorizer/server/graph/model"
"github.com/authorizerdev/authorizer/server/resolvers"
"github.com/stretchr/testify/assert"
)
2022-01-17 06:02:13 +00:00
func loginTests(t *testing.T, s TestSetup) {
t.Helper()
2021-12-24 00:57:39 +00:00
t.Run(`should login`, func(t *testing.T) {
_, ctx := createContext(s)
email := "login." + s.TestInfo.Email
2022-01-17 06:02:13 +00:00
_, err := resolvers.SignupResolver(ctx, model.SignUpInput{
2021-12-24 00:57:39 +00:00
Email: email,
Password: s.TestInfo.Password,
ConfirmPassword: s.TestInfo.Password,
})
2022-01-17 06:02:13 +00:00
_, err = resolvers.LoginResolver(ctx, model.LoginInput{
2021-12-24 00:57:39 +00:00
Email: email,
Password: s.TestInfo.Password,
})
assert.NotNil(t, err, "should fail because email is not verified")
2022-01-17 06:02:13 +00:00
verificationRequest, err := db.Mgr.GetVerificationByEmail(email, constants.VerificationTypeBasicAuthSignup)
resolvers.VerifyEmailResolver(ctx, model.VerifyEmailInput{
2021-12-24 00:57:39 +00:00
Token: verificationRequest.Token,
})
2022-01-17 06:02:13 +00:00
_, err = resolvers.LoginResolver(ctx, model.LoginInput{
2021-12-24 00:57:39 +00:00
Email: email,
Password: s.TestInfo.Password,
Roles: []string{"test"},
})
assert.NotNil(t, err, "invalid roles")
2022-01-17 06:02:13 +00:00
_, err = resolvers.LoginResolver(ctx, model.LoginInput{
2021-12-24 00:57:39 +00:00
Email: email,
Password: s.TestInfo.Password + "s",
})
assert.NotNil(t, err, "invalid password")
2022-01-17 06:02:13 +00:00
loginRes, err := resolvers.LoginResolver(ctx, model.LoginInput{
2021-12-24 00:57:39 +00:00
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)
})
}