Code Monkey home page Code Monkey logo

scanapi's Introduction

Codecov CircleCI LintCheck Examples Downloads Per Month PyPI version Discord

A library for your API that provides:

  • Automated Integration Testing
  • Automated Live Documentation

Given an API specification, written in YAML/JSON format, ScanAPI hits the specified endpoints, runs the test cases, and generates a detailed report of this execution - which can also be used as the API documentation itself.

With almost no Python knowledge, the user can define endpoints to be hit, the expected behavior for each response and will receive a full real-time diagnostic report of the API!

Contents

Requirements

How to install

$ pip install scanapi

Basic Usage

You will need to write the API's specification and save it as a YAML or JSON file. For example:

endpoints:
  - name: scanapi-demo # The API's name of your API
    path: http://demo.scanapi.dev/api/v1 # The API's base url
    requests:
      - name: list_all_users # The name of the first request
        path: users/ # The path of the first request
        method: get # The HTTP method of the first request
        tests:
          - name: status_code_is_200 # The name of the first test for this request
            assert: ${{ response.status_code == 200 }} # The assertion

And run the scanapi command

$ scanapi run <file_path>

Then, the lib will hit the specified endpoints and generate a scanapi-report.html file with the report results.

An overview screenshot of the report. A screenshot of the report showing the request details.

Documentation

The full documentation is available at scanapi.dev

Examples

You can find complete examples at scanapi/examples!

This tutorial helps you to create integration tests for your REST API using ScanAPI

Watch the video

Contributing

Collaboration is super welcome! We prepared the Newcomers Guide to help you in the first steps. Every little bit of help counts! Feel free to create new GitHub issues and interact here.

Let's build it together πŸš€πŸš€

scanapi's People

Contributors

5pence avatar abreumatheus avatar astenstrasser avatar barbosa avatar camilamaia avatar crocmagnon avatar dbzkunalss avatar dependabot[bot] avatar djalmaaraujo avatar dubirajara avatar gabrieltron avatar gillianomenezes avatar gitvitor avatar hebertjulio avatar jhermann avatar jrubics avatar kelvins avatar leonardomaier avatar marcuxyz avatar pradhvan avatar rafaquelhodev avatar rajarshig avatar renato04 avatar rvmoura96 avatar stannislav avatar supakeen avatar taconi avatar uesleicarvalhoo avatar vinigfer avatar winstonf88 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scanapi's Issues

Refactor on evaluation process

Improve Error Message for Invalid Python code error

Current, when there is an error when executing a command inside the python code tag ${{ }}, this is the message:

ERROR:scanapi.evaluators.string_evaluator:Invalid Python code defined in the API spec: Expecting value: line 1 column 1 (char 0)

We should improve it to show precisely which error happened.

Alert if CHANGELOG.md was not updated

Description

When a PR is opened and the CHANGELOG.md was not modified, send an alert to remember to update it.

Possible to be implemented with:

Or, alternatively, to create a CircleCI check that fails when it is not updated.

Create ScanAPI spec from an OpenAPI specification

Create ScanAPI spec from an OpenAPI specification

Create a command that converts an OpenAPI specification file into a ScanAPI specification file.
OpenAPI Specification: https://swagger.io/specification/

For that, we need to create a new Click command called convert in the __main__.py file.

With the arguments:

  • input file path (required)
  • output file path (required)

And with the options:

  • -f, --from, input_format. (default should be openapi, required)
  • -t, --to, output_format. (default should be scanapi, required)

Example:

$ scanapi convert -f openapi -t scanapi OPENAPI_PATH SCANAPI_PATH 

Related issue: ADR 6: How to integrate feature that converts OpenAPI file to ScanAPI file

Make console report pretty

To make console report prettier. And to add response time to it.

This is how it looks like now:

ScanAPI Report: Console
=======================

GET http://demo.scanapi.dev/api/health/ - 200
GET http://demo.scanapi.dev/api/languages/ - 200
GET http://demo.scanapi.dev/api/devs/ - 200
GET http://demo.scanapi.dev/api/devs/?newOpportunities=True - 200
GET http://demo.scanapi.dev/api/devs/?newOpportunities=False - 200
POST http://demo.scanapi.dev/api/devs/ - 201
GET http://demo.scanapi.dev/api/devs/129e8cb2-d19c-51ad-9921-cea329bed7fa - 404
GET http://demo.scanapi.dev/api/devs/129e8cb2-d19c-41ad-9921-cea329bed7f0 - 200
DELETE http://demo.scanapi.dev/api/devs/129e8cb2-d19c-41ad-9921-cea329bed7f0 - 200
GET http://demo.scanapi.dev/api/devs/129e8cb2-d19c-41ad-9921-cea329bed7f0/languages - 200

