Class: TestAuthenticationClient

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

Overview

备注: 不要用 staging 或 production 环境的用户池来测试,新建一个用户池专门做测试,因为测试期间会注册随机名字的用户

Instance Method Summary collapse

Instance Method Details

#setupObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 12

def setup
  options = {
    # appHost: ENV["appHost"], # "https://rails-demo.authing.cn", 
    # appId: ENV["appId"], # "60800b9151d040af9016d60b"
    appId: "60ab26fe5be730bfc1742c68",
    appHost: "https://hn-staging.authing.cn",
  }
  @authenticationClient = AuthingRuby::AuthenticationClient.new(options)
  @helper = Test::Helper.new
end

#test_bindEmailObject

绑定邮箱



213
214
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 213

def test_bindEmail
end

#test_checkLoginStatusObject

测试: 检测 Token 登录状态 ruby ./lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_checkLoginStatus



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 222

def test_checkLoginStatus
  # 第一步:先登录然后获取 token
  username = 'zhengcheng123'
  password = "123456789"
  @authenticationClient.registerByUsername(username, password)
  user = @authenticationClient.loginByUsername(username, password)
  token = user.dig("token")

  # 第二步:检测 Token 登录状态
  result1 = @authenticationClient.checkLoginStatus(token)
  # puts result1
  # {"code"=>200, "message"=>"已登录", "status"=>true, "exp"=>1620911894, "iat"=>1619702294, "data"=>{"id"=>"608966b08b4af522620d2e59", "userPoolId"=>"60800b8ee5b66b23128b4980", "arn"=>nil}
  assert(result1['code'] == 200, result1)

  # 第三步:登出
  @authenticationClient.logout()

  # 第四步:再次检测
  result2 = @authenticationClient.checkLoginStatus(token)
  # puts result2
  # {"code"=>2206, "message"=>"登录信息已过期", "status"=>false, "exp"=>nil, "iat"=>nil, "data"=>nil}
  assert(result2['code'] == 2206, result2)
end

#test_checkPasswordStrengthObject

测试: 检查密码强度 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_checkPasswordStrength 注意, 在 Authing 用户池里 -> “扩展能力” -> “自定义密码加密”,选的是 “用户可使用任意非空字符串作为密码”



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 167

def test_checkPasswordStrength
  password = "123"
  r = @authenticationClient.checkPasswordStrength(password)
  assert(r['valid'], r)

  # 默认情况下:用户可使用任意非空字符串作为密码,返回是
  # {"valid"=>true, "message"=>"密码验证成功"}
  
  # 如果修改为:用户须使用至少 6 位字符作为密码,返回是:
  # {"valid"=>false, "message"=>"密码长度不能少于 6 位"}

  # 如果传递一个空字符串
  password = ""
  r = @authenticationClient.checkPasswordStrength(password)
  # {"valid"=>false, "message"=>"请输入密码"}
  assert(r['valid'] == false, r)
end

#test_getObject

测试简单的 get 方法 ruby ./lib/test/mini_test/TestHttpClient.rb -n test_get



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/authing_ruby/test/mini_test/TestHttpClient.rb', line 16

def test_get
  httpClient = AuthingRuby::Common::HttpClient.new
  url = "https://postman-echo.com/get"
  # url = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
  params = {
    "a": 3,
    "b": 4,
  }
  resp = httpClient.request({
    method: 'GET',
    url: url,
    params: params,
  })
  json = JSON.parse(resp.body)
  # puts JSON.pretty_generate(json)
  assert(json.dig('args', "a") == "3")
end

#test_getCurrentUserObject

测试获取当前用户 ruby ./lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_getCurrentUser



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 96

def test_getCurrentUser
  # 注册+登录
  username = "test_getCurrentUser_#{@helper.randomString()}"
  password = "123456789"
  @authenticationClient.registerByUsername(username, password)
  @authenticationClient.loginByUsername(username, password)

  # 获取用户信息
  user = @authenticationClient.getCurrentUser()
  assert(user.dig("id"), user)
end

#test_initObject

测试初始化,初始化不应该报错



10
11
12
# File 'lib/authing_ruby/test/mini_test/TestHttpClient.rb', line 10

def test_init
  httpClient = AuthingRuby::Common::HttpClient.new
end

#test_loginByEmailObject

测试邮箱+密码登录 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_loginByEmail



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 67

