what is difference between `django.test.APITestCase` and `rest_framework.test.TestCase` in `python-django` - python-3.x

What is the major difference in these test classes django.test.TestCase and rest_framework.test.APITestCase . which is Better to test my views.py ?
can you suggest me documentations to understand about these.
Thank you in advance. :-)

Difference between rest_framework.test.APITestCase and django.test.testcases.TestCase in HTTP Client they use (source: github).
With rest_framework APIClient it easier to test API than with Django Client. For example, less work with json responses. See more: http://www.django-rest-framework.org/api-guide/testing/

APITestCase in rest_framework.test is to test the api's in the rest. It is specific for the api operations and api calls.
Django.test.TestCase is used to test the Django classes.

Related

How to create a 500 error on an rest API for error testing purpose?

I am writing some test code for my rest api using nodejs and chai.
I would like to create a 500 error on purpose to be sure my API react well, but I have no idea on how to do that.
Any help or tips about best practices would be really appreciated.
Thanks in advance
Try this URL, this is dedicated for testing purpose :
https://httpstat.us/500
Or expose a php script with garbage in it, like :
<?php
foobarbase
You can use following syntax:
res.status(500).json({your message});

How should I use Swagger with Hapi?

I have a working ordinary Hapi application that I'm planning to migrate to Swagger. I installed swagger-node using the official instructions, and chose Hapi when executing 'swagger project create'. However, I'm now confused because there seem to be several libraries for integrating swagger-node and hapi:
hapi-swagger: the most popular one
hapi-swaggered: somewhat popular
swagger-hapi: unpopular and not that active but used by the official Swagger Node.js library (i.e. swagger-node) as default for Hapi projects
I though swagger-hapi was the "official" approach, until I tried to find information on how do various configurations on Hapi routes (e.g. authorization, scoping, etc.). It seems also that the approaches are fundamentally different, swagger-hapi taking Swagger definition as input and generating the routes automatically, whereas hapi-swagger and hapi-swaggered seem to have similar approach with each other by only generating Swagger API documentation from plain old Hapi route definitions.
Considering the amount of contributors and the number of downloads, hapi-swagger seems to be the way to go, but I'm unsure on how to proceed. Is there an "official" Swagger way to set up Hapi, and if there is, how do I set up authentication (preferably by using hapi-auth-jwt2, or other similar JWT solution) and authorization?
EDIT: I also found swaggerize-hapi, which seems to be maintained by PayPal's open source kraken.js team, which indicates that it might have some kind of corporate backing (always a good thing). swaggerize-hapi seems to be very similar to hapi-swagger, although the latter seems to provide more out-of-the-box functionality (mainly Swagger Editor).
Edit: Point 3. from your question and understanding what swagger-hapi actually does is very important. It does not directly serves the swagger-ui html. It is not intended to, but it is enabling the whole swagger idea (which the other projects in points 1. and 2. are actually a bit reversing). Please see below.
It turns out that when you are using swagger-node and swagger-hapi you do not need all the rest of the packages you mentioned, except for using swagger-ui directly (which is used by all the others anyways - they are wrapping it in their dependencies)
I want to share my understanding so far in this hapi/swagger puzzle, hope that these 8 hours that I spent can help others as well.
Libraries like hapi-swaggered, hapi-swaggered-ui, also hapi-swagger - All of them follow the same approach - that might be described like that:
You document your API while you are defining your routes
They are somewhat sitting aside from the main idea of swagger-node and the boilerplate hello_world project created with swagger-cli, which you mentioned that you use.
While swagger-node and swagger-hapi (NOTE that its different from hapi-swagger) are saying:
You define all your API documentation and routes **in a single centralized place - swagger.yaml**
and then you just focus on writing controller logic. The boilerplate project provided with swagger-cli is already exposing this centralized place swagger.yaml as json thru the /swagger endpoint.
Now, because the swagger-ui project which all the above packages are making use of for showing the UI, is just a bunch of static html - in order to use it, you have two options:
1) to self host this static html from within your app
2) to host it on a separate web app or even load the index.html directly from file system.
In both cases you just need to feed the swagger-ui with your swagger json - which as said above is already exposed by the /swagger endpoint.
The only caveat if you chose option 2) is that you need to enable cors for that end point which happened to be very easy. Just change your default.yaml, to also make use of the cors bagpipe. Please see this thread for how to do this.
As #Kitanotori said above, I also don't see the point of documenting the code programmatically. The idea of just describing everything in one place and making both the code and the documentation engine to understand it, is great.
We have used Inert, Vision, hapi-swagger.
server.ts
import * as Inert from '#hapi/inert';
import * as Vision from '#hapi/vision';
import Swagger from './plugins/swagger';
...
...
// hapi server setup
...
const plugins: any[] = [Inert, Vision, Swagger];
await server.register(plugins);
...
// other setup
./plugins/swagger
import * as HapiSwagger from 'hapi-swagger';
import * as Package from '../../package.json';
const swaggerOptions: HapiSwagger.RegisterOptions = {
info: {
title: 'Some title',
version: Package.version
}
};
export default {
plugin: HapiSwagger,
options: swaggerOptions
};
We are using Inert, Vision and hapi-swagger to build and host swagger documentation.
We load those plugins in exactly this order, do not configure Inert or Vision and only set basic properties like title in the hapi-swagger config.

