forked from AstarLight/verification-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms_test.go
72 lines (61 loc) · 1.63 KB
/
sms_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
redisAddr := "10.10.40.231:6379"
password := ""
db := 0
RedisInit(redisAddr, password, db)
AccessKeyId := "" // 阿里云accesskey
AccessKeySecret := "" // 阿里云accessSecret
AliyunInit(AccessKeyId, AccessKeySecret)
retCode := m.Run() // 执行测试
os.Exit(retCode) // 退出测试
}
func TestSendSmsCode(t *testing.T) {
code := "658574"
phone := "15913137599"
from := "login"
smsTTL := 60
err := SendSmsCode(phone, code, from, smsTTL)
if err != nil {
t.Errorf("expected:%v, got:%v", nil, err)
}
// 再次发送,期望是不能发送,因为验证码有60秒的Cd
err = SendSmsCode(phone, code, from, smsTTL)
if err != errCodeNotExpire {
t.Errorf("expected:%v, got:%v", errCodeNotExpire, err)
}
// 验证
err = ValidateSmsCode(phone, code, from)
if err != nil {
t.Errorf("expected:%v, got:%v", nil, err)
}
// 再次验证,此时验证码理应删除
err = ValidateSmsCode(phone, code, from)
if err != errCodeNotExist {
t.Errorf("expected:%v, got:%v", errCodeNotExist, err)
}
}
func TestSendSmsCodeWithWrongCode(t *testing.T) {
code := "658574"
phone := "15913137599"
from := "login"
smsTTL := 60
err := SendSmsCode(phone, code, from, smsTTL)
if err != nil {
t.Errorf("expected:%v, got:%v", nil, err)
}
// 用错误的code验证
err = ValidateSmsCode(phone, "789789", from)
if err != errCodeNotMatch {
t.Errorf("expected:%v, got:%v", errCodeNotMatch, err)
}
// 用错误的phone验证
err = ValidateSmsCode("15935978964", code, from)
if err != errCodeNotExist {
t.Errorf("expected:%v, got:%v", errCodeNotExist, err)
}
}