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
# 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"
  }
  @authenticationClient = AuthingRuby::AuthenticationClient.new(options)
  @helper = Test::Helper.new
end

#test_bindEmailObject

绑定邮箱



200
201
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 200

def test_bindEmail
end

#test_checkLoginStatusObject

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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 209

def test_checkLoginStatus
  # 第一步:先登录然后获取 token
  username = 'zhengcheng123'
  password = "123456789"
  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 用户池里 -> “扩展能力” -> “自定义密码加密”,选的是 “用户可使用任意非空字符串作为密码”



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 146

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



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

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 48

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



62
63
64
65
66
67
68
69
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 62

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 91

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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 23

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_registerByUsernameObject

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



38
39
40
41
42
43
44
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 38

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

解绑邮箱



204
205
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 204

def test_unbindEmail
end

#test_unbindPhoneObject

测试: 解绑手机号



192
193
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 192

def test_unbindPhone
end

#test_updatePasswordObject

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



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

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_updatePhoneObject

测试: 更新用户手机号



196
197
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 196

def test_updatePhone
end

#test_updateProfileObject

测试: 修改用户资料ruby /lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb -n test_updateProfile



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/authing_ruby/test/mini_test/TestAuthenticationClient.rb', line 123

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

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

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