class TestManagementClient

Public Instance Methods

create_random_user(userInfo = {}) click to toggle source

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

# 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
setup() click to toggle source
# 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_delete() click to toggle source

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

# 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_deleteMany() click to toggle source

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

# 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_detail() click to toggle source

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

# 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_exists() click to toggle source

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

# 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": "haha2@qq.com",
    # "phone": "13700001111",
  }
  boolean = managementClient.users.exists(options)
  assert(boolean)
end
test_find() click to toggle source

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

# 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": "haha2@qq.com",
    # "phone": "13700001111",
  }
  hash = managementClient.users.find(options)
  assert(hash.dig("id"), hash)
end
test_list() click to toggle source

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

# 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_user() click to toggle source

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

# 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_2() click to toggle source

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

# 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_create() click to toggle source

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

# 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