作者 yangfu

单元测试

@@ -3,8 +3,8 @@ package protocol @@ -3,8 +3,8 @@ package protocol
3 import "gitlab.fjmaimaimai.com/mmm-go/ability/models" 3 import "gitlab.fjmaimaimai.com/mmm-go/ability/models"
4 4
5 const ( 5 const (
6 - LoginPassPord ="signInPassword"  
7 - LoginSmdcode ="signInCaptcha" 6 + LoginTypePassPord ="signInPassword"
  7 + LoginTypeSmdcode ="signInCaptcha"
8 ) 8 )
9 9
10 var Nums =[]byte("0123456789") 10 var Nums =[]byte("0123456789")
@@ -39,6 +39,7 @@ type SmsCodeRequest struct { @@ -39,6 +39,7 @@ type SmsCodeRequest struct {
39 39
40 } 40 }
41 type SmsCodeResponse struct { 41 type SmsCodeResponse struct {
  42 + Code string `json:"-"`
42 } 43 }
43 44
44 /*UpdateDevice*/ 45 /*UpdateDevice*/
@@ -56,7 +56,7 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe @@ -56,7 +56,7 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe
56 return 56 return
57 } 57 }
58 switch request.GrantType { 58 switch request.GrantType {
59 - case protocol.LoginPassPord: 59 + case protocol.LoginTypePassPord:
60 if strings.Compare(user.Password,request.PassWord)==0{ 60 if strings.Compare(user.Password,request.PassWord)==0{
61 goto Success 61 goto Success
62 }else{ 62 }else{
@@ -64,8 +64,8 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe @@ -64,8 +64,8 @@ func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginRe
64 return 64 return
65 } 65 }
66 break 66 break
67 - case protocol.LoginSmdcode:  
68 - if result,err=CheckSmsCode(request);result && err==nil{ 67 + case protocol.LoginTypeSmdcode:
  68 + if result,err=CheckSmsCode(request.Phone,request.Code,protocol.SmsLoginCode);result && err==nil{
69 goto Success 69 goto Success
70 }else{ 70 }else{
71 return 71 return
@@ -179,7 +179,7 @@ func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol @@ -179,7 +179,7 @@ func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol
179 rsp =&protocol.CheckUuidResponse{} 179 rsp =&protocol.CheckUuidResponse{}
180 return 180 return
181 } 181 }
182 -//短信验证码 182 +//短信验证码 T
183 func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){ 183 func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){
184 var( 184 var(
185 value,key,msgContent string 185 value,key,msgContent string
@@ -240,17 +240,20 @@ func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.Sms @@ -240,17 +240,20 @@ func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.Sms
240 },) 240 },)
241 request.Content = buf.String() 241 request.Content = buf.String()
242 err = sms.Send(request) 242 err = sms.Send(request)
  243 + rsp = &protocol.SmsCodeResponse{
  244 + Code:smsInfo.Code,
  245 + }
