Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing invalid inputs #88

Closed
aaltat opened this issue Sep 26, 2016 · 5 comments
Closed

Testing invalid inputs #88

aaltat opened this issue Sep 26, 2016 · 5 comments
Labels
Milestone

Comments

@aaltat
Copy link

aaltat commented Sep 26, 2016

Is it possible to test with invalid inputs? I have two requirements

  • Use data which is not according the specification.
  • Use other http methods than defined in the specification.

For the first one when the specification says that idmust be integer, I would like to input a string and verify that proper error code is retruned. For the second one intead of using the http post for the addPet, I would like to use http get or delete. Is this something possible and if yes, how I can do it?

Example when I try to do this:

from pyswagger import App
from pyswagger.contrib.client.requests import Client

app = App._create_('http://petstore.swagger.io/v2/swagger.json')

client = Client()
pet_tom = dict(
    id=123,
    name='Tom',
    photoUrls='https://github.com/',
    status='available')
res = client.request(app.op['addPet'](body=pet_tom))
assert res.status == 200

pet_tom = dict(
    id='not_valid',
    name='Tom',
    photoUrls='https://github.com/',
    status='available')
res = client.request(app.op['addPet'](body=pet_tom))
assert res.status == 405

I get error from pyswagger side:

Traceback (most recent call last):
  File "C:\tmp\try_swagger.py", line 20, in <module>
    res = client.request(app.op['addPet'](body=pet_tom))
  File "C:\Python27\lib\site-packages\pyswagger\spec\v2_0\objects.py", line 283, in __call__
    _convert_parameter(final(p))
  File "C:\Python27\lib\site-packages\pyswagger\spec\v2_0\objects.py", line 264, in _convert_parameter
    c = p._prim_(v, self._prim_factory, ctx=dict(read=False))
  File "C:\Python27\lib\site-packages\pyswagger\spec\v2_0\objects.py", line 184, in _prim_
    return prim_factory.produce(self.schema, v, ctx) if i == 'body' else prim_factory.produce(self, v, ctx)
  File "C:\Python27\lib\site-packages\pyswagger\primitives\__init__.py", line 182, in produce
    val = _2nd(obj, ret, val, ctx)
  File "C:\Python27\lib\site-packages\pyswagger\primitives\comm.py", line 40, in _2nd_pass_obj
    return ret.apply_with(obj, val, ctx) 
  File "C:\Python27\lib\site-packages\pyswagger\primitives\_model.py", line 29, in apply_with
    self[k] = ctx['factory'].produce(pobj, v)
  File "C:\Python27\lib\site-packages\pyswagger\primitives\__init__.py", line 180, in produce
    ret = creater(obj, val, ctx)
  File "C:\Python27\lib\site-packages\pyswagger\primitives\_int.py", line 12, in create_int
    r = int(v)
ValueError: invalid literal for int() with base 10: 'not_valid'
@mission-liao
Copy link
Member

My proposal is to patch the pyswagger.io.Request before passing it to client.request via inspecting _p property and patch it by yourself. For example, in your case, it could be done like this:

from pyswagger import App                                                                                                                                  
from pyswagger.contrib.client.requests import Client                           

app = App._create_('http://petstore.swagger.io/v2/swagger.json')               
client = Client()                                                                                             

pet_tom = dict(                                                                                               
    id=123,                                                                                                   
    name='Tom',                                                                                               
    photoUrls='https://github.com/',
    status='available')                                                                                       

req, resp = app.op['addPet'](body=pet_tom) 

#  patching the "body" parameter named "body", ... which seems cumbersome
req._p['body']['body']['id'] = 'not_valid'

resp = client.request((req, resp,))                                                                            
print resp.status # the code I tried is 500, not 405

Note: this property is used internally for testing or debugging when developing this library, and might be changed with the latest version of Swagger (Open API) (ex. 'form' becomes 'formData' after upgrading from swagger 1.2 to 2.0)

@aaltat
Copy link
Author

aaltat commented Sep 26, 2016

Thank you from the information. Definitely will try it out tomorrow and look more closely on the req and resp objects how to change the http post to http get for example.

@aaltat
Copy link
Author

aaltat commented Sep 27, 2016

I did found a way, by doing this, I can overwrite the http method from post to get:

from pyswagger import App
from pyswagger.contrib.client.requests import Client

app = App._create_('http://petstore.swagger.io/v2/swagger.json')
client = Client()

pet_tom = dict(
    id=123,
    name='Tom',
    photoUrls='https://github.com/',
    status='available'
)
# import rpdb2; rpdb2.start_embedded_debugger('12345678')
req, resp = app.op['addPet'](body=pet_tom)

req._Request__op._Operation__method = 'get'

resp = client.request((req, resp,))
print resp.status  # the code I tried is 500, not 405
print resp.raw

By this it should be easy task to look over the http methods which are not supported by the API and verify that correct status code is received. Also your example gives a clean solution how to manipulate the _p object in the way which is required to test invalid data scenarios.

My application is using swagger 2.0 and next I will start plying with that. But debugger should be able to guide me to the right direction. Thank you from the help. But because this might be useful to someone else too, should this be placed in the wiki pages, or somewhere other documentation?

@mission-liao
Copy link
Member

Did you mean the part to manipulate _p, and anything else? I think it's a good idea to document those stuffs. (Please correct me if you didn't mean that part). I'll create some md files in tutorial part later.

@mission-liao mission-liao added this to the v0.8.22 milestone Sep 27, 2016
@aaltat
Copy link
Author

aaltat commented Sep 27, 2016

Yes, that _p manipulation in md file could be useful.

mission-liao added a commit that referenced this issue Oct 1, 2016
- #88
- also update some document
mission-liao added a commit to pyopenapi/pyopenapi that referenced this issue Aug 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants