diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b87bfc3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +gem 'faker' +gem 'httparty' +gem 'rspec' +gem 'rubocop' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..8ae76d2 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,61 @@ +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + concurrent-ruby (1.1.9) + diff-lcs (1.4.4) + faker (2.18.0) + i18n (>= 1.6, < 2) + httparty (0.18.1) + mime-types (~> 3.0) + multi_xml (>= 0.5.2) + i18n (1.8.10) + concurrent-ruby (~> 1.0) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2021.0704) + multi_xml (0.6.0) + parallel (1.20.1) + parser (3.0.1.1) + ast (~> 2.4.1) + rainbow (3.0.0) + regexp_parser (2.1.1) + rexml (3.2.5) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.2) + rubocop (1.18.3) + parallel (~> 1.10) + parser (>= 3.0.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.7.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.7.0) + parser (>= 3.0.1.1) + ruby-progressbar (1.11.0) + unicode-display_width (2.0.0) + +PLATFORMS + x86_64-darwin-19 + +DEPENDENCIES + faker + httparty + rspec + rubocop + +BUNDLED WITH + 2.2.21 diff --git a/README.md b/README.md new file mode 100644 index 0000000..0cdaad0 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +### This repository uses: + +- https://github.com/jnunemaker/httparty/ +- https://github.com/rspec/rspec +- https://github.com/serverest/serverest + + +### Documentation: + +- https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/ClassMethods +- https://www.rubydoc.info/github/jnunemaker/httparty/HTTParty/Response +- https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers diff --git a/services/user_service.rb b/services/user_service.rb new file mode 100644 index 0000000..ba7b9e8 --- /dev/null +++ b/services/user_service.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Request + include HTTParty + base_uri 'https://serverest.dev' + format :json +end diff --git a/spec/get_spec.rb b/spec/get_spec.rb new file mode 100644 index 0000000..deaf5b3 --- /dev/null +++ b/spec/get_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +describe 'getting information' do + it 'get users' do + @users = Request.get('/usuarios') + expect(@users.code).to eq(200) + puts @users + # expect(@users['usuarios'][0]['nome']).to eq('Fulano da Silva') + end +end + +# # Or wrap things up in your own class +# class StackExchange +# include HTTParty +# base_uri 'api.stackexchange.com' + +# def initialize(service, page) +# @options = { query: { site: service, page: page } } +# end + +# def questions +# self.class.get("/2.2/questions", @options) +# end + +# def users +# self.class.get("/2.2/users", @options) +# end +# end + +# stack_exchange = StackExchange.new("stackoverflow", 1) +# puts stack_exchange.questions +# puts stack_exchange.users + +# httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow" --format json (--help) diff --git a/spec/post_spec.rb b/spec/post_spec.rb new file mode 100644 index 0000000..cdf9854 --- /dev/null +++ b/spec/post_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +describe 'posting information' do + it 'register user' do + @new_user = { + 'nome' => Faker::Name.name, + 'email' => Faker::Internet.email, + 'password' => Faker::Internet.password, + 'administrador' => Faker::Boolean.boolean + } + # puts @new_user + + @request = Request.post('/usuarios', body: @new_user) + expect(@request.code).to eq(201) + expect(@request['_id']).not_to be nil + puts @id_user = @request['_id'] + expect(@request['message']).to eq('Cadastro realizado com sucesso') + end +end diff --git a/spec/put_spec.rb b/spec/put_spec.rb new file mode 100644 index 0000000..46526d3 --- /dev/null +++ b/spec/put_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe 'changing information' do + it 'modify user information' do + @new_user = { + 'nome' => Faker::Name.name, + 'email' => Faker::Internet.email, + 'password' => Faker::Internet.password, + 'administrador' => Faker::Boolean.boolean + } + # puts @new_user + + @id_user = 'DE7RTKT8SWmcIc3e' + @request = Request.put(%(/usuarios/#{@id_user}), body: @new_user) + expect(@request.code).to eq(200) + expect(@request['message']).to eq('Registro alterado com sucesso') + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..840d88b --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'faker' +require 'httparty' +require 'rubocop' + +require_relative '../services/user_service' + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups +end