35 lines
1.4 KiB
Go
35 lines
1.4 KiB
Go
// 功能描述:未配置短信及缓存不可用时不能返回虚假验证码成功;版本:1.0.0。
|
||
package common
|
||
|
||
import (
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
|
||
"git.apinb.com/heqiapp/platforms/backend/api/internal/config"
|
||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// TestVerificationUnavailable 覆盖关闭Mock及缺少缓存的拒绝路径,不连接外部服务。
|
||
func TestVerificationUnavailable(t *testing.T) {
|
||
oldEnabled, oldCache := config.Spec.Global.MockVerificationEnabled, impl.RedisService
|
||
defer func() { config.Spec.Global.MockVerificationEnabled, impl.RedisService = oldEnabled, oldCache }()
|
||
impl.RedisService = nil
|
||
for _, enabled := range []bool{false, true} {
|
||
config.Spec.Global.MockVerificationEnabled = enabled
|
||
response := httptest.NewRecorder()
|
||
ctx, _ := gin.CreateTestContext(response)
|
||
ctx.Request = httptest.NewRequest("POST", "/auth/verification-code", strings.NewReader(`{"phone":"13800000001","purpose":"set_payment_password"}`))
|
||
ctx.Request.Header.Set("Content-Type", "application/json")
|
||
SendVerificationCode("user_app")(ctx)
|
||
body := response.Body.String()
|
||
if strings.Contains(body, "request_identity") || strings.Contains(body, `"code":0`) {
|
||
t.Fatalf("不可用服务不应创建成功请求:%s", body)
|
||
}
|
||
if !enabled && !strings.Contains(body, "2410") {
|
||
t.Fatalf("关闭Mock必须说明短信尚未配置:%s", body)
|
||
}
|
||
}
|
||
}
|