Last active
October 23, 2021 13:33
-
-
Save alekseyl/28f2fca625f4fd42fec5b23fedd3baeb to your computer and use it in GitHub Desktop.
mini-apivore example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cards_api_test.rb | |
require 'test_helper' | |
require 'mini_apivore_helper' | |
class CardsApiTest < MiniApivoreTest | |
#------- DEFINE CLASS SPECIFIC NAMED ROUTE HELPERS ---------------- | |
def __get_cards(expectation) | |
check_route( :get, '/cards.json', expectation ) | |
end | |
def __get_card( card, expectation) | |
# check_route will use to_param inside | |
check_route( :get, '/cards/{id}.json', expectation, id: card ) | |
end | |
def __update_card( card, expectation, params = {}) | |
# check_route will use to_param inside | |
check_route( :patch, '/cards/{id}.json', expectation, id: card, **params) | |
end | |
def __create_card( expectation, params = {}) | |
check_route( :post, '/cards.json', expectation, params ) | |
end | |
def __delete_card(card, expectation) | |
check_route( :delete, '/cards/{id}.json', expectation, id: card ) | |
end | |
#------- DEFINE CLASS SPECIFIC NAMED ROUTE HELPERS DONE ----------- | |
# | |
# failure need a proper stackframe and a context around: | |
def prepare_error_backtrace | |
# it will deliver something like this: | |
#"/app/test/helpers/base_routes_helpers.rb:57:in `__create_card'", | |
#"/app/test/integration/cards_api_test.rb:71:in `block (2 levels) in <class:CommentsApiTest>'", | |
Thread.current.backtrace[2..-1].slice_after{|trc| trc[/check_route/] }.to_a.last[0..1] | |
end | |
test 'cards unauthorized' do | |
card = cards(:valid_card_1) | |
__get_cards( NOT_AUTHORIZED ) | |
__get_card( card, NOT_AUTHORIZED ) | |
__update_card( card, NOT_AUTHORIZED, _data: { card: { title: '1' } } ) | |
__create_card( NOT_AUTHORIZED, _data: { card: { title: '1' } } ) | |
__delete_card( card, NOT_AUTHORIZED ) | |
end | |
test 'cards forbidden' do | |
sign_in( :first_user ) | |
# card with restricted privileges | |
card = cards(:restricted_card) | |
__get_card( card, FORBIDDEN ) | |
__update_card( card, FORBIDDEN, id: card, _data: { card: { title: '1' } } ) | |
# this may be added if not all users can create cards | |
# check_route( :post, '/cards.json', FORBIDDEN, _data: { card: { title: '1' } } ) | |
__delete_card( card, FORBIDDEN) | |
end | |
test 'cards not_found' do | |
sign_in( :first_user ) | |
card = Card.new(id: -1) | |
__get_card( card, NOT_FOUND ) | |
__update_card( card, NOT_FOUND ) | |
__delete_card( card, NOT_FOUND ) | |
end | |
test 'cards REST authorized' do | |
sign_in( :first_user ) | |
__get_cards( OK ) | |
__get_cards( cards(:valid_card_1), OK ) | |
assert_difference( -> { Card.count } ) do | |
__create_card( OK, _data: { | |
card: { title: 'test card creation', | |
card_preview_img_attributes: { | |
upload: fixture_file_upload( Rails.root.join('test', 'fixtures', 'files', 'test.png') ,'image/png') | |
} | |
} } ) | |
end | |
created_card = Card.last | |
assert_equal( 'test card creation', created_card.title ) | |
__update_card( created_card, OK, _data: { card: { title: 'Nothing' } } ) | |
assert_equal( created_card.reload.title, 'Nothing' ) | |
assert_difference( -> { Card.count }, -1 ) do | |
__delete_card( created_card, NO_CONTENT ) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment