Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: add auth repo unit test

LinkinStar6f1e8b6aa929
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index b034797f..eef3585e 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -155,7 +155,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
reportHandle := report_handle_backyard.NewReportHandle(questionCommon, commentRepo, configRepo)
reportBackyardService := report_backyard.NewReportBackyardService(reportRepo, userCommon, commonRepo, answerRepo, questionRepo, commentCommonRepo, reportHandle, configRepo)
controller_backyardReportController := controller_backyard.NewReportController(reportBackyardService)
- userBackyardRepo := user.NewUserBackyardRepo(dataData)
+ userBackyardRepo := user.NewUserBackyardRepo(dataData, authRepo)
userBackyardService := user_backyard.NewUserBackyardService(userBackyardRepo)
userBackyardController := controller_backyard.NewUserBackyardController(userBackyardService)
reasonRepo := reason.NewReasonRepo(configRepo)
diff --git a/internal/repo/auth/auth.go b/internal/repo/auth/auth.go
index 61bd00c7..9f5a1813 100644
--- a/internal/repo/auth/auth.go
+++ b/internal/repo/auth/auth.go
@@ -12,7 +12,7 @@ import (
"github.com/segmentfault/pacman/errors"
)
-// authRepo activity repository
+// authRepo auth repository
type authRepo struct {
data *data.Data
}
@@ -31,46 +31,60 @@ func (ar *authRepo) GetUserCacheInfo(ctx context.Context, accessToken string) (u
return userInfo, nil
}
-// GetUserStatus get user status
-func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error) {
- userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
+// SetUserCacheInfo set user cache info
+func (ar *authRepo) SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) {
+ userInfoCache, err := json.Marshal(userInfo)
if err != nil {
- return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ return err
}
- userInfo = &entity.UserCacheInfo{}
- err = json.Unmarshal([]byte(userInfoCache), userInfo)
+ err = ar.data.Cache.SetString(ctx, constant.UserTokenCacheKey+accessToken,
+ string(userInfoCache), constant.UserTokenCacheTime)
if err != nil {
- return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- return userInfo, nil
+ return nil
}
-// RemoveUserStatus remove user status
-func (ar *authRepo) RemoveUserStatus(ctx context.Context, userID string) (err error) {
- err = ar.data.Cache.Del(ctx, constant.UserStatusChangedCacheKey+userID)
+// RemoveUserCacheInfo remove user cache info
+func (ar *authRepo) RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) {
+ err = ar.data.Cache.Del(ctx, constant.UserTokenCacheKey+accessToken)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return nil
}
-// SetUserCacheInfo set user cache info
-func (ar *authRepo) SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) {
+// SetUserStatus set user status
+func (ar *authRepo) SetUserStatus(ctx context.Context, userID string, userInfo *entity.UserCacheInfo) (err error) {
userInfoCache, err := json.Marshal(userInfo)
if err != nil {
return err
}
- err = ar.data.Cache.SetString(ctx, constant.UserTokenCacheKey+accessToken,
- string(userInfoCache), constant.UserTokenCacheTime)
+ err = ar.data.Cache.SetString(ctx, constant.UserStatusChangedCacheKey+userID,
+ string(userInfoCache), constant.UserStatusChangedCacheTime)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return nil
}
-// RemoveUserCacheInfo remove user cache info
-func (ar *authRepo) RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) {
- err = ar.data.Cache.Del(ctx, constant.UserTokenCacheKey+accessToken)
+// GetUserStatus get user status
+func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error) {
+ userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
+ if err != nil {
+ return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ userInfo = &entity.UserCacheInfo{}
+ err = json.Unmarshal([]byte(userInfoCache), userInfo)
+ if err != nil {
+ return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return userInfo, nil
+}
+
+// RemoveUserStatus remove user status
+func (ar *authRepo) RemoveUserStatus(ctx context.Context, userID string) (err error) {
+ err = ar.data.Cache.Del(ctx, constant.UserStatusChangedCacheKey+userID)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
diff --git a/internal/repo/repo_test/auth_test.go b/internal/repo/repo_test/auth_test.go
new file mode 100644
index 00000000..7682b3db
--- /dev/null
+++ b/internal/repo/repo_test/auth_test.go
@@ -0,0 +1,86 @@
+package repo_test
+
+import (
+ "context"
+ "testing"
+
+ "github.com/answerdev/answer/internal/entity"
+ "github.com/answerdev/answer/internal/repo/auth"
+ "github.com/stretchr/testify/assert"
+)
+
+var (
+ token = "token"
+ userID = "1"
+)
+
+func Test_authRepo_SetUserCacheInfo(t *testing.T) {
+ authRepo := auth.NewAuthRepo(testDataSource)
+
+ err := authRepo.SetUserCacheInfo(context.TODO(), token, &entity.UserCacheInfo{UserID: userID})
+ assert.NoError(t, err)
+
+ cacheInfo, err := authRepo.GetUserCacheInfo(context.TODO(), token)
+ assert.NoError(t, err)
+ assert.Equal(t, userID, cacheInfo.UserID)
+}
+
+func Test_authRepo_RemoveUserCacheInfo(t *testing.T) {
+ authRepo := auth.NewAuthRepo(testDataSource)
+
+ err := authRepo.SetUserCacheInfo(context.TODO(), token, &entity.UserCacheInfo{UserID: userID})
+ assert.NoError(t, err)
+
+ err = authRepo.RemoveUserCacheInfo(context.TODO(), token)
+ assert.NoError(t, err)
+
+ _, err = authRepo.GetUserCacheInfo(context.TODO(), token)
+ assert.Error(t, err)
+}
+
+func Test_authRepo_SetUserStatus(t *testing.T) {
+ authRepo := auth.NewAuthRepo(testDataSource)
+
+ err := authRepo.SetUserStatus(context.TODO(), userID, &entity.UserCacheInfo{UserID: userID})
+ assert.NoError(t, err)
+
+ cacheInfo, err := authRepo.GetUserStatus(context.TODO(), userID)
+ assert.NoError(t, err)
+ assert.Equal(t, userID, cacheInfo.UserID)
+}
+func Test_authRepo_RemoveUserStatus(t *testing.T) {
+ authRepo := auth.NewAuthRepo(testDataSource)
+
+ err := authRepo.SetUserStatus(context.TODO(), userID, &entity.UserCacheInfo{UserID: userID})
+ assert.NoError(t, err)
+
+ err = authRepo.RemoveUserStatus(context.TODO(), userID)
+ assert.NoError(t, err)
+
+ _, err = authRepo.GetUserStatus(context.TODO(), userID)
+ assert.Error(t, err)
+}
+
+func Test_authRepo_SetBackyardUserCacheInfo(t *testing.T) {
+ authRepo := auth.NewAuthRepo(testDataSource)
+
+ err := authRepo.SetBackyardUserCacheInfo(context.TODO(), token, &entity.UserCacheInfo{UserID: userID})
+ assert.NoError(t, err)
+
+ cacheInfo, err := authRepo.GetBackyardUserCacheInfo(context.TODO(), token)
+ assert.NoError(t, err)
+ assert.Equal(t, userID, cacheInfo.UserID)
+}
+
+func Test_authRepo_RemoveBackyardUserCacheInfo(t *testing.T) {
+ authRepo := auth.NewAuthRepo(testDataSource)
+
+ err := authRepo.SetBackyardUserCacheInfo(context.TODO(), token, &entity.UserCacheInfo{UserID: userID})
+ assert.NoError(t, err)
+
+ err = authRepo.RemoveBackyardUserCacheInfo(context.TODO(), token)
+ assert.NoError(t, err)
+
+ _, err = authRepo.GetBackyardUserCacheInfo(context.TODO(), token)
+ assert.Error(t, err)
+}
diff --git a/internal/repo/repo_test/comment_repo_test.go b/internal/repo/repo_test/comment_repo_test.go
index 2b94b628..c7ad1d41 100644
--- a/internal/repo/repo_test/comment_repo_test.go
+++ b/internal/repo/repo_test/comment_repo_test.go
@@ -25,8 +25,8 @@ func buildCommentEntity() *entity.Comment {
}
func Test_commentRepo_AddComment(t *testing.T) {
- uniqueIDRepo := unique.NewUniqueIDRepo(dataSource)
- commentRepo := comment.NewCommentRepo(dataSource, uniqueIDRepo)
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ commentRepo := comment.NewCommentRepo(testDataSource, uniqueIDRepo)
testCommentEntity := buildCommentEntity()
err := commentRepo.AddComment(context.TODO(), testCommentEntity)
assert.NoError(t, err)
@@ -37,8 +37,8 @@ func Test_commentRepo_AddComment(t *testing.T) {
}
func Test_commentRepo_GetCommentPage(t *testing.T) {
- uniqueIDRepo := unique.NewUniqueIDRepo(dataSource)
- commentRepo := comment.NewCommentRepo(dataSource, uniqueIDRepo)
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ commentRepo := comment.NewCommentRepo(testDataSource, uniqueIDRepo)
testCommentEntity := buildCommentEntity()
err := commentRepo.AddComment(context.TODO(), testCommentEntity)
assert.NoError(t, err)
@@ -59,9 +59,9 @@ func Test_commentRepo_GetCommentPage(t *testing.T) {
}
func Test_commentRepo_UpdateComment(t *testing.T) {
- uniqueIDRepo := unique.NewUniqueIDRepo(dataSource)
- commentRepo := comment.NewCommentRepo(dataSource, uniqueIDRepo)
- commonCommentRepo := comment.NewCommentCommonRepo(dataSource, uniqueIDRepo)
+ uniqueIDRepo := unique.NewUniqueIDRepo(testDataSource)
+ commentRepo := comment.NewCommentRepo(testDataSource, uniqueIDRepo)
+ commonCommentRepo := comment.NewCommentCommonRepo(testDataSource, uniqueIDRepo)
testCommentEntity := buildCommentEntity()
err := commentRepo.AddComment(context.TODO(), testCommentEntity)
assert.NoError(t, err)
diff --git a/internal/repo/repo_test/email_repo_test.go b/internal/repo/repo_test/email_repo_test.go
index 2a6bd630..2da70944 100644
--- a/internal/repo/repo_test/email_repo_test.go
+++ b/internal/repo/repo_test/email_repo_test.go
@@ -9,7 +9,7 @@ import (
)
func Test_emailRepo_VerifyCode(t *testing.T) {
- emailRepo := export.NewEmailRepo(dataSource)
+ emailRepo := export.NewEmailRepo(testDataSource)
code, content := "1111", "test"
err := emailRepo.SetCode(context.TODO(), code, content)
assert.NoError(t, err)
diff --git a/internal/repo/repo_test/repo_main_test.go b/internal/repo/repo_test/repo_main_test.go
index 8ccfb879..e67223ff 100644
--- a/internal/repo/repo_test/repo_main_test.go
+++ b/internal/repo/repo_test/repo_main_test.go
@@ -45,8 +45,8 @@ var (
}
// after all test down will execute tearDown function to clean-up
tearDown func()
- // dataSource used for repo testing
- dataSource *data.Data
+ // testDataSource used for repo testing
+ testDataSource *data.Data
)
func TestMain(t *testing.M) {
@@ -99,7 +99,7 @@ func initTestDataSource(dbSetting TestDBSetting) error {
if err != nil {
return err
}
- dataSource = newData
+ testDataSource = newData
tearDown = func() {
dbCleanUp()
diff --git a/internal/repo/user/user_backyard_repo.go b/internal/repo/user/user_backyard_repo.go
index 06ce7d2f..82c95c38 100644
--- a/internal/repo/user/user_backyard_repo.go
+++ b/internal/repo/user/user_backyard_repo.go
@@ -5,11 +5,11 @@ import (
"encoding/json"
"time"
- "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
+ "github.com/answerdev/answer/internal/service/auth"
"github.com/answerdev/answer/internal/service/user_backyard"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
@@ -17,13 +17,15 @@ import (
// userBackyardRepo user repository
type userBackyardRepo struct {
- data *data.Data
+ data *data.Data
+ authRepo auth.AuthRepo
}
// NewUserBackyardRepo new repository
-func NewUserBackyardRepo(data *data.Data) user_backyard.UserBackyardRepo {
+func NewUserBackyardRepo(data *data.Data, authRepo auth.AuthRepo) user_backyard.UserBackyardRepo {
return &userBackyardRepo{
- data: data,
+ data: data,
+ authRepo: authRepo,
}
}
@@ -49,8 +51,7 @@ func (ur *userBackyardRepo) UpdateUserStatus(ctx context.Context, userID string,
}
t, _ := json.Marshal(userCacheInfo)
log.Infof("user change status: %s", string(t))
- err = ur.data.Cache.SetString(ctx, constant.UserStatusChangedCacheKey+userID, string(t),
- constant.UserStatusChangedCacheTime)
+ err = ur.authRepo.SetUserStatus(ctx, userID, userCacheInfo)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
diff --git a/internal/service/auth/auth.go b/internal/service/auth/auth.go
index e4fd9182..95604a14 100644
--- a/internal/service/auth/auth.go
+++ b/internal/service/auth/auth.go
@@ -13,6 +13,7 @@ type AuthRepo interface {
GetUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error)
SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error)
+ SetUserStatus(ctx context.Context, userID string, userInfo *entity.UserCacheInfo) (err error)
GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error)
RemoveUserStatus(ctx context.Context, userID string) (err error)
GetBackyardUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error)