Git Product home page Git Product logo

mini-apivore's Introduction

MiniApivore

MiniApivore is an adaptation of apivore gem for mini-test instead of rspec.

Original project: https://github.com/westfieldlabs/apivore

So base credits are for the apivore authors, this is 60% copy/paste of original project. Rem: didn't forked cause didn't expect it to be a relatively small changes.

What's new/different

  • Swagger schema can be loaded from file or directly. One schema per MiniTestClass.
  • Removed all dependencies from active support and rails. See test as example of how to use mini-apivore outside rails
  • Didn't implement custom schema validator ( but keeped schema and code from apivore in case of future need )
  • Test for untested routes added by default at the end of runnable_methods
  • Much simplified tests against original project, rspec is replaced with minitest
  • Removed all rspec dependencies and usage.

Installation

Add this line to your application's Gemfile:

gem 'mini-apivore'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mini-apivore

Usage

To start testing routes with mini_apivore you need:

  • require 'mini_apivore' in you MiniTest class file
  • include MiniApivore in you MiniTest class
  • init_swagger('/apidocs.json') init swagger-schema against which yourtest are gonna run
  • Run check_route( :get, '/cards.json', OK ) against all routes in your swagger schema

You can see example in test/mini_apivore/mini_apivore/api_schemas_test.rb

Here another complete example of testing simple internal REST api for Card model with devise integration as authentication framework

#mini_apivore_helper.rb
require 'mini_apivore'

# this is simple intermediate class for apivore tes classes
class MiniApivoreTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
  include MiniApivore

  # swagger checker inited once per class, but since we using one definition
  # for all we need redefine original swagger_checker
  def swagger_checker;
    SWAGGER_CHECKERS[MiniApivoreTest]
  end
  
end
#cards_api_test.rb
require 'test_helper'
require 'mini_apivore_helper'

class CardsApiTest < MiniApivoreTest

  test 'cards unauthorized' do
    card = cards(:valid_card_1)
    check_route( :get, '/cards.json', NOT_AUTHORIZED )
        
    check_route( :get, '/cards/{id}.json', NOT_AUTHORIZED, id: card.id )
    check_route( :patch, '/cards/{id}.json', NOT_AUTHORIZED, id: card.id,
                 _data: { card: { title: '1' } } )
    check_route( :post, '/cards.json', NOT_AUTHORIZED,  _data: { card: { title: '1' } } )
    check_route( :delete, '/cards/{id}.json', NOT_AUTHORIZED, id: card.id )
  end

  test 'cards forbidden' do
    sign_in( users(:first_user) )
    # card with restricted privileges 
    card = cards(:restricted_card)

    check_route( :get, '/cards/{id}.json', FORBIDDEN, id: card.id )
    check_route( :patch, '/cards/{id}.json', FORBIDDEN, id: card.id,
                 _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' } } )

    check_route( :delete, '/cards/{id}.json', FORBIDDEN, id: card.id )
  end


  test 'cards not_found' do
    sign_in( users(:first_user) )
    check_route( :get, '/cards/{id}.json', NOT_FOUND, id: -1 )
    check_route( :patch, '/cards/{id}.json', NOT_FOUND, id: -1 )
    check_route( :delete, '/cards/{id}.json', NOT_FOUND, id: -1 )
  end


  test 'cards REST authorized' do
    sign_in( users(:first_user) )
    check_route( :get, '/cards.json', OK )
    check_route( :get, '/cards/{id}.json', OK, id: cards(:valid_card_1).id )
    

    assert_difference( -> { Card.count } ) do
      check_route( :post, '/cards.json', 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
    assert( 'test card creation' == Card.last.title )

    check_route( :patch, '/cards/{id}.json', OK,
                 _data: { card: { title: 'Nothing' } }, id: Card.last.id )

    assert(Card.last.title == 'Nothing' )

    assert_difference( -> { Card.count }, -1 ) do
      check_route( :delete, '/cards/{id}.json', OK, id:  Card.last.id )
    end

  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mini-apivore. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

mini-apivore's People

Contributors

alekseyl avatar awesome avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.