Ejemplo n.º 1
0
class BasicAuthAndApiKeyTestCase(unittest.TestCase):
    """ test cases for authorzation related """

    def setUp(self):
        self.s = SwaggerSecurity(app)
        self.s.update_with('simple_key', '123456')
        self.s.update_with('simple_basic', ('mission', '123123'))
        self.s.update_with('simple_basic2', ('qoo', '456456'))
        self.s.update_with('simple_key2', '789789')

    def test_deleteUser(self):
        """ basic auth """
        req, _ = app.op['deleteUser'](username='******')
        self.s(req).prepare()

        self.assertTrue('Authorization' in req.header)
        self.assertEqual(req.header['Authorization'], 'Basic bWlzc2lvbjoxMjMxMjM=')

    def test_getUserByName(self):
        """ api key, passed by query """
        req, _ = app.op['getUserByName'](username='******')
        self.s(req).prepare()

        qk = [x for x in req.query if x[0] == 'simpleQK']
        self.assertTrue(len(qk) > 0)
        self.assertEqual(qk[0][1], '123456')

    def test_createUser(self):
        """ api key, passed by header """
        req, _ = app.op['createUser'](body=dict(id=0, username='******', firstName='liao', lastName='mission'))
        self.s(req).prepare()

        self.assertTrue('simpleHK' in req.header)
        self.assertEqual(req.header['simpleHK'], '789789')

    def test_getAllUsers(self):
        """ test global auth """
        req, _ = app.op['getAllUsers']()
        self.s(req).prepare()

        self.assertTrue('Authorization' in req.header)
        self.assertEqual(req.header['Authorization'], 'Basic cW9vOjQ1NjQ1Ng==')
Ejemplo n.º 2
0
from pyswagger import SwaggerApp, SwaggerSecurity
from pyswagger.contrib.client.requests import Client
from pyswagger.utils import jp_compose

# load Swagger resource file into SwaggerApp object
app = SwaggerApp._create_('http://petstore.swagger.io/v2/swagger.json')

auth = SwaggerSecurity(app)
auth.update_with('api_key', '12312312312312312313q') # api key
auth.update_with('petstore_auth', '12334546556521123fsfss') # oauth2

# init swagger client
client = Client(auth)

# a dict is enough for representing a Model in Swagger
pet_Tom=dict(id=1, name='Tom', photoUrls=['http://test'])
# a request to create a new pet
client.request(app.op['addPet'](body=pet_Tom))

# - access an Operation object via SwaggerApp.op when operationId is defined
# - a request to get the pet back
pet = client.request(app.op['getPetById'](petId=1)).data
assert pet.id == 1
assert pet.name == 'Tom'

# new ways to get Operation object corresponding to 'getPetById'.
# 'jp_compose' stands for JSON-Pointer composition
pet = client.request(app.resolve(jp_compose('/pet/{petId}', base='#/paths')).get(petId=1)).data
assert pet.id == 1
Ejemplo n.º 3
0
from pyswagger import SwaggerApp, SwaggerSecurity
from pyswagger.contrib.client.requests import Client
from pyswagger.utils import jp_compose

# load Swagger resource file into SwaggerApp object
app = SwaggerApp._create_('http://petstore.swagger.io/v2/swagger.json')

auth = SwaggerSecurity(app)
auth.update_with('api_key', '12312312312312312313q')  # api key
auth.update_with('petstore_auth', '12334546556521123fsfss')  # oauth2

# init swagger client
client = Client(auth)

# a dict is enough for representing a Model in Swagger
pet_Tom = dict(id=1, name='Tom', photoUrls=['http://test'])
# a request to create a new pet
client.request(app.op['addPet'](body=pet_Tom))

# - access an Operation object via SwaggerApp.op when operationId is defined
# - a request to get the pet back
pet = client.request(app.op['getPetById'](petId=1)).data
assert pet.id == 1
assert pet.name == 'Tom'

# new ways to get Operation object corresponding to 'getPetById'.
# 'jp_compose' stands for JSON-Pointer composition
pet = client.request(
    app.resolve(jp_compose('/pet/{petId}', base='#/paths')).get(petId=1)).data
assert pet.id == 1