All checks were successful
Deploy Smart Search Backend Test / deploy (push) Successful in 1m24s
315 lines
7.4 KiB
Go
315 lines
7.4 KiB
Go
package tests
|
|
|
|
import (
|
|
"context"
|
|
|
|
authpb "git.techease.ru/Smart-search/smart-search-back/pkg/pb/auth"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_LoginWithNonExistentUser() {
|
|
req := &authpb.LoginRequest{
|
|
Email: "nonexistent@example.com",
|
|
Password: "password123",
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "test-agent",
|
|
}
|
|
|
|
resp, err := s.authClient.Login(context.Background(), req)
|
|
|
|
s.Error(err)
|
|
s.Nil(resp)
|
|
|
|
st, ok := status.FromError(err)
|
|
s.True(ok)
|
|
s.Equal(codes.NotFound, st.Code())
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_ValidateWithInvalidToken() {
|
|
req := &authpb.ValidateRequest{
|
|
AccessToken: "invalid-token",
|
|
}
|
|
|
|
resp, err := s.authClient.Validate(context.Background(), req)
|
|
|
|
s.NoError(err)
|
|
s.NotNil(resp)
|
|
s.False(resp.Valid)
|
|
s.Equal(int64(0), resp.UserId)
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_ValidateWithEmptyToken() {
|
|
req := &authpb.ValidateRequest{
|
|
AccessToken: "",
|
|
}
|
|
|
|
resp, err := s.authClient.Validate(context.Background(), req)
|
|
|
|
s.NoError(err)
|
|
s.NotNil(resp)
|
|
s.False(resp.Valid)
|
|
s.Equal(int64(0), resp.UserId)
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RefreshWithInvalidToken() {
|
|
req := &authpb.RefreshRequest{
|
|
RefreshToken: "invalid-refresh-token",
|
|
}
|
|
|
|
resp, err := s.authClient.Refresh(context.Background(), req)
|
|
|
|
s.Error(err)
|
|
s.Nil(resp)
|
|
|
|
st, ok := status.FromError(err)
|
|
s.True(ok)
|
|
s.Equal(codes.Unauthenticated, st.Code())
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_LogoutWithInvalidToken() {
|
|
req := &authpb.LogoutRequest{
|
|
AccessToken: "invalid-token",
|
|
}
|
|
|
|
resp, err := s.authClient.Logout(context.Background(), req)
|
|
|
|
s.NoError(err)
|
|
s.NotNil(resp)
|
|
s.True(resp.Success)
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RefreshTokenFlow() {
|
|
ctx := context.Background()
|
|
|
|
loginReq := &authpb.LoginRequest{
|
|
Email: "test@example.com",
|
|
Password: "testpassword",
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
loginResp, err := s.authClient.Login(ctx, loginReq)
|
|
s.NoError(err)
|
|
s.NotNil(loginResp)
|
|
s.NotEmpty(loginResp.AccessToken)
|
|
s.NotEmpty(loginResp.RefreshToken)
|
|
|
|
validateReq := &authpb.ValidateRequest{
|
|
AccessToken: loginResp.AccessToken,
|
|
}
|
|
|
|
validateResp, err := s.authClient.Validate(ctx, validateReq)
|
|
s.NoError(err)
|
|
s.NotNil(validateResp)
|
|
s.True(validateResp.Valid)
|
|
|
|
refreshReq := &authpb.RefreshRequest{
|
|
RefreshToken: loginResp.RefreshToken,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
refreshResp, err := s.authClient.Refresh(ctx, refreshReq)
|
|
s.NoError(err)
|
|
s.NotNil(refreshResp)
|
|
s.NotEmpty(refreshResp.AccessToken)
|
|
|
|
logoutReq := &authpb.LogoutRequest{
|
|
AccessToken: refreshResp.AccessToken,
|
|
}
|
|
|
|
logoutResp, err := s.authClient.Logout(ctx, logoutReq)
|
|
s.NoError(err)
|
|
s.NotNil(logoutResp)
|
|
s.True(logoutResp.Success)
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_LogoutInvalidatesSession() {
|
|
ctx := context.Background()
|
|
|
|
loginReq := &authpb.LoginRequest{
|
|
Email: "test@example.com",
|
|
Password: "testpassword",
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
loginResp, err := s.authClient.Login(ctx, loginReq)
|
|
s.NoError(err)
|
|
s.NotEmpty(loginResp.AccessToken)
|
|
|
|
logoutReq := &authpb.LogoutRequest{
|
|
AccessToken: loginResp.AccessToken,
|
|
}
|
|
|
|
logoutResp, err := s.authClient.Logout(ctx, logoutReq)
|
|
s.NoError(err)
|
|
s.True(logoutResp.Success)
|
|
|
|
validateReq := &authpb.ValidateRequest{
|
|
AccessToken: loginResp.AccessToken,
|
|
}
|
|
|
|
validateResp, err := s.authClient.Validate(ctx, validateReq)
|
|
s.NoError(err)
|
|
s.NotNil(validateResp)
|
|
s.False(validateResp.Valid)
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RegisterSuccess() {
|
|
ctx := context.Background()
|
|
|
|
inviteCode := s.createActiveInviteCode(5)
|
|
|
|
registerReq := &authpb.RegisterRequest{
|
|
Email: "newuser@example.com",
|
|
Password: "newpassword123",
|
|
Name: "New User",
|
|
Phone: "+1234567890",
|
|
InviteCode: inviteCode,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp, err := s.authClient.Register(ctx, registerReq)
|
|
s.NoError(err)
|
|
s.NotNil(registerResp)
|
|
s.NotEmpty(registerResp.AccessToken)
|
|
s.NotEmpty(registerResp.RefreshToken)
|
|
|
|
validateReq := &authpb.ValidateRequest{
|
|
AccessToken: registerResp.AccessToken,
|
|
}
|
|
|
|
validateResp, err := s.authClient.Validate(ctx, validateReq)
|
|
s.NoError(err)
|
|
s.NotNil(validateResp)
|
|
s.True(validateResp.Valid)
|
|
s.Greater(validateResp.UserId, int64(0))
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RegisterInvalidInviteCode() {
|
|
ctx := context.Background()
|
|
|
|
registerReq := &authpb.RegisterRequest{
|
|
Email: "newuser2@example.com",
|
|
Password: "newpassword123",
|
|
Name: "New User 2",
|
|
Phone: "+1234567891",
|
|
InviteCode: 999999,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp, err := s.authClient.Register(ctx, registerReq)
|
|
s.Error(err)
|
|
s.Nil(registerResp)
|
|
|
|
st, ok := status.FromError(err)
|
|
s.True(ok)
|
|
s.Equal(codes.FailedPrecondition, st.Code())
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RegisterExpiredInviteCode() {
|
|
ctx := context.Background()
|
|
|
|
inviteCode := s.createExpiredInviteCode()
|
|
|
|
registerReq := &authpb.RegisterRequest{
|
|
Email: "newuser3@example.com",
|
|
Password: "newpassword123",
|
|
Name: "New User 3",
|
|
Phone: "+1234567892",
|
|
InviteCode: inviteCode,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp, err := s.authClient.Register(ctx, registerReq)
|
|
s.Error(err)
|
|
s.Nil(registerResp)
|
|
|
|
st, ok := status.FromError(err)
|
|
s.True(ok)
|
|
s.Equal(codes.FailedPrecondition, st.Code())
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RegisterExhaustedInviteCode() {
|
|
ctx := context.Background()
|
|
|
|
inviteCode := s.createActiveInviteCode(1)
|
|
|
|
registerReq1 := &authpb.RegisterRequest{
|
|
Email: "newuser4@example.com",
|
|
Password: "newpassword123",
|
|
Name: "New User 4",
|
|
Phone: "+1234567893",
|
|
InviteCode: inviteCode,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp1, err := s.authClient.Register(ctx, registerReq1)
|
|
s.NoError(err)
|
|
s.NotNil(registerResp1)
|
|
|
|
registerReq2 := &authpb.RegisterRequest{
|
|
Email: "newuser5@example.com",
|
|
Password: "newpassword123",
|
|
Name: "New User 5",
|
|
Phone: "+1234567894",
|
|
InviteCode: inviteCode,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp2, err := s.authClient.Register(ctx, registerReq2)
|
|
s.Error(err)
|
|
s.Nil(registerResp2)
|
|
|
|
st, ok := status.FromError(err)
|
|
s.True(ok)
|
|
s.Equal(codes.FailedPrecondition, st.Code())
|
|
}
|
|
|
|
func (s *IntegrationSuite) TestAuthHandler_RegisterDuplicateEmail() {
|
|
ctx := context.Background()
|
|
|
|
inviteCode := s.createActiveInviteCode(5)
|
|
|
|
registerReq1 := &authpb.RegisterRequest{
|
|
Email: "duplicate@example.com",
|
|
Password: "newpassword123",
|
|
Name: "Duplicate User",
|
|
Phone: "+1234567895",
|
|
InviteCode: inviteCode,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp1, err := s.authClient.Register(ctx, registerReq1)
|
|
s.NoError(err)
|
|
s.NotNil(registerResp1)
|
|
|
|
inviteCode2 := s.createActiveInviteCode(5)
|
|
|
|
registerReq2 := &authpb.RegisterRequest{
|
|
Email: "duplicate@example.com",
|
|
Password: "anotherpassword",
|
|
Name: "Another User",
|
|
Phone: "+1234567896",
|
|
InviteCode: inviteCode2,
|
|
Ip: "127.0.0.1",
|
|
UserAgent: "integration-test",
|
|
}
|
|
|
|
registerResp2, err := s.authClient.Register(ctx, registerReq2)
|
|
s.Error(err)
|
|
s.Nil(registerResp2)
|
|
|
|
st, ok := status.FromError(err)
|
|
s.True(ok)
|
|
s.Equal(codes.AlreadyExists, st.Code())
|
|
}
|