Skip to content

Commit

Permalink
✅ Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Aug 12, 2023
1 parent 1eb4f0f commit 95d1e45
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 23 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/alicebob/miniredis/v2 v2.30.5
github.com/anchore/syft v0.86.1
github.com/aquasecurity/trivy v0.44.1
github.com/aws/aws-sdk-go v1.44.320
github.com/aws/aws-sdk-go v1.44.322
github.com/bytedance/sonic v1.10.0
github.com/caarlos0/env/v9 v9.0.0
github.com/casbin/casbin/v2 v2.73.1
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/xanzy/go-gitlab v0.90.0
go.uber.org/mock v0.2.0
golang.org/x/crypto v0.12.0
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
golang.org/x/oauth2 v0.11.0
gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-metrics v0.3.10/go.mod h1:4O98XIr/9W0sxpJ8UaYkvjk10Iff7SnFrb4QAOwNTFc=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.44.320 h1:o2cno15HVUYj+IAgZHJ5No6ifAxwa2HcluzahMEPfOw=
github.com/aws/aws-sdk-go v1.44.320/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.44.322 h1:7JfwifGRGQMHd99PvfXqxBaZsjuRaOF6e3X9zRx2uYo=
github.com/aws/aws-sdk-go v1.44.322/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/becheran/wildmatch-go v1.0.0 h1:mE3dGGkTmpKtT4Z+88t8RStG40yN9T+kFEGj2PZFSzA=
github.com/becheran/wildmatch-go v1.0.0/go.mod h1:gbMvj0NtVdJ15Mg/mH9uxk2R1QCistMyU7d9KFzroX4=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -992,8 +992,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad h1:g0bG7Z4uG+OgH2QDODnjp6ggkk1bJDsINcuWmJN1iJU=
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ CREATE TABLE IF NOT EXISTS "code_repository_clone_credentials" (
"id" bigserial PRIMARY KEY,
"user_3rdparty_id" bigint NOT NULL,
"type" code_repository_clone_credentials_type NOT NULL,
"ssh_key" BLOB,
"ssh_key" bytea,
"username" varchar(256),
"password" varchar(256),
"token" varchar(256),
Expand Down
10 changes: 7 additions & 3 deletions pkg/graceful/graceful_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"time"

"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/logger"
)

func TestGetCtx(t *testing.T) {
Expand All @@ -39,6 +41,8 @@ func TestRunAtShutdown(t *testing.T) {
}

func TestShutdown(t *testing.T) {
logger.SetLevel("debug")

runAtShutdown = []item{}

orderArray := []int{}
Expand Down Expand Up @@ -74,11 +78,11 @@ func TestShutdown(t *testing.T) {
orderArray = append(orderArray, 2)
})
RunAtShutdown("test5", 5, func() {
time.Sleep(time.Second)
time.Sleep(time.Second * 2)
orderArray = append(orderArray, 5)
})
RunAtShutdown("test4", 4, func() {
time.Sleep(time.Second)
time.Sleep(time.Second * 2)
orderArray = append(orderArray, 4)
})

Expand All @@ -88,7 +92,7 @@ func TestShutdown(t *testing.T) {
assert.Equal(t, []int{1, 2}, orderArray)

/* TestShutdownPanic*/
time.Sleep(3 * time.Second)
time.Sleep(5 * time.Second)
runAtShutdown = []item{}
orderArray = []int{}
RunAtShutdown("test1", 1, func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/tokens/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestToken(t *testing.T) {

var tokenTimes int
tokenMock := tokenmock.NewMockTokenService(ctrl)
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ *models.User, _ time.Duration) (string, error) {
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ int64, _ time.Duration) (string, error) {
if tokenTimes == 0 {
tokenTimes++
return "test", nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/users/users_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestLoginMockToken(t *testing.T) {

var times int
tokenMock := tokenmock.NewMockTokenService(ctrl)
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ *models.User, _ time.Duration) (string, error) {
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ int64, _ time.Duration) (string, error) {
times++
if times == 2 {
return "test", nil
Expand Down
18 changes: 13 additions & 5 deletions pkg/handlers/users/users_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package users

import (
"errors"
"fmt"
"net/http"

mapset "github.com/deckarep/golang-set/v2"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/go-sigma/sigma/pkg/types"
"github.com/go-sigma/sigma/pkg/utils"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (h *handlers) Logout(c echo.Context) error {
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeBadRequest, err.Error())
}

var ids = mapset.NewSet[string]()
var ids = sets.New[string]()
for _, t := range req.Tokens {
if t == "" {
continue
Expand All @@ -62,7 +63,7 @@ func (h *handlers) Logout(c echo.Context) error {
log.Error().Err(err).Msg("Revoke token failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeInternalError, err.Error())
}
ids.Add(id)
ids.Insert(id)
}

jti, ok := c.Get("jti").(string)
Expand All @@ -71,9 +72,16 @@ func (h *handlers) Logout(c echo.Context) error {
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized, "Get jti failed")
}

ids.Add(jti)
ids.Insert(jti)

fmt.Println("82", ids)

for {
id, ok := ids.PopAny()
if !ok {
break
}

for _, id := range ids.ToSlice() {
err = h.tokenService.Revoke(ctx, id)
if err != nil {
log.Error().Err(err).Msg("Revoke token failed")
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/users/users_logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ func TestLogout(t *testing.T) {
var times int
tokenMock := tokenmock.NewMockTokenService(ctrl)
tokenMock.EXPECT().Revoke(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, _ string) error {
if times < 3 {
if times < 1 {
times++
return nil
} else {
return fmt.Errorf("error")
}
}).Times(4)
}).AnyTimes()
tokenMock.EXPECT().Validate(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, _ string) (string, string, error) {
return "test", "id", nil
}).Times(6)
}).AnyTimes()

miniRedis := miniredis.RunT(t)
viper.SetDefault("redis.url", "redis://"+miniRedis.Addr())
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/users/users_signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestSignupMockToken1(t *testing.T) {
defer ctrl.Finish()

tokenMock := tokenmock.NewMockTokenService(ctrl)
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ *models.User, _ time.Duration) (string, error) {
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ int64, _ time.Duration) (string, error) {
return "test", nil
}).Times(2)

Expand Down Expand Up @@ -157,7 +157,7 @@ func TestSignupMockToken2(t *testing.T) {
defer ctrl.Finish()

tokenMock := tokenmock.NewMockTokenService(ctrl)
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ *models.User, _ time.Duration) (string, error) {
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ int64, _ time.Duration) (string, error) {
return "", fmt.Errorf("test")
}).Times(1)

Expand Down Expand Up @@ -199,7 +199,7 @@ func TestSignupMockToken3(t *testing.T) {

var times int
tokenMock := tokenmock.NewMockTokenService(ctrl)
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ *models.User, _ time.Duration) (string, error) {
tokenMock.EXPECT().New(gomock.Any(), gomock.Any()).DoAndReturn(func(_ int64, _ time.Duration) (string, error) {
if times == 0 {
times++
return "test", nil
Expand Down

0 comments on commit 95d1e45

Please sign in to comment.