def test_loginByEmail
  # 第一步:先注册
  random_string = @helper.randomString(9)
  email = "#{random_string}@qq.com"
  password = random_string
  @authenticationClient.registerByEmail(email, password)

  # 第二步:登录
  resp = @authenticationClient.loginByEmail(email, password)
  assert(resp.dig('id'), resp)
end

#test_loginByUsernameObject

测试用户名+密码登录 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_loginByUsername



81
82
83
84
85
86
87
88
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 81

def test_loginByUsername
  random_string = @helper.randomString(9)
  username = random_string
  password = random_string
  @authenticationClient.registerByUsername(username, password)
  resp = @authenticationClient.loginByUsername(username, password)
  assert(resp.dig('id'), resp)
end

#test_logoutObject

测试退出登录 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_logout



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 110

def test_logout
  # 如何测试?
  # 1. 先登录, 用身份去做一些事情 (比如修改 nickname)
  # 2. 再登出, 做同样的事情,但是这次会失败

  # 注册+登录
  username = 'zhengcheng123'
  password = "123456789"
  @authenticationClient.registerByUsername(username, password)
  @authenticationClient.loginByUsername(username, password)

  # 更新用户信息  
  res1 = @authenticationClient.updateProfile({
    nickname: '昵称修改-这次会成功'
  })

  # 退出登录
  @authenticationClient.logout()

  # 更新用户信息
  begin
    res2 = @authenticationClient.updateProfile({
      nickname: '昵称-这次会失败'
    })
    puts res2
  rescue => e
    assert(e.message == '请先登录!')
  end
end

#test_postObject

测试 post 方法 ruby ./lib/test/mini_test/TestHttpClient.rb -n test_post



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/authing_ruby/test/mini_test/TestHttpClient.rb', line 36

def test_post
  httpClient = AuthingRuby::Common::HttpClient.new
  url = "https://postman-echo.com/post"
  resp = httpClient.request({
    method: 'POST',
    url: url,
    data: {
      "x": 100,
      "y": 200,
    }
  })
  json = JSON.parse(resp.body)
  # puts JSON.pretty_generate(json)
  assert(json.dig('json', "x") == "100")
end

#test_registerByEmailObject

测试邮箱+密码注册 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_registerByEmail



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 42

def test_registerByEmail
  random_string = @helper.randomNumString()
  email = "#{random_string}@qq.com"
  password = "12345678"
  resp = @authenticationClient.registerByEmail(email, password)
  # 如果失败
  # {"errors"=>[{"message"=>{"code"=>2026, "message"=>"用户已存在,请直接登录!"}, "locations"=>[{"line"=>2, "column"=>3}], "path"=>["registerByEmail"], "extensions"=>{"code"=>"INTERNAL_SERVER_ERROR"}}], "data"=>{"registerByEmail"=>nil}}
  
  # 如果成功
  # {"id"=>"6083842709f7934053e988f6", "arn"=>"arn:cn:authing:60800b8ee5b66b23128b4980:user:6083842709f7934053e988f6", "userPoolId"=>"60800b8ee5b66b23128b4980", "status"=>"Activated", "username"=>nil, "email"=>"[email protected]", "emailVerified"=>false, "phone"=>nil, "phoneVerified"=>false, "unionid"=>nil, "openid"=>nil, "nickname"=>nil, "registerSource"=>["basic:email"], "photo"=>"default-user-avatar.png", "password"=>"ec0bad9e7bbdf8d71c8e717849954520", "oauth"=>nil, "token"=>nil, "tokenExpiredAt"=>nil, "loginsCount"=>0, "lastLogin"=>nil, "lastIP"=>nil, "signedUp"=>nil, "blocked"=>false, "isDeleted"=>false, "device"=>nil, "browser"=>nil, "company"=>nil, "name"=>nil, "givenName"=>nil, "familyName"=>nil, "middleName"=>nil, "profile"=>nil, "preferredUsername"=>nil, "website"=>nil, "gender"=>"U", "birthdate"=>nil, "zoneinfo"=>nil, "locale"=>nil, "address"=>nil, "formatted"=>nil, "streetAddress"=>nil, "locality"=>nil, "region"=>nil, "postalCode"=>nil, "city"=>nil, "province"=>nil, "country"=>nil, "createdAt"=>"2021-04-24T02:36:23+00:00", "updatedAt"=>"2021-04-24T02:36:23+00:00", "externalId"=>nil}
  assert(resp.dig('id'), resp)
