Skip to content

Instantly share code, notes, and snippets.

@MarioRuiz
Last active April 27, 2020 12:23
Show Gist options
  • Save MarioRuiz/824d7a462b62fd85f02c1a09455deefb to your computer and use it in GitHub Desktop.
Save MarioRuiz/824d7a462b62fd85f02c1a09455deefb to your computer and use it in GitHub Desktop.
Creates 1000 good random and unique requests to register a user and test that the validation of the fields is correct by the user was able to be registered. Send 800 requests where just one field is wrong and verify the user was not able to be created.
# nice_http gem: https://github.com/MarioRuiz/nice_http
# nice_hash gem: https://github.com/MarioRuiz/nice_hash
# string_pattern gem: https://github.com/MarioRuiz/string_pattern
require 'nice_http'
url = "https://reqres.in/"
# The valid national chars to be used to generate random values when required
StringPattern.national_chars<<"ñÑáéíóúÁÉÍÓÚÜü"
# Registration Request Hash
# loginame: from 5 to 10 chars, mandatory lower alpha and numbers. Unique values, not possible to repeat
# password, repeatPwd: from 5 to 10 chars, capital alpha, mandatory numbers. password and repeatPwd should be the same
# name: from 10 to 30 chars, capital and lower alpha including national characters and space
# email: from 20 to 30 chars, valid and unique email
# zip: 5 numbers
# gender: three possible values, male, female or other
# mobilePhone: something like (553)34424533
# comments: from 10 to 130 chars, capital and lower alpha including national characters, space and numbers. Also valid empty string.
# origin: fix text, 'web'
registration_hash = {
loginame: :"5-10:/xn/&",
[:password, :repeatPwd] => :"5-10:L/n/",
name: :"10-30:T_",
email: :"20-30:@&",
zip: :"5:N",
gender: "male|female|other",
mobilePhone: ['(', :'3:N', ')', :'6-8:N'],
comments: :"10-130:0T_n",
origin: 'web'
}
# An example of a generated registration_hash with good values:
{:loginame=>"a61y2n5",
:password=>"Z9pYB",
:repeatPwd=>"Z9pYB",
:name=>"vóXÑUEnÉYDIbEH",
:email=>"Pf1_HD7_ZVEQxmdI0d@snyejww.wv",
:zip=>"44419",
:gender=>"male",
:mobilePhone=>"(273)858591",
:comments=>"QrVFT6iln64éAdEÉIÓóqVjvn8N6IzháTqÑaB7nNúJó4Áñ üéyxYuM1VL4SÓESRmGílwYáCjOsZhNvumÉF",
:origin=>"web"}
# Create the http connection
http = NiceHttp.new(url)
# Successful registration of 1000 random good users
# To see all requests sent and the responses take a look at nice_http.log file
1000.times {
json_values = registration_hash.gen
resp = http.post path: "/api/register", data: json_values
if (resp.message!="Created" or resp.code!=201)
puts "No succesful user registration for loginame: #{json_values.loginame}. Response: #{resp.code}:#{resp.message}"
end
}
# Wrong minimum length for just one field each time. (:min_length key)
# The possible values you can specify is one or more of these ones:
# :length, :min_length, :max_length, :value, :required_data, :excluded_data, :string_set_not_allowed
array_of_hashes = Array.new()
100.times {
wrong_min_length_hash = registration_hash.gen errors: :min_length
array_of_hashes += NiceHash.change_one_by_one(registration_hash, wrong_min_length_hash)
}
# array_of_hashes will contain 800 hashes with only one wrong field each time
array_of_hashes.each {|hash_one_wrong|
resp = http.post path: "/api/register", data: hash_one_wrong
#if you want to know which field is the one that is wrong:
wrong_field = registration_hash.validate(:correct, hash_one_wrong)
http.logger.warn "Wrong field: #{wrong_field}"
if (resp.message=="Created" or resp.code==201)
puts "Succesful user registration for loginame: #{hash_one_wrong.loginame} and wrong field: #{wrong_field.keys}. Response: #{resp.code}:#{resp.message}"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment