Class: TestSMSandEmail

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/authing_ruby/test/mini_test/TestSMSandEmail.rb

Instance Method Summary collapse

Instance Method Details

#manual_send_SMS(phone) ⇒ Object

手动发送短信



42
43
44
45
46
47
# File 'lib/authing_ruby/test/mini_test/TestSMSandEmail.rb', line 42

def manual_send_SMS(phone)
  sms_result = @authenticationClient.sendSmsCode(phone)
  return sms_result
  # {"code":200,"message":"发送成功"}
  #【Authing】验证码7326,该验证码5分钟内有效,请勿泄漏于他人。
end

#setupObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/authing_ruby/test/mini_test/TestSMSandEmail.rb', line 20

def setup
  # 新建一个用户侧的
  authenticationClient_options = {
    appHost: ENV["appHost"], # "https://rails-demo.authing.cn", 
    appId: ENV["appId"], # "60800b9151d040af9016d60b"
  }
  @authenticationClient = AuthingRuby::AuthenticationClient.new(authenticationClient_options)

  # 新建一个管理侧的
  managementClient_options = {
    host: 'https://core.authing.cn',
    userPoolId: ENV["userPoolId"],
    secret: ENV["secret"],
  }
  @managementClient = AuthingRuby::ManagementClient.new(managementClient_options)

  @helper = Test::Helper.new

  @phone = '13556136684' # 测发短信时需要改一下这里, 填你自己的手机号,这样才能收到短信
end

#test_bindPhoneObject

测试: 绑定手机号 ruby ./lib/test/mini_test/TestSMSandEmail.rb -n test_bindPhone



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/authing_ruby/test/mini_test/TestSMSandEmail.rb', line 75

def test_bindPhone
  # 第一步:用户名注册用户
  username = "test_bindPhone_#{@helper.randomString()}"
  password = "123456789"
  user = @authenticationClient.registerByUsername(username, password)

  # 第二步:登录用户
  @authenticationClient.loginByUsername(username, password)

  # 第三步
  phoneCode = nil # 先保持为 nil 运行一次, 触发 manual_send_SMS 发个短信,然后自己填一下 phoneCode 为手机短信收到的验证码
  if phoneCode == nil
    # manual_send_SMS(@phone) # 取消这行的注释
  else
    res = @authenticationClient.bindPhone(@phone, phoneCode)
    assert(res.dig('id') != nil)

    # 错误情况
    # puts res
    # {"errors":[{"message":{"code":500,"message":"该手机号已被绑定"},"locations":[{"line":2,"column":3}],"path":["bindPhone"],"extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":null}
  end

  # 清理工作:测完了删除第一步注册的用户
  user_id = user['id']
  @managementClient.users.delete(user_id)
end

#test_resetPasswordByPhoneCodeObject

通过短信验证码重置密码 ruby ./lib/test/mini_test/TestSMSandEmail.rb -n test_resetPasswordByPhoneCode



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/authing_ruby/test/mini_test/TestSMSandEmail.rb', line 51

def test_resetPasswordByPhoneCode
  # 前提条件:先确保有一个用户是自己的手机号,可以直接进 Authing 手工新建一个用户。

  phone = @phone # 手机号
  phoneCode = nil # 先保持为 nil 运行一次, 触发 manual_send_SMS 发个短信,然后自己填一下 phoneCode 为手机短信收到的验证码
  if phoneCode == nil
    # manual_send_SMS(phone) # 取消这行的注释
  else
    newPassword = '123456789'
    res = @authenticationClient.resetPasswordByPhoneCode(phone, phoneCode, newPassword)
    assert(res.dig("code") == 200, res)
  end
  # 如果成功
  # {"message":"重置密码成功!","code":200}

  # 错误可能1
  # {"errors":[{"message":{"code":2004,"message":"用户不存在"},"locations":[{"line":2,"column":5}],"path":["resetPassword"],"extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":{"resetPassword":null}}

  # 错误可能2
  # {"errors":[{"message":{"code":2001,"message":"验证码不正确!"},"locations":[{"line":2,"column":5}],"path":["resetPassword"],"extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":{"resetPassword":null}}
end