Class: TestManagementClient

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

Instance Method Summary collapse

Instance Method Details

#create_random_user(userInfo = {}) ⇒ Object

创建一个用户, 并返回这个用户



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 58

def create_random_user(userInfo = {})
  # 如果没传参数就用一个默认的,随机生成的。
  if userInfo.empty?
    random_phone = "1760000#{rand(1000..9999)}"
    userInfo = {
      nickname: "Nick #{random_phone}",
      phone: random_phone, # 由于是管理员操作,所以不需要检验手机号验证码, 如果你需要检验,请使用  AuthenticationClient
    }
  end

  managementClient = AuthingRuby::ManagementClient.new(@options)
  user = managementClient.users.create(userInfo)
  return user
end

#setupObject



10
11
12
13
14
15
16
17
18
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 10

def setup
  @options = {
    host: 'https://core.authing.cn',
    # host: 'https://core.authing.co', # 2021-5-13 他们临时使用 .co,过几天等他们恢复了 .cn 这里就改回 .cn
    userPoolId: ENV["userPoolId"],
    secret: ENV["secret"],
  }
  @helper = Test::Helper.new
end

#test_deleteObject

测试删除用户ruby ./lib/authing_ruby/test/mini_test/TestManagementClient.rb -n test_delete



108
109
110
111
112
113
114
115
116
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 108

def test_delete
  user = create_random_user()
  user_id = user.dig("id")

  managementClient = AuthingRuby::ManagementClient.new(@options)
  hash = managementClient.users.delete(user_id)
  # {"message":"删除成功!","code":200}
  assert(hash.dig("code") == 200, hash)
end

#test_deleteManyObject

测试删除多个用户ruby ./lib/test/mini_test/TestManagementClient.rb -n test_deleteMany



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 120

def test_deleteMany
  user1 = create_random_user()
  user1_id = user1.dig("id")

  user2 = create_random_user()
  user2_id = user2.dig("id")

  user_ids = [user1_id, user2_id]

  managementClient = AuthingRuby::ManagementClient.new(@options)
  hash = managementClient.users.deleteMany(user_ids)
  # {"message":"删除成功!","code":200}
  assert(hash.dig("code") == 200, hash)
end

#test_detailObject

测试通过 ID 获取用户信息ruby ./lib/authing_ruby/test/mini_test/TestManagementClient.rb -n test_detail



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

def test_detail
  user = create_random_user()
  user_id = user.dig("id")

  managementClient = AuthingRuby::ManagementClient.new(@options)
  hash = managementClient.users.detail(user_id)
  assert(hash.dig("id") != nil, hash)
end

#test_existsObject

测试 检查用户是否存在 (返回 true | false) ruby ./lib/test/mini_test/TestManagementClient.rb -n test_exists



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

def test_exists
  # 第一步:先创建用户
  userInfo = {
    "username": "bob",
  }
  create_random_user(userInfo)

  # 第二步:检查是否存在
  managementClient = AuthingRuby::ManagementClient.new(@options)
  options = { 
    "username": "bob",
    # "email": "[email protected]",
    # "phone": "13700001111",
  }
  boolean = managementClient.users.exists(options)
  assert(boolean)
end

#test_findObject

测试 查找用户 (返回这个用户) ruby ./lib/authing_ruby/test/mini_test/TestManagementClient.rb -n test_find



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 165

def test_find
  # 第一步:先创建用户
  userInfo = {
    "username": "alice",
  }
  create_random_user(userInfo)

  managementClient = AuthingRuby::ManagementClient.new(@options)
  options = { 
    "username": "alice",
    # "email": "[email protected]",
    # "phone": "13700001111",
  }
  hash = managementClient.users.find(options)
  assert(hash.dig("id"), hash)
end

#test_listObject

测试获取用户列表ruby ./lib/authing_ruby/test/mini_test/TestManagementClient.rb -n test_list



137
138
139
140
141
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 137

def test_list
  managementClient = AuthingRuby::ManagementClient.new(@options)
  hash = managementClient.users.list()
  assert(hash.dig("totalCount") != nil, hash)
end

#test_update_userObject

更新用户ruby ./lib/authing_ruby/test/mini_test/TestManagementClient.rb -n test_update_user



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 75

