作者 yangfu

单元测试

... ... @@ -3,8 +3,8 @@ package protocol
import "gitlab.fjmaimaimai.com/mmm-go/ability/models"
const (
LoginPassPord ="signInPassword"
LoginSmdcode ="signInCaptcha"
LoginTypePassPord ="signInPassword"
LoginTypeSmdcode ="signInCaptcha"
)
var Nums =[]byte("0123456789")
... ... @@ -39,6 +39,7 @@ type SmsCodeRequest struct {
}
type SmsCodeResponse struct {
Code string `json:"-"`
}
/*UpdateDevice*/
... ...
... ... @@ -56,7 +56,7 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe
return
}
switch request.GrantType {
case protocol.LoginPassPord:
case protocol.LoginTypePassPord:
if strings.Compare(user.Password,request.PassWord)==0{
goto Success
}else{
... ... @@ -64,8 +64,8 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe
return
}
break
case protocol.LoginSmdcode:
if result,err=CheckSmsCode(request);result && err==nil{
case protocol.LoginTypeSmdcode:
if result,err=CheckSmsCode(request.Phone,request.Code,protocol.SmsLoginCode);result && err==nil{
goto Success
}else{
return
... ... @@ -179,7 +179,7 @@ func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol
rsp =&protocol.CheckUuidResponse{}
return
}
//短信验证码
//短信验证码 T
func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
var(
value,key,msgContent string
... ... @@ -240,17 +240,20 @@ func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.Sms
},)
request.Content = buf.String()
err = sms.Send(request)
rsp = &protocol.SmsCodeResponse{
Code:smsInfo.Code,
}
}
return
}
//验证短信验证码
func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
//验证短信验证码 T
func CheckSmsCode(phone ,code ,sendType string)(result bool,err error){
var(
value string
smsInfo *protocol.SmsInfo
)
result =false
if value,err =redis.Hget(protocol.SmsLoginCode,request.Phone);err!=nil{
if value,err =redis.Hget(sendType,phone);err!=nil{//protocol.SmsLoginCode
err = common.NewErrorWithMsg(1009,"smscode expire")
return
}
... ... @@ -261,11 +264,11 @@ func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
err = common.NewErrorWithMsg(1011,"smscode over error times")
return
}
if smsInfo.LastTime+60*5<time.Now().Unix(){
err = common.NewErrorWithMsg(1009,"smscode expire")
if (smsInfo.LastTime+60*5)<time.Now().Unix(){
err = common.NewErrorWithMsg(1009,fmt.Sprintf("smscode expire %v < %v",(smsInfo.LastTime+60*5),time.Now().Unix()))
goto Fail
}
if smsInfo.Code == request.Code{
if smsInfo.Code == code{
result = true
return
}else{
... ... @@ -275,7 +278,7 @@ func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
Fail:
{
smsInfo.ErrorCount +=1
if err=redis.Hset(protocol.SmsLoginCode,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{
if err=redis.Hset(sendType,phone,common.AssertJson(smsInfo),-1);err!=nil{
return
}
}
... ...
package auth
import (
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/ability/tests"
"testing"
)
func init(){
tests.Init()
}
func Test_SmsCode(t *testing.T){
var (
resp *protocol.SmsCodeResponse
err error
out bool
)
input :=[]*protocol.SmsCodeRequest{
{Phone:"18860183051",SendType:"sms_login_code"},
{Phone:"18860183052",SendType:"sms_login_code"},
{Phone:"18860183052",SendType:"sms_login_code"},
{Phone:"18860183053",SendType:"sms_change_mobile"},
{Phone:"18860183053",SendType:"sms_change_mobile"},
{Phone:"18860183054",SendType:"sms_change_mobile"},
}
var s IAuthService = &AuthService{}
for i:=range input{
if resp,err =s.SmsCode(input[i]);err!=nil{
t.Fatal("send sms code error. input:",input[i],err)
}
if out,err =CheckSmsCode(input[i].Phone,resp.Code,input[i].SendType);err!=nil || !out{
t.Fatal("check sms code error.",input[i].Phone,input[i].SendType,resp.Code,err)
}
}
}
... ...
package tests
import (
"github.com/astaxie/beego"
_ "github.com/go-sql-driver/mysql"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/config"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
"os"
"path/filepath"
"runtime"
"sync"
)
var one sync.Once
func Init(){
one.Do(func(){
_, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
path,_:=os.Getwd()
filename :="app.conf"
beego.LoadAppConfig("ini", filepath.Join(path, "conf", filename))
log.InitLog(config.Logger{
Filename:"app.log",
Level:"3", //7
})
err:= redis.InitWithDb(100,beego.AppConfig.String("redis_add_port"),beego.AppConfig.String("redis_auth"),"0")
if err!=nil{
log.Fatal(err)
panic(err)
}
orm.NewBeeormEngine(config.Mysql{
DataSource:beego.AppConfig.String("data_source"),
MaxIdle: 100,
MaxOpen:100,
})
protocol.InitMessageCode()
})
}
... ...
package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "gitlab.fjmaimaimai.com/mmm-go/ability/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
package tests
//import (
// "net/http"
// "net/http/httptest"
// "testing"
// "runtime"
// "path/filepath"
// _ "gitlab.fjmaimaimai.com/mmm-go/ability/routers"
//
// "github.com/astaxie/beego"
// . "github.com/smartystreets/goconvey/convey"
//)
//
//func init() {
// _, file, _, _ := runtime.Caller(0)
// apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
// beego.TestBeegoInit(apppath)
//}
//
//
//// TestBeego is a sample to run an endpoint test
//func TestBeego(t *testing.T) {
// r, _ := http.NewRequest("GET", "/", nil)
// w := httptest.NewRecorder()
// beego.BeeApp.Handlers.ServeHTTP(w, r)
//
// beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
//
// Convey("Subject: Test Station Endpoint\n", t, func() {
// Convey("Status Code Should Be 200", func() {
// So(w.Code, ShouldEqual, 200)
// })
// Convey("The Result Should Not Be Empty", func() {
// So(w.Body.Len(), ShouldBeGreaterThan, 0)
// })
// })
//}
... ...