Files
smart-search-back/tests/edge_cases_test.go
vallyenfail 9b4b8bd012
Some checks failed
Deploy Smart Search Backend / deploy (push) Failing after 13m51s
add service
2026-01-20 22:30:05 +03:00

423 lines
9.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tests
import (
"context"
authpb "git.techease.ru/Smart-search/smart-search-back/pkg/pb/auth"
invitepb "git.techease.ru/Smart-search/smart-search-back/pkg/pb/invite"
requestpb "git.techease.ru/Smart-search/smart-search-back/pkg/pb/request"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *IntegrationSuite) TestEdgeCase_CreateTZWithEmptyRequestText() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
req := &requestpb.CreateTZRequest{
UserId: validateResp.UserId,
RequestTxt: "",
}
resp, err := s.requestClient.CreateTZ(ctx, req)
if err != nil {
st, ok := status.FromError(err)
s.True(ok)
s.Contains([]codes.Code{codes.InvalidArgument, codes.Internal}, st.Code())
return
}
s.NotNil(resp)
}
func (s *IntegrationSuite) TestEdgeCase_GenerateInviteWithZeroMaxUses() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
req := &invitepb.GenerateRequest{
UserId: validateResp.UserId,
TtlDays: 30,
MaxUses: 0,
}
resp, err := s.inviteClient.Generate(ctx, req)
if err != nil {
st, ok := status.FromError(err)
s.True(ok)
s.Contains([]codes.Code{codes.InvalidArgument, codes.Internal}, st.Code())
return
}
s.NotNil(resp)
}
func (s *IntegrationSuite) TestEdgeCase_GenerateInviteWithZeroTTL() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
req := &invitepb.GenerateRequest{
UserId: validateResp.UserId,
TtlDays: 0,
MaxUses: 10,
}
resp, err := s.inviteClient.Generate(ctx, req)
if err != nil {
st, ok := status.FromError(err)
s.True(ok)
s.Contains([]codes.Code{codes.InvalidArgument, codes.Internal}, st.Code())
return
}
s.NotNil(resp)
}
func (s *IntegrationSuite) TestEdgeCase_ApproveTZWithEmptyFinalTZ() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
createReq := &requestpb.CreateTZRequest{
UserId: validateResp.UserId,
RequestTxt: "Test request",
}
createResp, err := s.requestClient.CreateTZ(ctx, createReq)
if err != nil {
s.T().Skip("Cannot test ApproveTZ without CreateTZ")
return
}
approveReq := &requestpb.ApproveTZRequest{
RequestId: createResp.RequestId,
FinalTz: "",
UserId: validateResp.UserId,
}
approveResp, err := s.requestClient.ApproveTZ(ctx, approveReq)
if err != nil {
st, ok := status.FromError(err)
s.True(ok)
s.Contains([]codes.Code{codes.InvalidArgument, codes.Internal}, st.Code())
return
}
s.NotNil(approveResp)
}
func (s *IntegrationSuite) TestEdgeCase_DoubleLogout() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
logoutReq := &authpb.LogoutRequest{
AccessToken: loginResp.AccessToken,
}
logoutResp1, err := s.authClient.Logout(ctx, logoutReq)
s.NoError(err)
s.True(logoutResp1.Success)
logoutResp2, err := s.authClient.Logout(ctx, logoutReq)
s.NoError(err)
s.True(logoutResp2.Success)
}
func (s *IntegrationSuite) TestEdgeCase_ValidateAfterLogout() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
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.False(validateResp.Valid)
s.Equal(int64(0), validateResp.UserId)
}
func (s *IntegrationSuite) TestEdgeCase_RefreshAfterLogout() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
logoutReq := &authpb.LogoutRequest{
AccessToken: loginResp.AccessToken,
}
logoutResp, err := s.authClient.Logout(ctx, logoutReq)
s.NoError(err)
s.True(logoutResp.Success)
refreshReq := &authpb.RefreshRequest{
RefreshToken: loginResp.RefreshToken,
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
refreshResp, err := s.authClient.Refresh(ctx, refreshReq)
s.Error(err)
s.Nil(refreshResp)
st, ok := status.FromError(err)
s.True(ok)
s.Equal(codes.Unauthenticated, st.Code())
}
func (s *IntegrationSuite) TestEdgeCase_LoginWithWrongPassword() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "WrongPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.Error(err)
s.Nil(loginResp)
st, ok := status.FromError(err)
s.True(ok)
s.Equal(codes.Unauthenticated, st.Code())
}
func (s *IntegrationSuite) TestEdgeCase_ApproveTZTwice() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
userID := validateResp.UserId
createReq := &requestpb.CreateTZRequest{
UserId: userID,
RequestTxt: "Тест двойного approve",
}
createResp, err := s.requestClient.CreateTZ(ctx, createReq)
s.NoError(err)
requestID := createResp.RequestId
approveReq1 := &requestpb.ApproveTZRequest{
RequestId: requestID,
FinalTz: "Первое утверждение",
UserId: userID,
}
approveResp1, err := s.requestClient.ApproveTZ(ctx, approveReq1)
s.NoError(err)
s.NotEmpty(approveResp1.RequestId)
approveReq2 := &requestpb.ApproveTZRequest{
RequestId: requestID,
FinalTz: "Второе утверждение",
UserId: userID,
}
approveResp2, err := s.requestClient.ApproveTZ(ctx, approveReq2)
s.NoError(err)
s.NotEmpty(approveResp2.RequestId)
}
func (s *IntegrationSuite) TestEdgeCase_CreateTZWithVeryLongText() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
_, err = s.pool.Exec(ctx, "UPDATE users SET balance = 10000 WHERE id = $1", validateResp.UserId)
s.NoError(err)
longText := "Нужны поставщики. "
for i := 0; i < 300; i++ {
longText += "Дополнительные требования к качеству и срокам поставки материалов. "
}
req := &requestpb.CreateTZRequest{
UserId: validateResp.UserId,
RequestTxt: longText,
}
resp, err := s.requestClient.CreateTZ(ctx, req)
s.NoError(err)
s.NotNil(resp)
s.NotEmpty(resp.RequestId)
s.NotEmpty(resp.TzText)
}
func (s *IntegrationSuite) TestEdgeCase_ApproveTZWithVeryLongFinalTZ() {
ctx := context.Background()
loginReq := &authpb.LoginRequest{
Email: "test@example.com",
Password: "TestPassword123",
Ip: "127.0.0.1",
UserAgent: "integration-test",
}
loginResp, err := s.authClient.Login(ctx, loginReq)
s.NoError(err)
validateReq := &authpb.ValidateRequest{
AccessToken: loginResp.AccessToken,
}
validateResp, err := s.authClient.Validate(ctx, validateReq)
s.NoError(err)
userID := validateResp.UserId
createReq := &requestpb.CreateTZRequest{
UserId: userID,
RequestTxt: "Тест длинного ТЗ",
}
createResp, err := s.requestClient.CreateTZ(ctx, createReq)
s.NoError(err)
requestID := createResp.RequestId
longFinalTZ := "ТЕХНИЧЕСКОЕ ЗАДАНИЕ\n\n"
for i := 0; i < 500; i++ {
longFinalTZ += "Пункт требований с детальным описанием спецификации и условий поставки. "
}
approveReq := &requestpb.ApproveTZRequest{
RequestId: requestID,
FinalTz: longFinalTZ,
UserId: userID,
}
approveResp, err := s.requestClient.ApproveTZ(ctx, approveReq)
s.NoError(err)
s.NotEmpty(approveResp.RequestId)
}