what is the proper way to create a swagger web service in TypeScript

I am part of a project written in TypeScipt and I am trying to add TypeScript Web Server which will be compatible with Swagger.
What is the most basic strategy to implement it, considering easy maintainability.
For Typescript I have notice that exists 'Typson' library for generating a JSON Model from TypeScript Interface.
For Swagger, I have tried using 'swagger-node-restify' library, since it supports adding JSON models to swagger.
However, I encounter some problems:
Typson doesn't support typeScript syntax of Import -
(import {Server} from "restify")
I tried to implement the 'swagger-node-restify' example (Pet Example), however the RESPONSE of the localhost:8080/api-docs.json GET Request is missing all the SPEC data of the API.
{"apiVersion":"0.1","swaggerVersion":"1.1","basePath":"http://localhost:8080","apis":[{"path":"/api-docs.{format}/pet","description":"none"}]}
I suggest to describe a Swagger compliant API using yaml or json and to generate the server from that.
swagger-server can build APIs on top of express in real time (no source code generation).
There are JavaScript code generators :
Call the swagger-codegen client with -l nodejs-server
swagger-node is a great alternative but seems hard to integrate with TypeScript
Yes, you can easily generate Swagger and OpenAPI documents from your TypeScript types by using tsoa. The readme contains all of the setup information that you would need to start using it. It's compatible with express, hapi, koa, and more (via the ability to add your own template for your preferred server type):
https://github.com/lukeautry/tsoa
The advantages that tsoa has over other libraries is:
it both generates the swagger/OpenAPI document and it also validates the types at runtime
(Full Transparency: I am one of the maintainers of tsoa. But I was first a consumer of tsoa and I find it to be a great product... that's why I asked to help maintain it! :) )

How do i access the Phabricator Maniphest restful APIs?

I'm trying to programmatically create tasks/bugs on Maniphest: https://www.phacility.com/phabricator/maniphest/
but i can't quite seem to find a RESTful API that can do this.
am i totally missing out on something? or does there not currently exist one
Conduit (https://secure.phabricator.com/book/phabricator/article/conduit/) should work for you. There is a method called createtask (looks like https://secure.phabricator.com/conduit/method/maniphest.createtask/) that is exactly what you are looking for.
You can access phabricator's api, to create task ,query user info etc.
here is the demo for access https api by postman .
Note: for multi value field such as "ccPHIDs" , you should use format like the image.
but in conduit UI Test. you have to use json format, like this: ["PHID-PROJ-xxx3", "PHID-PROJ-xx12"]
phabricator's API is the wrost api set, I have used by now. so sick...

Looking for sequelize.js REST API generator with associations support

Is there any node.js module which would provide full REST api sequelize?
Which must follow associations, so for example if I have tables testsuites (hasMany testcases) and testcases, than the api will allow me to do: get /api/testsuite/1/testcases and it will do all the magic for me?
I checked sequelize-restful, but does not seems to work this way :(
See below libraries can help you in generating REST API with sequelize
1.https://github.com/dchester/epilogue
2.https://github.com/sequelize/sequelize-restful
Example: https://github.com/pjanaya/nodejs-express4-sequelize-restful

Resources