end

#test_registerByPhoneCodeObject

测试: 手机号+验证码+密码 注册 需要手动测试(填验证码) ruby ./lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_registerByPhoneCode



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 26

def test_registerByPhoneCode
  phone = '13556136684'
  code = '8086' 
  password = '1234567890'
  res = @authenticationClient.registerByPhoneCode(phone, code, password)
  # puts '拿到的结果是:-----'
  # puts res
  # {:code=>2026, :message=>"用户已存在,请直接登录!", :data=>nil}
  # {:code=>2001, :message=>"验证码不正确!", :data=>nil}

  # 如果成功
  # {"id"=>"60ad0c49d6e166f9fe23ba4d", "arn"=>"arn:cn:authing:60ab26fe478f98290befefaa:user:60ad0c49d6e166f9fe23ba4d", "userPoolId"=>"60ab26fe478f98290befefaa", "status"=>"Activated", "username"=>nil, "email"=>nil, "emailVerified"=>false, "phone"=>"13556136684", "phoneVerified"=>true, "unionid"=>nil, "openid"=>nil, "nickname"=>nil, "registerSource"=>["basic:phone-code"], "photo"=>"default-user-avatar.png", "password"=>"0361a6088087f31edd172c4f6076c0e6", "oauth"=>nil, "token"=>nil, "tokenExpiredAt"=>nil, "loginsCount"=>0, "lastLogin"=>nil, "lastIP"=>nil, "signedUp"=>nil, "blocked"=>false, "isDeleted"=>false, "device"=>nil, "browser"=>nil, "company"=>nil, "name"=>nil, "givenName"=>nil, "familyName"=>nil, "middleName"=>nil, "profile"=>nil, "preferredUsername"=>nil, "website"=>nil, "gender"=>"U", "birthdate"=>nil, "zoneinfo"=>nil, "locale"=>nil, "address"=>nil, "formatted"=>nil, "streetAddress"=>nil, "locality"=>nil, "region"=>nil, "postalCode"=>nil, "city"=>nil, "province"=>nil, "country"=>nil, "createdAt"=>"2021-05-25T14:40:09+00:00", "updatedAt"=>"2021-05-25T14:40:09+00:00", "externalId"=>nil}
end

#test_registerByUsernameObject

测试用户名+密码注册 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_registerByUsername



57
58
59
60
61
62
63
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 57

def test_registerByUsername
  random_string = @helper.randomString(9)
  username = random_string
  password = random_string
  resp = @authenticationClient.registerByUsername(username, password)
  assert(resp.dig('id'), resp)
end

#test_unbindEmailObject

解绑邮箱



217
218
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 217

def test_unbindEmail
end

#test_updatePasswordObject

测试: 更新用户密码 ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_updatePassword



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 187

def test_updatePassword
  # 例子1:如果不登陆,直接改,会提示尚未登录
  # oldPassword = "123456789"
  # newPassword = "123456789-ABC"
  # res = @authenticationClient.updatePassword(newPassword, oldPassword)
  # puts res
  # {"errors"=>[{"message"=>{"code"=>2020, "message"=>"尚未登录,无访问权限"}, "locations"=>[{"line"=>2, "column"=>3}], "path"=>["updatePassword"], "extensions"=>{"code"=>"INTERNAL_SERVER_ERROR"}}], "data"=>nil}

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

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

  # 第三步:更新密码
  oldPassword = password
  newPassword = "987654321"
  result = @authenticationClient.updatePassword(newPassword, oldPassword)

  # 第四步:看返回结果对不对
  assert(result.dig('id') != nil, result)
end

#test_updateProfileObject

测试: 修改用户资料 需要手动测试(确保用户名+密码账号的确存在) ruby ./lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_updateProfile



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 143

def test_updateProfile
  # 先登录
  username = 'zhengcheng123'
  password = "123456789"
  @authenticationClient.registerByUsername(username, password)
  @authenticationClient.loginByUsername(username, password)

  # 进行第一次修改
  @authenticationClient.updateProfile({
    nickname: '第一次修改'
  })

  # 进行第二次修改
  nickname = '第二次修改'
  user = @authenticationClient.updateProfile({
    nickname: nickname
  })
  user_nickname = user.dig('nickname')
  assert(user_nickname == nickname)
end