作者 yangfu

mock 测试

... ... @@ -20,3 +20,5 @@
*.tmp
*.sum
ability
\ No newline at end of file
... ...
[dev]
#数据库相关
data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display"
data_source = "root:123456@tcp(192.168.100.102:3306)/ability_display?loc=Local"
#data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
... ...
[prod]
#数据库相关
data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display"
data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display?loc=Local"
#redis相关配置
... ...
... ... @@ -5,6 +5,7 @@ import (
"log"
"reflect"
"testing"
"time"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
... ... @@ -42,3 +43,7 @@ func Benchmark_GenMessage(b *testing.B) {
}
}
}
func TestXXX(t *testing.T){
t.Log(time.Now())
}
... ...
package repository
import "ability/models"
type IConfigRepository interface {
GetCfgClient(clintId, clientSecret string) (v *models.CfgClient, err error)
}
type ConfigRepository struct{}
func (r *ConfigRepository) GetCfgClient(clintId, clientSecret string) (v *models.CfgClient, err error) {
return models.GetCfgClient(clintId, clientSecret)
}
... ...
package repository
import "ability/models"
type ConfigMockeRepository struct{}
func (r *ConfigMockeRepository) GetCfgClient(clintId, clientSecret string) (v *models.CfgClient, err error) {
return
}
... ...
... ... @@ -5,6 +5,9 @@ import "ability/models"
type IUserRepository interface {
GetUsersByMobile(mobile string) (v *models.Users, err error)
GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error)
UpdateUserInfoById(m *models.UserInfo) (err error)
GetUserInfoByAuthCode(authCode string) (v *models.UserInfo, err error)
GetUserInfoByRefreshToken(refreshToken string) (v *models.UserInfo, err error)
}
func assertImplement() {
... ... @@ -20,3 +23,15 @@ func (r *UserRepository) GetUsersByMobile(mobile string) (v *models.Users, err e
func (r *UserRepository) GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) {
return models.GetUserInfoByMobile(mobile)
}
func (r *UserRepository) UpdateUserInfoById(m *models.UserInfo) (err error) {
return models.UpdateUserInfoById(m)
}
func (r *UserRepository) GetUserInfoByAuthCode(authCode string) (v *models.UserInfo, err error) {
return models.GetUserInfoByAuthCode(authCode)
}
func (r *UserRepository)GetUserInfoByRefreshToken(refreshToken string) (v *models.UserInfo, err error) {
return models.GetUserInfoByRefreshToken(refreshToken)
}
... ...
package repository
import "ability/models"
type UserMockRepository struct{}
var userInfo =&models.UserInfo{
Auth:"897ca746f46b11e98771000c29ad8d6d",
AccessToken:"897ca746f46b11e98771000c29ad8d7d",
RefreshToken:"897ca746f46b11e98771000c29ad8d8d",
}
func (r *UserMockRepository) GetUsersByMobile(mobile string) (v *models.Users, err error) {
v =&models.Users{
Username:"18065048389",
Password:"$2y$10$YWg7jPRVLBzc3kevokMkW.boswtCvhToqC.TappIwfqwJ.cI0efvy",
}
return
}
func (r *UserMockRepository) GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) {
v =userInfo
return
}
func (r *UserMockRepository) UpdateUserInfoById(m *models.UserInfo) (err error) {
return
}
func (r *UserMockRepository) GetUserInfoByAuthCode(authCode string) (v *models.UserInfo, err error) {
v =userInfo
return
}
func (r *UserMockRepository)GetUserInfoByRefreshToken(refreshToken string) (v *models.UserInfo, err error) {
v =userInfo
return
}
\ No newline at end of file
... ...
... ... @@ -10,9 +10,11 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
"time"
)
func init() {
time.Local = time.FixedZone("CST", 3600*8)
log.InitLog(config.Logger{
Filename: "app.log",
Level: "7",
... ...
... ... @@ -40,7 +40,7 @@ func assertImplement() {
var (
//服务
sms s_sms.ISmsService = &s_sms.YunPianSmsService{}
ConfigRepository repository.IConfigRepository = &repository.ConfigRepository{}
//仓储
UserRepository repository.IUserRepository = &repository.UserRepository{}
)
... ... @@ -87,7 +87,9 @@ Success:
if userInfo.Auth == "" {
userInfo.Auth = uid.NewV1().StringNoDash()
}
if err = models.UpdateUserInfoById(userInfo); err != nil {
//TODO:更新
userInfo.AuthExp = time.Now().Add(time.Second*protocol.TokenExpire)
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
return
}
rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth}
... ... @@ -106,19 +108,20 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
var (
userInfo *models.UserInfo
)
_, err = models.GetCfgClient(request.ClientId, request.ClientSecret)
_, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
common.NewError(101, err)
err = common.NewError(101, err)
return
}
userInfo, err = models.GetUserInfoByAuthCode(request.AuthCode)
userInfo, err = UserRepository.GetUserInfoByAuthCode(request.AuthCode)
if err != nil {
common.NewError(4139, err)
err = common.NewError(4139, err)
return
}
userInfo.AccessToken = uid.NewV1().StringNoDash()
userInfo.RefreshToken = uid.NewV1().StringNoDash()
if err = models.UpdateUserInfoById(userInfo); err != nil {
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
log.Error(err)
return
}
//valid token
... ... @@ -134,25 +137,24 @@ func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *pr
func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
var (
userInfo *models.UserInfo
newAccess *protocol.Access
)
_, err = models.GetCfgClient(request.ClientId, request.ClientSecret)
_, err = ConfigRepository.GetCfgClient(request.ClientId, request.ClientSecret)
if err != nil {
common.NewError(101, err)
return
}
userInfo, err = models.GetUserInfoByAuthCode(request.RefreshToken)
userInfo, err = UserRepository.GetUserInfoByRefreshToken(request.RefreshToken)
if err != nil {
common.NewError(4139, err)
return
}
userInfo.AccessToken = uid.NewV1().StringNoDash()
if err = models.UpdateUserInfoById(userInfo); err != nil {
if err = UserRepository.UpdateUserInfoById(userInfo); err != nil {
return
}
rsp = &protocol.RefreshTokenResponse{
AccessToken: newAccess.AccessToken,
RefreshToken: newAccess.RefreshToken,
AccessToken: userInfo.AccessToken,
RefreshToken: userInfo.RefreshToken,
ExpiresIn: protocol.TokenExpire,
}
return
... ...
package auth
import (
"ability/internal/repository"
"testing"
"ability/protocol"
... ... @@ -9,8 +10,12 @@ import (
func init() {
tests.Init()
UserRepository = &repository.UserMockRepository{}
ConfigRepository = &repository.ConfigMockeRepository{}
}
var auth =AuthService{}
func Test_SmsCode(t *testing.T) {
var (
resp *protocol.SmsCodeResponse
... ... @@ -35,3 +40,35 @@ func Test_SmsCode(t *testing.T) {
}
}
}
func Test_Login(t *testing.T){
login:=&protocol.LoginRequest{
Phone:"18065048389",
Code:"562246",
GrantType:"signInPassword",
PassWord:"$2y$10$YWg7jPRVLBzc3kevokMkW.boswtCvhToqC.TappIwfqwJ.cI0efvy",
//ClientId:"123456",
}
loginRsp,err :=auth.Login(login)
if err!=nil{
t.Fatal(err,loginRsp)
}
}
func Test_AccessToken(t *testing.T){
req :=&protocol.AccessTokenRequest{
AuthCode:"897ca746f46b11e98771000c29ad8d6d",
}
if rsp,err :=auth.AccessToken(req);err!=nil{
t.Fatal(err,rsp)
}
}
func Test_RefreshToken(t *testing.T) {
req :=&protocol.RefreshTokenRequest{
}
if rsp,err :=auth.RefreshToken(req);err!=nil{
t.Fatal(err,rsp)
}
}
... ...