Maybe to add some different colours to each HTTP method?

πŸ’„

No module named 'scanapi.tree'

Error when running scanapi v0.0.15

    from scanapi.tree.api_tree import APITree
ModuleNotFoundError: No module named 'scanapi.tree'

Stop using eval to evaluate python code on API specification

Currently we are evaluating python code from API specification ${{ code }} using eval:

sequence, match.group(), str(eval(code))

We must use a safer and more elegant solution. I am out of ideas here.

  • Templates (like jinja) don't work because we need to evaluate dynamically the python code while the requests are being made. The python code might depend on the last request response, for example.
  • ast.literal_eval does not support access to external variables

Allow PATCH HTTP Method

Hi, I see that HTTP requests PATCH method are not implemented, for any particular reason?

Thank you.
Congratulations on the project, it is a great idea. πŸ‘Œ

Implement Cases

...
          requests:
                  - name: list_all # posts_list_all
                    method: get
                    case: when user is not authenticated
                    ...
                  - name: list_all # posts_list_all
                    method: get
                    case: when user is authenticated
                    ...

Validate mandatory keys for API spec

Description

We need to ensure that API spec has some mandatories keys in order to work properly.

The first mandatory key that need to be checked is the key api. The specification should start with it.
https://github.com/scanapi/scanapi/blob/master/scanapi/__init__.py#L68

Under the key endpoints, we need to ensure each entry has at least a name and a requests key
https://github.com/scanapi/scanapi/blob/master/scanapi/tree/endpoint_node.py#L79

Under the key requests, we need to ensure each entry has at least a name and a path key.
https://github.com/scanapi/scanapi/blob/master/scanapi/tree/request_node.py#L106

This is an example of a minimal possible structure:

api:
  endpoints:
    - name: scanapi-demo
      requests:
        - name: health
          path: http://demo.scanapi.dev/api/health/

If any of this mandatories keys is missing, an error should be raised.

Update CONTRIBUTING.md

Description

Update CONTRIBUTING.md with:

  • Instructions of how to create a release PR
  • Instructions of how to deploy on docker hub with version tag

Hide sensitive info from headers is not working

Description

After the refactor on the reporter templates, hide sensitive info from headers is not working. It shows the real data instead of "<sensitive information>"

How to reproduce

Create a config file .scanapi.yaml with the content:

docs:
  hide:
    headers:
      - Authorization

Run scanapi with this config file and set an Authorization header inside the api specification:

api:
  base_url: ${BASE_URL}
  headers:
    Authorization: token123

The word token123 will appear in the report, instead of "<sensitive information>"

Considering any variable that has at least one upper case letter as env var

Problem

If we define a var containing an upper case letter, like:

vars:
    apiKey: abc123

and we try to use it later as ${apiKey} it will raise the error:

ERROR:scanapi.evaluators.string_evaluator:'apiKey' environment variable not set or badly c

Possible Solution

To check if there is any lower case in the word here:

https://github.com/camilamaia/scanapi/blob/master/scanapi/evaluators/string_evaluator.py#L48

if any(letter.islower() for letter in variable_name):
  continue

Create Expected Assertions for Requests

A assertions.yaml with some assertions for each request.

for example, posts_list_all must have:

  • x Length?
  • this Structure?
  • which type of data for each field?

Tasks:

  • define a format for it
  • implement how to run the assertions

in the future, we can create alerts when the assertions fails

Hide tokens and authorization info in the generated report

Description

Add the possibility to hide tokens and authorization info in the generated report to avoid expose sensitive information via configuration file (usually .scanapi.yaml).

Change the sensitive information value to <sensitive_information>

Configuration Options:

  • report
    -- hide-response or hide-request
    --- headers or body or url
    ---- list of keys to hide

Example:

report:
  hide-response:
    headers:
      - Authorization
      - api-key
  hide-response:
    body:
      - api-key

The logic is implemented inside the hide_sensitive_info method

Example of how this should be rendered in the reports:

image

  • header for request
  • header for response
  • body for request
  • body for response
  • url for request
  • url for response

Add Factory Boy

factory_boy is a fixtures replacement based on thoughtbot’s factory_bot

As a fixtures replacement tool, it aims to replace static, hard to maintain fixtures with easy-to-use factories for complex object.

Instead of building an exhaustive test setup with every possible combination of corner cases, factory_boy allows you to use objects customized for the current test, while only declaring the test-specific fields:

Make html report pretty

πŸ’„

  • Show only path_url in the titles
  • Add response and request headers
  • Add request headers and body
  • Add response headers, metada and body
  • Add cURL
  • Add response time

Implement Query Params

Similar to headers implementation

api:
  base_url: ${BASE_URL}
  headers:
    Authorization: ${BEARER_TOKEN}
  params:
    per_page: 10

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.