def test_update_user
  # 第一步:创建用户
  user = create_random_user()
  user_id = user.dig("id") # "6088decdcc904f5c993d6226"

  # 第二步:更新用户
  managementClient = AuthingRuby::ManagementClient.new(@options)
  hash = managementClient.users.update(user_id, {
    nickname: 'Nick',
  })
  # puts hash
  # 成功
  # {"id"=>"60b902e844428416be3a25b8", "arn"=>"arn:cn:authing:60800b8ee5b66b23128b4980:user:60b902e844428416be3a25b8", "userPoolId"=>"60800b8ee5b66b23128b4980", "status"=>"Activated", "username"=>nil, "email"=>nil, "emailVerified"=>false, "phone"=>"17600009338", "phoneVerified"=>false, "unionid"=>nil, "openid"=>nil, "nickname"=>"Nick", "registerSource"=>["import:manual"], "photo"=>"https://files.authing.co/authing-console/default-user-avatar.png", "password"=>nil, "oauth"=>nil, "token"=>nil, "tokenExpiredAt"=>nil, "loginsCount"=>0, "lastLogin"=>nil, "lastIP"=>nil, "signedUp"=>"2021-06-03T16:27:20+00:00", "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-06-03T16:27:20+00:00", "updatedAt"=>"2021-06-03T16:27:22+00:00", "externalId"=>nil}

  # 如果失败
  # {"errors":[{"message":{"code":2004,"message":"用户不存在"},"locations":[{"line":2,"column":3}],"path":["updateUser"],"extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":null}

  assert(hash.dig("id") != nil, hash)
end

#test_user_create_2Object

创建用户ruby ./lib/test/mini_test/TestManagementClient.rb -n test_user_create_2



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/authing_ruby/test/mini_test/TestManagementClient.rb', line 44

def test_user_create_2
  managementClient = AuthingRuby::ManagementClient.new(@options)
  random_phone = "1350000#{rand(1000..9999)}"
  userInfo = {
    nickname: 'Nick Water3',
    phone: random_phone, # 由于是管理员操作,所以不需要检验手机号验证码, 如果你需要检验,请使用  AuthenticationClient
    loginsCount: 2, # 原有用户系统记录的用户登录次数
    signedUp: '2020-10-15T17:55:37+08:00' # 原有用户系统记录的用户注册时间
  }
  hash = managementClient.users.create(userInfo)
  assert(hash.dig("id") != nil, hash)
end

#test_users_createObject

创建用户ruby ./lib/authing_ruby/test/mini_test/TestManagementClient.rb -n test_users_create



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

def test_users_create
  managementClient = AuthingRuby::ManagementClient.new(@options)
  random_phone = "135#{@helper.randomNumString(8)}"
  userInfo = {
    # username: @helper.randomString, # 随机字符串,比如 "mpflok"
    # username: 'SpongeBob3', # 或者明确指定用户名
    phone: random_phone,
    password: '123456789',
  }
  hash = managementClient.users.create(userInfo)
  
  # 成功
  # {"id":"608ab9828eab7e35e81bd732","arn":"arn:cn:authing:60800b8ee5b66b23128b4980:user:608ab9828eab7e35e81bd732","userPoolId":"60800b8ee5b66b23128b4980","status":"Activated","username":"SpongeBob2","email":null,"emailVerified":false,"phone":null,"phoneVerified":false,"unionid":null,"openid":null,"nickname":null,"registerSource":["import:manual"],"photo":"default-user-avatar.png","password":"91b133c2e13e40852505946b7e0c2f04","oauth":null,"token":null,"tokenExpiredAt":null,"loginsCount":0,"lastLogin":null,"lastIP":null,"signedUp":null,"blocked":false,"isDeleted":false,"device":null,"browser":null,"company":null,"name":null,"givenName":null,"familyName":null,"middleName":null,"profile":null,"preferredUsername":null,"website":null,"gender":"U","birthdate":null,"zoneinfo":null,"locale":null,"address":null,"formatted":null,"streetAddress":null,"locality":null,"region":null,"postalCode":null,"city":null,"province":null,"country":null,"createdAt":"2021-04-29T13:49:54+00:00","updatedAt":"2021-04-29T13:49:54+00:00","externalId":null}
  
  # 失败
  # {"errors":[{"message":{"code":2026,"message":"用户已存在,请勿重复创建!"},"locations":[{"line":2,"column":3}],"path":["createUser"],"extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":null}

  assert(hash.dig("id") != nil, hash)
end