243 } 246 }
244 return 247 return
245 } 248 }
246 -//验证短信验证码  
247 -func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){ 249 +//验证短信验证码 T
  250 +func CheckSmsCode(phone ,code ,sendType string)(result bool,err error){
248 var( 251 var(
249 value string 252 value string
250 smsInfo *protocol.SmsInfo 253 smsInfo *protocol.SmsInfo
251 ) 254 )
252 result =false 255 result =false
253 - if value,err =redis.Hget(protocol.SmsLoginCode,request.Phone);err!=nil{ 256 + if value,err =redis.Hget(sendType,phone);err!=nil{//protocol.SmsLoginCode
254 err = common.NewErrorWithMsg(1009,"smscode expire") 257 err = common.NewErrorWithMsg(1009,"smscode expire")
255 return 258 return
256 } 259 }
@@ -261,11 +264,11 @@ func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){ @@ -261,11 +264,11 @@ func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
261 err = common.NewErrorWithMsg(1011,"smscode over error times") 264 err = common.NewErrorWithMsg(1011,"smscode over error times")
262 return 265 return
263 } 266 }
264 - if smsInfo.LastTime+60*5<time.Now().Unix(){  
265 - err = common.NewErrorWithMsg(1009,"smscode expire") 267 + if (smsInfo.LastTime+60*5)<time.Now().Unix(){
  268 + err = common.NewErrorWithMsg(1009,fmt.Sprintf("smscode expire %v < %v",(smsInfo.LastTime+60*5),time.Now().Unix()))
266 goto Fail 269 goto Fail
267 } 270 }
268 - if smsInfo.Code == request.Code{ 271 + if smsInfo.Code == code{
269 result = true 272 result = true
270 return 273 return
271 }else{ 274 }else{
@@ -275,7 +278,7 @@ func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){ @@ -275,7 +278,7 @@ func CheckSmsCode(request *protocol.LoginRequest)(result bool,err error){
275 Fail: 278 Fail:
276 { 279 {
277 smsInfo.ErrorCount +=1 280 smsInfo.ErrorCount +=1
278 - if err=redis.Hset(protocol.SmsLoginCode,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{ 281 + if err=redis.Hset(sendType,phone,common.AssertJson(smsInfo),-1);err!=nil{
279 return 282 return
280 } 283 }
281 } 284 }
  1 +package auth
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  5 + "gitlab.fjmaimaimai.com/mmm-go/ability/tests"
  6 + "testing"
  7 +)
  8 +
  9 +func init(){
  10 + tests.Init()
  11 +}
  12 +
  13 +func Test_SmsCode(t *testing.T){
  14 + var (
  15 + resp *protocol.SmsCodeResponse
  16 + err error
  17 + out bool
  18 + )
  19 + input :=[]*protocol.SmsCodeRequest{
  20 + {Phone:"18860183051",SendType:"sms_login_code"},
  21 + {Phone:"18860183052",SendType:"sms_login_code"},
  22 + {Phone:"18860183052",SendType:"sms_login_code"},
  23 + {Phone:"18860183053",SendType:"sms_change_mobile"},
  24 + {Phone:"18860183053",SendType:"sms_change_mobile"},
  25 + {Phone:"18860183054",SendType:"sms_change_mobile"},
  26 + }
  27 + var s IAuthService = &AuthService{}
  28 + for i:=range input{
  29 + if resp,err =s.SmsCode(input[i]);err!=nil{
  30 + t.Fatal("send sms code error. input:",input[i],err)
  31 + }
  32 + if out,err =CheckSmsCode(input[i].Phone,resp.Code,input[i].SendType);err!=nil || !out{
  33 + t.Fatal("check sms code error.",input[i].Phone,input[i].SendType,resp.Code,err)
  34 + }
  35 + }
  36 +}
  1 +package tests
  2 +
  3 +import (
  4 + "github.com/astaxie/beego"
  5 + _ "github.com/go-sql-driver/mysql"
  6 + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  7 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/config"
  8 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  9 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm"
  10 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis"
  11 + "os"
  12 + "path/filepath"
  13 + "runtime"
  14 + "sync"
  15 +)
  16 +
  17 +var one sync.Once
  18 +
  19 +func Init(){
  20 + one.Do(func(){
  21 + _, file, _, _ := runtime.Caller(0)
  22 + apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
  23 + beego.TestBeegoInit(apppath)
  24 +
  25 + path,_:=os.Getwd()
  26 + filename :="app.conf"
  27 + beego.LoadAppConfig("ini", filepath.Join(path, "conf", filename))
  28 + log.InitLog(config.Logger{
  29 + Filename:"app.log",
  30 + Level:"3", //7
  31 + })
  32 + err:= redis.InitWithDb(100,beego.AppConfig.String("redis_add_port"),beego.AppConfig.String("redis_auth"),"0")
  33 + if err!=nil{
  34 + log.Fatal(err)
  35 + panic(err)
  36 + }
  37 + orm.NewBeeormEngine(config.Mysql{
  38 + DataSource:beego.AppConfig.String("data_source"),
  39 + MaxIdle: 100,
  40 + MaxOpen:100,
  41 + })
  42 + protocol.InitMessageCode()
  43 + })
  44 +}
1 -package test  
2 -  
3 -import (  
4 - "net/http"  
5 - "net/http/httptest"  
6 - "testing"  
7 - "runtime"  
8 - "path/filepath"  
9 - _ "gitlab.fjmaimaimai.com/mmm-go/ability/routers"  
10 -  
11 - "github.com/astaxie/beego"  
12 - . "github.com/smartystreets/goconvey/convey"  
13 -)  
14 -  
15 -func init() {  
16 - _, file, _, _ := runtime.Caller(0)  
17 - apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))  
18 - beego.TestBeegoInit(apppath)  
19 -}  
20 -  
21 -  
22 -// TestBeego is a sample to run an endpoint test  
23 -func TestBeego(t *testing.T) {  
24 - r, _ := http.NewRequest("GET", "/", nil)  
25 - w := httptest.NewRecorder()  
26 - beego.BeeApp.Handlers.ServeHTTP(w, r)  
27 -  
28 - beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())  
29 -  
30 - Convey("Subject: Test Station Endpoint\n", t, func() {  
31 - Convey("Status Code Should Be 200", func() {  
32 - So(w.Code, ShouldEqual, 200)  
33 - })  
34 - Convey("The Result Should Not Be Empty", func() {  
35 - So(w.Body.Len(), ShouldBeGreaterThan, 0)  
36 - })  
37 - })  
38 -} 1 +package tests
  2 +
  3 +//import (
  4 +// "net/http"
  5 +// "net/http/httptest"
  6 +// "testing"
  7 +// "runtime"
  8 +// "path/filepath"
  9 +// _ "gitlab.fjmaimaimai.com/mmm-go/ability/routers"
  10 +//
  11 +// "github.com/astaxie/beego"
  12 +// . "github.com/smartystreets/goconvey/convey"
  13 +//)
  14 +//
  15 +//func init() {
  16 +// _, file, _, _ := runtime.Caller(0)
  17 +// apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
  18 +// beego.TestBeegoInit(apppath)
  19 +//}
  20 +//
  21 +//
  22 +//// TestBeego is a sample to run an endpoint test
  23 +//func TestBeego(t *testing.T) {
  24 +// r, _ := http.NewRequest("GET", "/", nil)
  25 +// w := httptest.NewRecorder()
  26 +// beego.BeeApp.Handlers.ServeHTTP(w, r)
  27 +//
  28 +// beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
  29 +//
  30 +// Convey("Subject: Test Station Endpoint\n", t, func() {
  31 +// Convey("Status Code Should Be 200", func() {
  32 +// So(w.Code, ShouldEqual, 200)
  33 +// })
  34 +// Convey("The Result Should Not Be Empty", func() {
  35 +// So(w.Body.Len(), ShouldBeGreaterThan, 0)
  36 +// })
  37 +// })
  38 +//}
39 39