annotate test/rest_common.py @ 8266:43dc610bbd24

doc: known issues with possible dependent tests When reversing the testing order, two blocks of tests fail. Document these. I spent time trying to fix them, but got nowhere. Obviously I am missing something. But this doc gives future me or somebody else a starting point.
author John Rouillard <rouilj@ieee.org>
date Thu, 09 Jan 2025 09:32:10 -0500
parents 8c1e0459b73d
children 05d8806b25ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6090
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1 import pytest
5586
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
2 import unittest
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
3 import shutil
8220
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
4 import sys
5586
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
5 import errno
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
6
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
7 from time import sleep
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
8 from datetime import datetime, timedelta
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
9 from roundup.anypy.cgi_ import cgi
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
10 from roundup.anypy.datetime_ import utcnow
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
11 from roundup.date import Date
7853
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
12 from roundup.exceptions import UsageError
6361
58817c3bf471 Fix roundup/test
Ralf Schlatterbeck <rsc@runtux.com>
parents: 6350
diff changeset
13 from roundup.test.tx_Source_detector import init as tx_Source_init
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
14
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
15
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
16 try:
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
17 from datetime import timezone
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
18 myutc = timezone.utc
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
19 except ImportError:
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
20 # python 2
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
21 from datetime import tzinfo
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
22 ZERO = timedelta(0)
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
23
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
24 class UTC(tzinfo):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
25 """UTC"""
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
26 def utcoffset(self, dt):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
27 return ZERO
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
28
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
29 def tzname(self, dt):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
30 return "UTC"
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
31
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
32 def dst(self, dt):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
33 return ZERO
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
34
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
35 myutc = UTC()
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
36
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
37 from roundup.cgi.exceptions import *
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
38 from roundup.hyperdb import HyperdbValueError
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
39 from roundup.exceptions import *
5586
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
40 from roundup import password, hyperdb
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
41 from roundup.rest import RestfulInstance, calculate_etag
5586
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
42 from roundup.cgi import client
6185
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
43 from roundup.anypy.strings import b2s, s2b, us2u
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
44 import random
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
45
5733
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
46 from roundup.backends.sessions_dbm import OneTimeKeys
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
47 from roundup.anypy.dbm_ import whichdb
5733
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
48
5602
c40d04915e23 Python3 fixes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5601
diff changeset
49 from .db_test_base import setupTracker
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
50
6366
f2c31f5ec50b Move mocknull from test to roundup/test
Ralf Schlatterbeck <rsc@runtux.com>
parents: 6361
diff changeset
51 from roundup.test.mocknull import MockNull
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
52
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
53 from io import BytesIO
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
54 import json
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
55
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
56 from copy import copy
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
57
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
58 try:
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
59 import jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
60 skip_jwt = lambda func, *args, **kwargs: func
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
61 except ImportError:
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
62 from .pytest_patcher import mark_class
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
63 jwt = None
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
64 skip_jwt = mark_class(pytest.mark.skip(
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
65 reason='Skipping JWT tests: jwt library not available'))
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
66
8220
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
67 if sys.version_info[0] > 2:
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
68 skip_on_py2 = lambda func, *args, **kwargs: func
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
69 else:
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
70 from .pytest_patcher import mark_class
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
71 skip_on_py2 =mark_class(pytest.mark.skip(
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
72 reason='Skipping test on Python 2'))
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
73
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
74 NEEDS_INSTANCE = 1
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
75
5586
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
76
5601
fcbeff272828 Integrate REST tests into db framework
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5600
diff changeset
77 class TestCase():
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
78
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
79 backend = None
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
80 url_pfx = 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/'
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
81
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
82 def setUp(self):
6314
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
83 from packaging import version
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
84
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
85 self.dirname = '_test_rest'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
86 # set up and open a tracker
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
87 # Set optimize=True as code under test (Client.main()::determine_user)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
88 # will close and re-open the database on user changes. This wipes
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
89 # out additions to the schema needed for testing.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
90 self.instance = setupTracker(self.dirname, self.backend, optimize=True)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
91
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
92 # open the database
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
93 self.db = self.instance.open('admin')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
94
5733
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
95 # Create the Otk db.
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
96 # This allows a test later on to open the existing db and
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
97 # set a class attribute to test the open retry loop
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
98 # just as though this process was using a pre-existing db
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
99 # rather then the new one we create.
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
100 otk = OneTimeKeys(self.db)
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
101 otk.set('key', key="value")
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
102
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
103 # Get user id (user4 maybe). Used later to get data from db.
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
104 self.joeid = self.db.user.create(
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
105 username='joe',
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
106 password=password.Password('random'),
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
107 address='random@home.org',
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
108 realname='Joe Random',
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
109 roles='User'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
110 )
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
111
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
112 self.db.user.set('1', address="admin@admin.com")
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
113 self.db.user.set('2', address="anon@admin.com")
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
114
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
115 # set up some more stuff for testing
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
116 self.db.msg.create(
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
117 author="1",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
118 date=Date(),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
119 summary="stuff",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
120 content="abcdefghi\njklmnop",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
121 type="text/markdown"
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
122 )
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
123
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
124 self.db.msg.create(
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
125 author="1",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
126 date=Date(),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
127 summary="stuff",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
128 content="abcdefghi\njklmnop",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
129 )
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
130
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
131 self.db.file.create(
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
132 name="afile",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
133 content="PNG\x01abcdefghi\njklmnop",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
134 type="image/png"
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
135 )
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
136
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
137 self.db.commit()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
138 self.db.close()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
139 self.db = self.instance.open('joe')
5604
ed02a1e0aa5d Fix actions
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5602
diff changeset
140 # Allow joe to retire
ed02a1e0aa5d Fix actions
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5602
diff changeset
141 p = self.db.security.addPermission(name='Retire', klass='issue')
ed02a1e0aa5d Fix actions
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5602
diff changeset
142 self.db.security.addPermissionToRole('User', p)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
143
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
144 # add set of roles for testing jwt's.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
145 self.db.security.addRole(name="User:email",
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
146 description="allow email by jwt")
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
147 # allow the jwt to access everybody's email addresses.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
148 # this makes it easier to differentiate between User and
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
149 # User:email roles by accessing the /rest/data/user
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
150 # endpoint
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
151 jwt_perms = self.db.security.addPermission(
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
152 name='View',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
153 klass='user',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
154 properties=('id', 'realname', 'address', 'username'),
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
155 description="Allow jwt access to email",
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
156 props_only=False)
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
157 self.db.security.addPermissionToRole("User:email", jwt_perms)
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
158 self.db.security.addPermissionToRole("User:email", "Rest Access")
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
159
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
160 # add set of roles for testing jwt's.
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
161 # this is like the user:email role, but it missing access to the rest endpoint.
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
162 self.db.security.addRole(name="User:emailnorest",
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
163 description="allow email by jwt")
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
164 jwt_perms = self.db.security.addPermission(
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
165 name='View',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
166 klass='user',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
167 properties=('id', 'realname', 'address', 'username'),
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
168 description="Allow jwt access to email but forget to allow rest",
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
169 props_only=False)
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
170 self.db.security.addPermissionToRole("User:emailnorest", jwt_perms)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
171
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
172 if jwt:
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
173 # must be 32 chars in length minimum (I think this is at least
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
174 # 256 bits of data)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
175
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
176 self.old_secret = "TestingTheJwtSecretTestingTheJwtSecret"
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
177 self.new_secret = "TestingTheNEW JwtSecretTestingTheNEWJwtSecret"
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
178 self.db.config['WEB_JWT_SECRET'] = self.old_secret
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
179
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
180 # generate all timestamps in UTC.
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
181 base_datetime = datetime(1970, 1, 1, tzinfo=myutc)
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
182
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
183 # A UTC timestamp for now.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
184 dt = datetime.now(myutc)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
185 now_ts = int((dt - base_datetime).total_seconds())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
186
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
187 # one good for a minute
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
188 dt = dt + timedelta(seconds=60)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
189 plus1min_ts = int((dt - base_datetime).total_seconds())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
190
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
191 # one that expired a minute ago
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
192 dt = dt - timedelta(seconds=120)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
193 expired_ts = int((dt - base_datetime).total_seconds())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
194
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
195 # claims match what cgi/client.py::determine_user
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
196 # is looking for
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
197 claim = {'sub': self.db.getuid(),
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
198 'iss': self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
199 'aud': self.db.config.TRACKER_WEB,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
200 'roles': ['User'],
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
201 'iat': now_ts,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
202 'exp': plus1min_ts}
6314
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
203
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
204 # in version 2.0.0 and newer jwt.encode returns string
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
205 # not bytestring. So we have to skip b2s conversion
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
206
6314
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
207 if version.parse(jwt.__version__) >= version.parse('2.0.0'):
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
208 tostr = lambda x: x
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
209 else:
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
210 tostr = b2s
a2fbd3592322 pyjwt 2.00 changed return type of jwt.encode from byte to str
John Rouillard <rouilj@ieee.org>
parents: 6312
diff changeset
211
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
212 self.jwt = {}
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
213 self.claim = {}
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
214 # generate invalid claim with expired timestamp
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
215 self.claim['expired'] = copy(claim)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
216 self.claim['expired']['exp'] = expired_ts
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
217 self.jwt['expired'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
218 self.claim['expired'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
219 algorithm='HS256'))
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
220
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
221 # generate valid claim with user role
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
222 self.claim['user'] = copy(claim)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
223 self.claim['user']['exp'] = plus1min_ts
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
224 self.jwt['user'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
225 self.claim['user'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
226 algorithm='HS256'))
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
227
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
228 # generate valid claim with user role and new secret
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
229 self.claim['user_new_secret'] = copy(claim)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
230 self.claim['user_new_secret']['exp'] = plus1min_ts
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
231 self.jwt['user_new_secret'] = tostr(jwt.encode(
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
232 self.claim['user'], self.new_secret,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
233 algorithm='HS256'))
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
234
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
235 # generate invalid claim bad issuer
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
236 self.claim['badiss'] = copy(claim)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
237 self.claim['badiss']['iss'] = "http://someissuer/bugs"
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
238 self.jwt['badiss'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
239 self.claim['badiss'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
240 algorithm='HS256'))
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
241 # generate invalid claim bad aud(ience)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
242 self.claim['badaud'] = copy(claim)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
243 self.claim['badaud']['aud'] = "http://someaudience/bugs"
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
244 self.jwt['badaud'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
245 self.claim['badaud'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
246 algorithm='HS256'))
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
247 # generate invalid claim bad sub(ject)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
248 self.claim['badsub'] = copy(claim)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
249 self.claim['badsub']['sub'] = str("99")
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
250 self.jwt['badsub'] = tostr(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
251 jwt.encode(self.claim['badsub'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
252 algorithm='HS256'))
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
253 # generate invalid claim bad roles
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
254 self.claim['badroles'] = copy(claim)
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
255 self.claim['badroles']['roles'] = ["badrole1", "badrole2"]
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
256 self.jwt['badroles'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
257 self.claim['badroles'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
258 algorithm='HS256'))
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
259 # generate valid claim with limited user:email role
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
260 self.claim['user:email'] = copy(claim)
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
261 self.claim['user:email']['roles'] = ["user:email"]
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
262 self.jwt['user:email'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
263 self.claim['user:email'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
264 algorithm='HS256'))
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
265
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
266 # generate valid claim with limited user:emailnorest role
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
267 self.claim['user:emailnorest'] = copy(claim)
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
268 self.claim['user:emailnorest']['roles'] = ["user:emailnorest"]
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
269 self.jwt['user:emailnorest'] = tostr(jwt.encode(
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
270 self.claim['user:emailnorest'], self.old_secret,
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
271 algorithm='HS256'))
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
272
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
273 self.db.tx_Source = 'web'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
274
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
275 self.db.issue.addprop(tx_Source=hyperdb.String())
5690
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
276 self.db.issue.addprop(anint=hyperdb.Integer())
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
277 self.db.issue.addprop(afloat=hyperdb.Number())
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
278 self.db.issue.addprop(abool=hyperdb.Boolean())
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
279 self.db.issue.addprop(requireme=hyperdb.String(required=True))
6256
29c6dc8ed004 Test handling of unset transitive link field.
John Rouillard <rouilj@ieee.org>
parents: 6185
diff changeset
280 self.db.user.addprop(issue=hyperdb.Link('issue'))
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
281 self.db.msg.addprop(tx_Source=hyperdb.String())
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
282
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
283 self.db.post_init()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
284
6361
58817c3bf471 Fix roundup/test
Ralf Schlatterbeck <rsc@runtux.com>
parents: 6350
diff changeset
285 tx_Source_init(self.db)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
286
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
287 self.client_env = {
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
288 'PATH_INFO': 'http://localhost/rounduptest/rest/',
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
289 'HTTP_HOST': 'localhost',
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
290 'TRACKER_NAME': 'rounduptest',
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
291 'HTTP_ORIGIN': 'http://tracker.example'
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
292 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
293 self.dummy_client = client.Client(self.instance, MockNull(),
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
294 self.client_env, [], None)
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
295 self.dummy_client.request.headers.get = self.get_header
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
296 self.dummy_client.db = self.db
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
297
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
298 self.empty_form = cgi.FieldStorage()
7183
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
299 # under python2 invoking:
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
300 # python2 -m pytest --durations=20
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
301 # loads the form with:
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
302 # FieldStorage(None, None, [MiniFieldStorage('--durations', '2')])
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
303 # Invoking it as: python2 -m pytest -v --durations=20
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
304 # results in an empty list. In any case, force it to be empty.
2de72f75f2f8 Production PBKDF rounds back to 2M, test 1k; fix empty_form (python2)
John Rouillard <rouilj@ieee.org>
parents: 7156
diff changeset
305 self.empty_form.list = []
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
306 self.terse_form = cgi.FieldStorage()
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
307 self.terse_form.list = [
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
308 cgi.MiniFieldStorage('@verbose', '0'),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
309 ]
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
310
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
311 self.server = RestfulInstance(self.dummy_client, self.db)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
312
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
313 self.db.Otk = self.db.getOTKManager()
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
314
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
315 self.db.config['WEB_SECRET_KEY'] = "XyzzykrnKm45Sd"
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
316
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
317 def tearDown(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
318 self.db.close()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
319 try:
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
320 shutil.rmtree(self.dirname)
5602
c40d04915e23 Python3 fixes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5601
diff changeset
321 except OSError as error:
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
322 if error.errno not in (errno.ENOENT, errno.ESRCH):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
323 raise
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
324
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
325 def get_header(self, header, not_found=None):
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
326 try:
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
327 return self.headers[header.lower()]
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
328 except (AttributeError, KeyError, TypeError):
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
329 if header.upper() in self.client_env:
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
330 return self.client_env[header.upper()]
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
331 return not_found
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
332
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
333 def create_stati(self):
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
334 try:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
335 self.db.status.create(name='open', order='9')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
336 except ValueError:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
337 pass
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
338 try:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
339 self.db.status.create(name='closed', order='91')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
340 except ValueError:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
341 pass
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
342 try:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
343 self.db.priority.create(name='normal')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
344 except ValueError:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
345 pass
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
346 try:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
347 self.db.priority.create(name='critical')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
348 except ValueError:
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
349 pass
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
350
7854
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
351 def create_sampledata(self, data_max=3):
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
352 """ Create sample data common to some test cases
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
353 """
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
354 self.create_stati()
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
355 self.db.issue.create(
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
356 title='foo1',
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
357 status=self.db.status.lookup('open'),
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
358 priority=self.db.priority.lookup('normal'),
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
359 nosy=["1", "2"]
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
360 )
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
361 issue_open_norm = self.db.issue.create(
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
362 title='foo2',
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
363 status=self.db.status.lookup('open'),
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
364 priority=self.db.priority.lookup('normal'),
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
365 assignedto="3"
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
366 )
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
367 issue_open_crit = self.db.issue.create(
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
368 title='foo5',
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
369 status=self.db.status.lookup('open'),
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
370 priority=self.db.priority.lookup('critical')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
371 )
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
372
7854
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
373 if data_max > 10:
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
374 raise ValueError('data_max must be less than 10')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
375
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
376 if data_max == 3:
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
377 return
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
378
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
379 sample_data = [
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
380 ["foo6", "normal", "closed"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
381 ["foo7", "critical", "open"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
382 ["foo8", "normal", "open"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
383 ["foo9", "critical", "open"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
384 ["foo10", "normal", "closed"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
385 ["foo11", "critical", "open"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
386 ["foo12", "normal", "closed"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
387 ["foo13", "normal", "open"],
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
388
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
389 ]
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
390
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
391 for title, priority, status in sample_data:
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
392 new_issue = self.db.issue.create(
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
393 title=title,
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
394 status=self.db.status.lookup(status),
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
395 priority=self.db.priority.lookup(priority)
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
396 )
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
397
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
398 if int(new_issue) == data_max:
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
399 break
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
400
7853
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
401 def test_no_next_link_on_full_last_page(self):
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
402 """Make sure that there is no next link
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
403 on the last page where the total number of entries
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
404 is a multiple of the page size.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
405 """
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
406
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
407 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
408
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
409 # Retrieve third user of the total of 3.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
410 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
411 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
412 cgi.MiniFieldStorage('@page_index', '3'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
413 cgi.MiniFieldStorage('@page_size', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
414 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
415 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
416 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
417 self.assertEqual(len(results['data']['collection']), 1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
418 self.assertEqual(results['data']['collection'][0]['id'], "3")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
419 self.assertEqual(results['data']['@total_size'], 3)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
420 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
421 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
422 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
423 "3"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
424 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
425 self.assertNotIn('next', results['data']['@links'])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
426 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
427
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
428 # Retrieve first user of the total of 3.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
429 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
430 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
431 cgi.MiniFieldStorage('@page_index', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
432 cgi.MiniFieldStorage('@page_size', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
433 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
434 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
435 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
436 self.assertEqual(len(results['data']['collection']), 1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
437 self.assertEqual(results['data']['collection'][0]['id'], "1")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
438 self.assertEqual(results['data']['@total_size'], 3)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
439 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
440 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
441 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
442 "3"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
443 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
444 self.assertIn('next', results['data']['@links'])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
445 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
446
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
447 def testTotal_size(self):
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
448 """Make sure that total_size is properly set if @page_size
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
449 is specified.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
450
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
451 Also test for the cases:
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
452
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
453 @page_size >= the max number of retreivable rows.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
454 raises UsageError (and error code 400)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
455 @page_size < max retreivable rows, but
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
456 the amount of matching rows is > max retreivable rows.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
457 total_size/X-Count-Total should be -1
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
458
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
459 no @page_size and limit < total results returns
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
460 limit size and -1 for total.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
461
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
462 Check:
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
463 http response code
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
464 length of collection
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
465 An expected id at end of collection
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
466 @total_size in payload
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
467 X-Count-Total in http headers
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
468
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
469 """
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
470 from roundup.rest import RestfulInstance
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
471
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
472 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
473
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
474 # Retrieve one user of the total of 3. limit 10M+1
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
475 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
476 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
477 cgi.MiniFieldStorage('@page_index', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
478 cgi.MiniFieldStorage('@page_size', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
479 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
480 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
481 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
482 self.assertEqual(len(results['data']['collection']), 1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
483 self.assertEqual(results['data']['collection'][0]['id'], "1")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
484 self.assertEqual(results['data']['@total_size'], 3)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
485 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
486 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
487 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
488 "3"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
489 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
490 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
491
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
492 # set max number of returned rows
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
493 self.stored_max = RestfulInstance.max_response_row_size
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
494 RestfulInstance.max_response_row_size = 2
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
495
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
496 # Retrieve whole class (no @page_*) with max rows restricted.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
497 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
498 results = self.server.get_collection('user', self.empty_form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
499 # reset so changes don't affect other tests if any assetion fails.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
500 RestfulInstance.max_response_row_size = self.stored_max
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
501 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
502 self.assertEqual(len(results['data']['collection']), 2)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
503 self.assertEqual(results['data']['collection'][1]['id'], "2")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
504 self.assertEqual(results['data']['@total_size'], -1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
505 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
506 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
507 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
508 "-1"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
509 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
510 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
511
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
512 # Make sure we can access items that are returned
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
513 # in rows RestfulInstance.max_response_row_size + 1.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
514 # so can we access item 2
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
515 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
516 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
517 cgi.MiniFieldStorage('@page_index', '2'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
518 cgi.MiniFieldStorage('@page_size', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
519 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
520 RestfulInstance.max_response_row_size = 2
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
521 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
522 RestfulInstance.max_response_row_size = self.stored_max
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
523 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
524 self.assertEqual(len(results['data']['collection']), 1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
525 self.assertEqual(results['data']['collection'][0]['id'], "2")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
526 self.assertEqual(results['data']['@total_size'], -1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
527 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
528 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
529 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
530 "-1"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
531 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
532 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
533
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
534 # Same as above, but access item 3
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
535 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
536 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
537 cgi.MiniFieldStorage('@page_index', '3'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
538 cgi.MiniFieldStorage('@page_size', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
539 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
540 RestfulInstance.max_response_row_size = 2
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
541 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
542 RestfulInstance.max_response_row_size = self.stored_max
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
543 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
544 self.assertEqual(len(results['data']['collection']), 1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
545 self.assertEqual(results['data']['collection'][0]['id'], "3")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
546 self.assertEqual(results['data']['@total_size'], 3)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
547 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
548 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
549 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
550 "3"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
551 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
552 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
553
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
554 # Retrieve one user but max number of rows is set to 2,
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
555 # and we retrieve two users from the db.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
556 # So we don't know how many total users there are.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
557
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
558 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
559 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
560 cgi.MiniFieldStorage('@page_index', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
561 cgi.MiniFieldStorage('@page_size', '1'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
562 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
563 RestfulInstance.max_response_row_size = 2
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
564 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
565 RestfulInstance.max_response_row_size = self.stored_max
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
566 self.assertEqual(self.dummy_client.response_code, 200)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
567 self.assertEqual(len(results['data']['collection']), 1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
568 self.assertEqual(results['data']['collection'][0]['id'], "1")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
569 self.assertEqual(results['data']['@total_size'], -1)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
570 print(self.dummy_client.additional_headers["X-Count-Total"])
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
571 self.assertEqual(
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
572 self.dummy_client.additional_headers["X-Count-Total"],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
573 "-1"
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
574 )
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
575 self.dummy_client.additional_headers.clear()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
576
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
577
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
578 # Set the page size to be >= the max number of rows returned.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
579 # and verify the exception returned.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
580 form = cgi.FieldStorage()
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
581 form.list = [
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
582 cgi.MiniFieldStorage('@page_index', '2'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
583 cgi.MiniFieldStorage('@page_size', '2'),
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
584 ]
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
585 RestfulInstance.max_response_row_size = 2
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
586 results = self.server.get_collection('user', form)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
587 RestfulInstance.max_response_row_size = self.stored_max
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
588 self.assertEqual(self.dummy_client.response_code, 400)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
589 self.assertEqual(results['error']['status'], 400)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
590 self.assertTrue(isinstance(results['error']['msg'], UsageError))
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
591 self.assertEqual(results['error']['msg'].args[0],
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
592 "Page size 2 must be less than "
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
593 "admin limit on query result size: 2.")
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
594 self.assertTrue('@total_size' not in results)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
595 self.assertTrue('@data' not in results)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
596 self.assertTrue("X-Count-Total" not in
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
597 self.dummy_client.additional_headers)
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
598
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
599 # reset environment just in case I forgot a reset above.
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
600 RestfulInstance.max_response_row_size = self.stored_max
03c1b7ae3a68 issue2551328/issue2551264 unneeded next link and total_count incorrect
John Rouillard <rouilj@ieee.org>
parents: 7809
diff changeset
601
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
602 def testGet(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
603 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
604 Retrieve all three users
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
605 obtain data for 'joe'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
606 """
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
607 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
608 # Retrieve all three users.
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
609 results = self.server.get_collection('user', self.empty_form)
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
610 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
611 self.assertEqual(len(results['data']['collection']), 3)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
612 self.assertEqual(results['data']['@total_size'], 3)
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
613 print(self.dummy_client.additional_headers["X-Count-Total"])
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
614 self.assertEqual(
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
615 self.dummy_client.additional_headers["X-Count-Total"],
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
616 "3"
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
617 )
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
618
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
619 # Obtain data for 'joe'.
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
620 results = self.server.get_element('user', self.joeid, self.empty_form)
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
621 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
622 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
623 self.assertEqual(results['attributes']['username'], 'joe')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
624 self.assertEqual(results['attributes']['realname'], 'Joe Random')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
625
5678
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
626 # Obtain data for 'joe' via username lookup.
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
627 results = self.server.get_element('user', 'joe', self.empty_form)
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
628 results = results['data']
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
629 self.assertEqual(self.dummy_client.response_code, 200)
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
630 self.assertEqual(results['attributes']['username'], 'joe')
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
631 self.assertEqual(results['attributes']['realname'], 'Joe Random')
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
632
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
633 # Obtain data for 'joe' via username lookup (long form).
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
634 key = 'username=joe'
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
635 results = self.server.get_element('user', key, self.empty_form)
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
636 results = results['data']
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
637 self.assertEqual(self.dummy_client.response_code, 200)
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
638 self.assertEqual(results['attributes']['username'], 'joe')
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
639 self.assertEqual(results['attributes']['realname'], 'Joe Random')
b8e8b1b3ec77 REST: Add key lookup
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5674
diff changeset
640
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
641 # Obtain data for 'joe'.
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
642 results = self.server.get_attribute(
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
643 'user', self.joeid, 'username', self.empty_form
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
644 )
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
645 self.assertEqual(self.dummy_client.response_code, 200)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
646 self.assertEqual(results['data']['data'], 'joe')
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
647
5872
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
648 def testGetTransitive(self):
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
649 """
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
650 Retrieve all issues with an 'o' in status
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
651 sort by status.name (not order)
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
652 """
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
653 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/'
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
654 # self.maxDiff=None
5872
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
655 self.create_sampledata()
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
656 self.db.issue.set('2', status=self.db.status.lookup('closed'))
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
657 self.db.issue.set('3', status=self.db.status.lookup('chatting'))
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
658 expected = {'data':
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
659 {'@total_size': 2,
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
660 'collection': [
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
661 {'id': '2',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
662 'link': base_path + 'issue/2',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
663 'assignedto.issue': None,
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
664 'status':
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
665 {'id': '10',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
666 'link': base_path + 'status/10'
5872
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
667 }
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
668 },
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
669 {'id': '1',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
670 'link': base_path + 'issue/1',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
671 'assignedto.issue': None,
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
672 'status':
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
673 {'id': '9',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
674 'link': base_path + 'status/9'
5872
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
675 }
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
676 },
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
677 ]}
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
678 }
5872
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
679 form = cgi.FieldStorage()
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
680 form.list = [
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
681 cgi.MiniFieldStorage('status.name', 'o'),
6256
29c6dc8ed004 Test handling of unset transitive link field.
John Rouillard <rouilj@ieee.org>
parents: 6185
diff changeset
682 cgi.MiniFieldStorage('@fields', 'status,assignedto.issue'),
5872
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
683 cgi.MiniFieldStorage('@sort', 'status.name'),
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
684 ]
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
685 results = self.server.get_collection('issue', form)
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
686 self.assertDictEqual(expected, results)
1b91e3df3fd0 Implement transitive props for sort and filter
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5865
diff changeset
687
6554
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
688 def testGetBadTransitive(self):
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
689 """
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
690 Mess up the names of various properties and make sure we get a 400
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
691 and a somewhat useful error message.
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
692 """
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
693 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/'
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
694 # self.maxDiff=None
6554
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
695 self.create_sampledata()
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
696 self.db.issue.set('2', status=self.db.status.lookup('closed'))
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
697 self.db.issue.set('3', status=self.db.status.lookup('chatting'))
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
698 expected = [
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
699 {'error': {'msg': KeyError('Unknown property: assignedto.isse',),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
700 'status': 400}},
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
701 {'error': {'msg': KeyError('Unknown property: stat',),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
702 'status': 400}},
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
703 {'error': {'msg': KeyError('Unknown property: status.nam',),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
704 'status': 400}},
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
705 ]
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
706
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
707 ## test invalid transitive property in @fields
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
708 form = cgi.FieldStorage()
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
709 form.list = [
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
710 cgi.MiniFieldStorage('status.name', 'o'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
711 cgi.MiniFieldStorage('@fields', 'status,assignedto.isse'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
712 cgi.MiniFieldStorage('@sort', 'status.name'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
713 ]
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
714 results = self.server.get_collection('issue', form)
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
715 self.assertEqual(self.dummy_client.response_code, 400)
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
716 self.assertEqual(repr(expected[0]['error']['msg']),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
717 repr(results['error']['msg']))
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
718 self.assertEqual(expected[0]['error']['status'],
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
719 results['error']['status'])
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
720
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
721 ## test invalid property in @fields
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
722 form = cgi.FieldStorage()
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
723 form.list = [
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
724 cgi.MiniFieldStorage('status.name', 'o'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
725 cgi.MiniFieldStorage('@fields', 'stat,assignedto.isuse'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
726 cgi.MiniFieldStorage('@sort', 'status.name'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
727 ]
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
728 results = self.server.get_collection('issue', form)
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
729 self.assertEqual(self.dummy_client.response_code, 400)
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
730 self.assertEqual(repr(expected[1]['error']['msg']),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
731 repr(results['error']['msg']))
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
732 self.assertEqual(expected[1]['error']['status'],
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
733 results['error']['status'])
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
734
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
735 ## test invalid transitive property in filter TODO
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
736 form = cgi.FieldStorage()
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
737 form.list = [
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
738 cgi.MiniFieldStorage('status.nam', 'o'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
739 cgi.MiniFieldStorage('@fields', 'status,assignedto.isuse'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
740 cgi.MiniFieldStorage('@sort', 'status.name'),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
741 ]
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
742 results = self.server.get_collection('issue', form)
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
743 # is currently 403 not 400
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
744 self.assertEqual(self.dummy_client.response_code, 400)
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
745 self.assertEqual(repr(expected[2]['error']['msg']),
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
746 repr(results['error']['msg']))
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
747 self.assertEqual(expected[2]['error']['status'],
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
748 results['error']['status'])
576d630fc908 Fix error status for invalid props
John Rouillard <rouilj@ieee.org>
parents: 6539
diff changeset
749
5874
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
750 def testGetExactMatch(self):
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
751 """ Retrieve all issues with an exact title
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
752 """
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
753 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/'
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
754 # self.maxDiff=None
5874
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
755 self.create_sampledata()
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
756 self.db.issue.set('2', title='This is an exact match')
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
757 self.db.issue.set('3', title='This is an exact match')
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
758 self.db.issue.set('1', title='This is AN exact match')
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
759 expected = {'data':
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
760 {'@total_size': 2,
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
761 'collection': [
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
762 {'id': '2',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
763 'link': base_path + 'issue/2',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
764 },
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
765 {'id': '3',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
766 'link': base_path + 'issue/3',
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
767 },
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
768 ]}
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
769 }
5874
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
770 form = cgi.FieldStorage()
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
771 form.list = [
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
772 cgi.MiniFieldStorage('title:', 'This is an exact match'),
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
773 cgi.MiniFieldStorage('@sort', 'status.name'),
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
774 ]
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
775 results = self.server.get_collection('issue', form)
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
776 self.assertDictEqual(expected, results)
6630baff5f68 Implement exact string search in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5872
diff changeset
777
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
778 def testOutputFormat(self):
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
779 """ test of @fields and @verbose implementation """
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
780
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
781 self.maxDiff = 4000
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
782 self.create_sampledata()
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
783 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/'
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
784
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
785 # Check formating for issues status=open; @fields and verbose tests
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
786 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
787 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
788 cgi.MiniFieldStorage('status', 'open'),
5825
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
789 cgi.MiniFieldStorage('@fields', 'nosy,status,creator'),
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
790 cgi.MiniFieldStorage('@verbose', '2')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
791 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
792
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
793 expected = {'data':
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
794 {'@total_size': 3,
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
795 'collection': [ {
5825
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
796 'creator': {'id': '3',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
797 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
798 'username': 'joe'},
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
799 'status': {'id': '9',
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
800 'name': 'open',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
801 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
802 'id': '1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
803 'nosy': [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
804 {'username': 'admin',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
805 'id': '1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
806 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/1'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
807 {'username': 'anonymous',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
808 'id': '2',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
809 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2'}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
810 ],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
811 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
812 'title': 'foo1' },
5825
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
813 { 'creator': {'id': '3',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
814 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
815 'username': 'joe'},
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
816 'status': {
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
817 'id': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
818 'name': 'open',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
819 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9' },
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
820 'id': '2',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
821 'nosy': [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
822 {'username': 'joe',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
823 'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
824 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3'}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
825 ],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
826 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/2',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
827 'title': 'foo2'},
5825
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
828 {'creator': {'id': '3',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
829 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
830 'username': 'joe'},
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
831 'status': {
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
832 'id': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
833 'name': 'open',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
834 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
835 'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
836 'nosy': [],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
837 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
838 'title': 'foo5'}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
839 ]}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
840
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
841 results = self.server.get_collection('issue', form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
842 self.assertDictEqual(expected, results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
843
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
844 # Check formating for issues status=open; @fields and verbose tests
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
845 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
846 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
847 cgi.MiniFieldStorage('status', 'open')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
848 # default cgi.MiniFieldStorage('@verbose', '1')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
849 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
850
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
851 expected={'data':
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
852 {'@total_size': 3,
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
853 'collection': [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
854 {'id': '1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
855 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1',},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
856 { 'id': '2',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
857
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
858 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/2'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
859 {'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
860 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3'} ]}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
861
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
862
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
863 results = self.server.get_collection('issue', form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
864 self.assertDictEqual(expected, results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
865
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
866 # Generate failure case, unknown field.
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
867 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
868 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
869 cgi.MiniFieldStorage('status', 'open'),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
870 cgi.MiniFieldStorage('@fields', 'title,foo')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
871 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
872
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
873 expected={'error': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
874 'msg': UsageError("Failed to find property 'foo' "
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
875 "for class issue.",),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
876 'status': 400}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
877
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
878 results = self.server.get_collection('issue', form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
879 # I tried assertDictEqual but seems it can't handle
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
880 # the exception value of 'msg'. So I am using repr to check.
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
881 self.assertEqual(repr(sorted(expected['error'])),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
882 repr(sorted(results['error']))
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
883 )
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
884
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
885 # Check formating for issues status=open; @fields and verbose tests
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
886 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
887 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
888 cgi.MiniFieldStorage('status', 'open'),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
889 cgi.MiniFieldStorage('@fields', 'nosy,status,assignedto'),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
890 cgi.MiniFieldStorage('@verbose', '0')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
891 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
892
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
893 expected={'data': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
894 '@total_size': 3,
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
895 'collection': [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
896 {'assignedto': None,
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
897 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
898 'status': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
899 'nosy': ['1', '2'],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
900 'id': '1'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
901 {'assignedto': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
902 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/2',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
903 'status': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
904 'nosy': ['3'],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
905 'id': '2'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
906 {'assignedto': None,
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
907 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
908 'status': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
909 'nosy': [],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
910 'id': '3'}]}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
911
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
912 results = self.server.get_collection('issue', form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
913 print(results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
914 self.assertDictEqual(expected, results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
915
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
916 # check users
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
917 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
918 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
919 cgi.MiniFieldStorage('@fields', 'username,queries,password'),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
920 cgi.MiniFieldStorage('@verbose', '0')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
921 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
922 # note this is done as user joe, so we only get queries
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
923 # and password for joe.
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
924 expected = {'data': {'collection': [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
925 {'id': '1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
926 'username': 'admin',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
927 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/1'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
928 {'id': '2',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
929 'username': 'anonymous',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
930 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2'},
8243
8c1e0459b73d test: issue2551253. fix test for default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 8220
diff changeset
931 {'password': '[password hidden scheme PBKDF2S5]',
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
932 'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
933 'queries': [],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
934 'username': 'joe',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
935 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3'}],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
936 '@total_size': 3}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
937
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
938 results = self.server.get_collection('user', form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
939 self.assertDictEqual(expected, results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
940
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
941 ## Start testing get_element
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
942 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
943 form.list = [
5825
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
944 cgi.MiniFieldStorage('@fields', 'queries,password,creator'),
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
945 cgi.MiniFieldStorage('@verbose', '2')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
946 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
947 expected = {'data': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
948 'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
949 'type': 'user',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
950 '@etag': '',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
951 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
952 'attributes': {
5825
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
953 'creator': {'id': '1',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
954 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/1',
bcb894bc9740 Add test of protected values for collections and item.
John Rouillard <rouilj@ieee.org>
parents: 5801
diff changeset
955 'username': 'admin'},
8243
8c1e0459b73d test: issue2551253. fix test for default hash is PBKDF2-SHA512.
John Rouillard <rouilj@ieee.org>
parents: 8220
diff changeset
956 'password': '[password hidden scheme PBKDF2S5]',
5682
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
957 'queries': [],
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
958 'username': 'joe'
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
959 }
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
960 }}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
961
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
962 results = self.server.get_element('user', self.joeid, form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
963 results['data']['@etag'] = '' # etag depends on date, set to empty
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
964 self.assertDictEqual(expected,results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
965
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
966 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
967 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
968 cgi.MiniFieldStorage('@fields', 'status:priority'),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
969 cgi.MiniFieldStorage('@verbose', '1')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
970 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
971 expected = {'data': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
972 'type': 'issue',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
973 'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
974 'attributes': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
975 'status': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
976 'id': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
977 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status/9'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
978 'priority': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
979 'id': '1',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
980 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/priority/1'}},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
981 '@etag': '',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
982 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3'}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
983
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
984 results = self.server.get_element('issue', "3", form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
985 results['data']['@etag'] = '' # etag depends on date, set to empty
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
986 self.assertDictEqual(expected,results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
987
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
988 form = cgi.FieldStorage()
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
989 form.list = [
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
990 cgi.MiniFieldStorage('@fields', 'status,priority'),
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
991 cgi.MiniFieldStorage('@verbose', '0')
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
992 ]
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
993 expected = {'data': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
994 'type': 'issue',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
995 'id': '3',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
996 'attributes': {
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
997 'status': '9',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
998 'priority': '1'},
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
999 '@etag': '',
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
1000 'link': 'http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/3'}}
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
1001
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
1002 results = self.server.get_element('issue', "3", form)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
1003 results['data']['@etag'] = '' # etag depends on date, set to empty
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
1004 self.assertDictEqual(expected,results)
e8ac82b8d074 Fix parse of @fields/@attrs with : as separator. Add tests @verbose and @fields.
John Rouillard <rouilj@ieee.org>
parents: 5678
diff changeset
1005
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1006 def testSorting(self):
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1007 self.maxDiff = 4000
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1008 self.create_sampledata()
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1009 self.db.issue.set('1', status='7')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1010 self.db.issue.set('2', status='2')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1011 self.db.issue.set('3', status='2')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1012 self.db.commit()
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1013 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/'
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1014 # change some data for sorting on later
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1015 form = cgi.FieldStorage()
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1016 form.list = [
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1017 cgi.MiniFieldStorage('@fields', 'status'),
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1018 cgi.MiniFieldStorage('@sort', 'status,-id'),
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1019 cgi.MiniFieldStorage('@verbose', '0')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1020 ]
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1021
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1022 # status is sorted by orderprop (property 'order')
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1023 # which provides the same ordering as the status ID
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1024 expected={'data': {
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1025 '@total_size': 3,
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1026 'collection': [
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1027 {'link': base_path + '3', 'status': '2', 'id': '3'},
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1028 {'link': base_path + '2', 'status': '2', 'id': '2'},
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1029 {'link': base_path + '1', 'status': '7', 'id': '1'}]}}
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1030
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1031 results = self.server.get_collection('issue', form)
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1032 self.assertDictEqual(expected, results)
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1033
7854
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1034 def testGrouping(self):
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1035 self.maxDiff = 4000
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1036 self.create_sampledata(data_max=5)
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1037 self.db.issue.set('1', status='7', priority='4')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1038 self.db.issue.set('2', status='2', priority='4')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1039 self.db.issue.set('3', status='2', priority='4')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1040 self.db.issue.set('4', status='2', priority='2')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1041 self.db.issue.set('5', status='2', priority='2')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1042 self.db.commit()
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1043 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/'
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1044 # change some data for sorting on later
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1045 form = cgi.FieldStorage()
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1046 form.list = [
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1047 cgi.MiniFieldStorage('@fields', 'status,priority'),
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1048 cgi.MiniFieldStorage('@sort', '-id'),
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1049 cgi.MiniFieldStorage('@group', '-status,priority'),
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1050 cgi.MiniFieldStorage('@verbose', '0')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1051 ]
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1052
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1053 # status is sorted by orderprop (property 'order')
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1054 expected={'data': {
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1055 '@total_size': 5,
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1056 'collection': [
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1057 {'link': base_path + '1', 'priority': '4',
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1058 'status': '7', 'id': '1'},
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1059 {'link': base_path + '5', 'priority': '2',
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1060 'status': '2', 'id': '5'},
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1061 {'link': base_path + '4', 'priority': '2',
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1062 'status': '2', 'id': '4'},
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1063 {'link': base_path + '3', 'priority': '4',
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1064 'status': '2', 'id': '3'},
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1065 {'link': base_path + '2', 'priority': '4',
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1066 'status': '2', 'id': '2'},
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1067 ]
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1068 }}
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1069
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1070
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1071 results = self.server.get_collection('issue', form)
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1072 print(results)
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1073 self.assertDictEqual(expected, results)
171ff2e487df Add @group for grouping in rest interface.
John Rouillard <rouilj@ieee.org>
parents: 7853
diff changeset
1074
6090
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1075 def testTransitiveField(self):
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1076 """ Test a transitive property in @fields """
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1077 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/'
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1078 # create sample data
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1079 self.create_stati()
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1080 self.db.issue.create(
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1081 title='foo4',
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1082 status=self.db.status.lookup('closed'),
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1083 priority=self.db.priority.lookup('critical')
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1084 )
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1085 # Retrieve all issue @fields=status.name
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1086 form = cgi.FieldStorage()
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1087 form.list = [
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1088 cgi.MiniFieldStorage('@fields', 'status.name')
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1089 ]
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1090 results = self.server.get_collection('issue', form)
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1091 self.assertEqual(self.dummy_client.response_code, 200)
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1092
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1093 exp = [
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1094 {'link': base_path + 'issue/1', 'id': '1', 'status.name': 'closed'}]
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1095 self.assertEqual(results['data']['collection'], exp)
e097ff5064b8 Allow transitive properties in @fields in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5993
diff changeset
1096
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1097 def testFilter(self):
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1098 """
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1099 Retrieve all three users
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1100 obtain data for 'joe'
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1101 """
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1102 # create sample data
5865
04deafac71ab Implement sorting of collections in REST API
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5842
diff changeset
1103 self.create_stati()
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1104 self.db.issue.create(
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1105 title='foo4',
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1106 status=self.db.status.lookup('closed'),
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1107 priority=self.db.priority.lookup('critical')
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1108 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1109 self.db.issue.create(
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1110 title='foo1',
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1111 status=self.db.status.lookup('open'),
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1112 priority=self.db.priority.lookup('normal')
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1113 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1114 issue_open_norm = self.db.issue.create(
5842
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1115 title='foo2 normal',
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1116 status=self.db.status.lookup('open'),
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1117 priority=self.db.priority.lookup('normal')
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1118 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1119 issue_closed_norm = self.db.issue.create(
5842
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1120 title='foo3 closed normal',
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1121 status=self.db.status.lookup('closed'),
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1122 priority=self.db.priority.lookup('normal')
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1123 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1124 issue_closed_crit = self.db.issue.create(
5842
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1125 title='foo4 closed',
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1126 status=self.db.status.lookup('closed'),
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1127 priority=self.db.priority.lookup('critical')
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1128 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1129 issue_open_crit = self.db.issue.create(
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1130 title='foo5',
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1131 status=self.db.status.lookup('open'),
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1132 priority=self.db.priority.lookup('critical')
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1133 )
5623
1c4adab65faf use config file setting for creating tracker uri
John Rouillard <rouilj@ieee.org>
parents: 5604
diff changeset
1134 base_path = self.db.config['TRACKER_WEB'] + 'rest/data/issue/'
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1135
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1136 # Retrieve all issue status=open
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1137 form = cgi.FieldStorage()
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1138 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1139 cgi.MiniFieldStorage('status', 'open')
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1140 ]
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1141 results = self.server.get_collection('issue', form)
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1142 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1143 self.assertIn(get_obj(base_path, issue_open_norm),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1144 results['data']['collection'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1145 self.assertIn(get_obj(base_path, issue_open_crit),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1146 results['data']['collection'])
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1147 self.assertNotIn(
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1148 get_obj(base_path, issue_closed_norm),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1149 results['data']['collection']
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1150 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1151
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1152 # Retrieve all issue status=closed and priority=critical
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1153 form = cgi.FieldStorage()
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1154 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1155 cgi.MiniFieldStorage('status', 'closed'),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1156 cgi.MiniFieldStorage('priority', 'critical')
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1157 ]
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1158 results = self.server.get_collection('issue', form)
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1159 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1160 self.assertIn(get_obj(base_path, issue_closed_crit),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1161 results['data']['collection'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1162 self.assertNotIn(get_obj(base_path, issue_open_crit),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1163 results['data']['collection'])
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1164 self.assertNotIn(
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1165 get_obj(base_path, issue_closed_norm),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1166 results['data']['collection']
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1167 )
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1168
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1169 # Retrieve all issue status=closed and priority=normal,critical
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1170 form = cgi.FieldStorage()
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1171 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1172 cgi.MiniFieldStorage('status', 'closed'),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1173 cgi.MiniFieldStorage('priority', 'normal,critical')
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1174 ]
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1175 results = self.server.get_collection('issue', form)
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1176 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1177 self.assertIn(get_obj(base_path, issue_closed_crit),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1178 results['data']['collection'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1179 self.assertIn(get_obj(base_path, issue_closed_norm),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1180 results['data']['collection'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1181 self.assertNotIn(get_obj(base_path, issue_open_crit),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1182 results['data']['collection'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1183 self.assertNotIn(get_obj(base_path, issue_open_norm),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1184 results['data']['collection'])
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1185
5842
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1186 # Retrieve all issue status=closed and priority=normal,critical
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1187 # using duplicate priority key's.
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1188 form = cgi.FieldStorage()
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1189 form.list = [
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1190 cgi.MiniFieldStorage('status', 'closed'),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1191 cgi.MiniFieldStorage('priority', 'normal'),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1192 cgi.MiniFieldStorage('priority', 'critical')
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1193 ]
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1194 results = self.server.get_collection('issue', form)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1195 self.assertEqual(self.dummy_client.response_code, 200)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1196 self.assertIn(get_obj(base_path, issue_closed_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1197 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1198 self.assertIn(get_obj(base_path, issue_closed_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1199 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1200 self.assertNotIn(get_obj(base_path, issue_open_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1201 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1202 self.assertNotIn(get_obj(base_path, issue_open_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1203 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1204
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1205 # Retrieve all issues with title containing
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1206 # closed, normal and 3 using duplicate title filterkeys
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1207 form = cgi.FieldStorage()
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1208 form.list = [
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1209 cgi.MiniFieldStorage('title', 'closed'),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1210 cgi.MiniFieldStorage('title', 'normal'),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1211 cgi.MiniFieldStorage('title', '3')
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1212 ]
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1213 results = self.server.get_collection('issue', form)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1214 self.assertEqual(self.dummy_client.response_code, 200)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1215 self.assertNotIn(get_obj(base_path, issue_closed_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1216 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1217 self.assertIn(get_obj(base_path, issue_closed_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1218 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1219 self.assertNotIn(get_obj(base_path, issue_open_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1220 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1221 self.assertNotIn(get_obj(base_path, issue_open_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1222 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1223 self.assertEqual(len(results['data']['collection']), 1)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1224
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1225 # Retrieve all issues (no hits) with title containing
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1226 # closed, normal and foo3 in this order using title filter
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1227 form = cgi.FieldStorage()
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1228 form.list = [
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1229 cgi.MiniFieldStorage('title', 'closed normal foo3')
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1230 ]
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1231 results = self.server.get_collection('issue', form)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1232 self.assertEqual(self.dummy_client.response_code, 200)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1233 self.assertNotIn(get_obj(base_path, issue_closed_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1234 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1235 self.assertNotIn(get_obj(base_path, issue_closed_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1236 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1237 self.assertNotIn(get_obj(base_path, issue_open_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1238 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1239 self.assertNotIn(get_obj(base_path, issue_open_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1240 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1241 self.assertEqual(len(results['data']['collection']), 0)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1242
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1243 # Retrieve all issues with title containing
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1244 # foo3, closed and normal in this order using title filter
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1245 form = cgi.FieldStorage()
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1246 form.list = [
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1247 cgi.MiniFieldStorage('title', 'foo3 closed normal')
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1248 ]
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1249 results = self.server.get_collection('issue', form)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1250 self.assertEqual(self.dummy_client.response_code, 200)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1251 self.assertNotIn(get_obj(base_path, issue_closed_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1252 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1253 self.assertIn(get_obj(base_path, issue_closed_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1254 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1255 self.assertNotIn(get_obj(base_path, issue_open_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1256 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1257 self.assertNotIn(get_obj(base_path, issue_open_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1258 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1259 self.assertEqual(len(results['data']['collection']), 1)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1260
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1261 # Retrieve all issues with word closed in title
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1262 form = cgi.FieldStorage()
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1263 form.list = [
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1264 cgi.MiniFieldStorage('title', 'closed'),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1265 ]
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1266 results = self.server.get_collection('issue', form)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1267 self.assertEqual(self.dummy_client.response_code, 200)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1268 self.assertIn(get_obj(base_path, issue_closed_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1269 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1270 self.assertIn(get_obj(base_path, issue_closed_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1271 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1272 self.assertNotIn(get_obj(base_path, issue_open_crit),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1273 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1274 self.assertNotIn(get_obj(base_path, issue_open_norm),
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1275 results['data']['collection'])
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1276 self.assertEqual(len(results['data']['collection']), 2)
9c6617857032 Support use of duplicate rest filters keys. So URL's like:
John Rouillard <rouilj@ieee.org>
parents: 5825
diff changeset
1277
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1278 def testPagination(self):
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1279 """
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1280 Test pagination. page_size is required and is an integer
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1281 starting at 1. page_index is optional and is an integer
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1282 starting at 1. Verify that pagination links are present
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1283 if paging, @total_size and X-Count-Total header match
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1284 number of items.
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1285 """
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1286 # create sample data
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1287 for i in range(0, random.randint(8,15)):
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1288 self.db.issue.create(title='foo' + str(i))
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1289
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1290 # Retrieving all the issues
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1291 results = self.server.get_collection('issue', self.empty_form)
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1292 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1293 total_length = len(results['data']['collection'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1294 # Verify no pagination links if paging not used
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1295 self.assertFalse('@links' in results['data'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1296 self.assertEqual(results['data']['@total_size'], total_length)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1297 self.assertEqual(
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1298 self.dummy_client.additional_headers["X-Count-Total"],
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1299 str(total_length)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1300 )
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1301
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1302
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1303 # Pagination will be 45% of the total result
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1304 # So 2 full pages and 1 partial page.
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1305 page_size = total_length * 45 // 100
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1306 page_one_expected = page_size
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1307 page_two_expected = page_size
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1308 page_three_expected = total_length - (2*page_one_expected)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1309 base_url="http://tracker.example/cgi-bin/roundup.cgi/" \
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1310 "bugs/rest/data/issue"
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1311
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1312 # Retrieve page 1
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1313 form = cgi.FieldStorage()
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1314 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1315 cgi.MiniFieldStorage('@page_size', page_size),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1316 cgi.MiniFieldStorage('@page_index', 1)
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1317 ]
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1318 results = self.server.get_collection('issue', form)
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1319 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1320 self.assertEqual(len(results['data']['collection']),
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1321 page_one_expected)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1322 self.assertTrue('@links' in results['data'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1323 self.assertTrue('self' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1324 self.assertTrue('next' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1325 self.assertFalse('prev' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1326 self.assertEqual(results['data']['@links']['self'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1327 "%s?@page_index=1&@page_size=%s"%(base_url,page_size))
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1328 self.assertEqual(results['data']['@links']['next'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1329 "%s?@page_index=2&@page_size=%s"%(base_url,page_size))
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1330
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1331 page_one_results = results # save this for later
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1332
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1333 # Retrieve page 2
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1334 form = cgi.FieldStorage()
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1335 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1336 cgi.MiniFieldStorage('@page_size', page_size),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1337 cgi.MiniFieldStorage('@page_index', 2)
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1338 ]
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1339 results = self.server.get_collection('issue', form)
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1340 self.assertEqual(self.dummy_client.response_code, 200)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1341 self.assertEqual(len(results['data']['collection']), page_two_expected)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1342 self.assertTrue('@links' in results['data'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1343 self.assertTrue('self' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1344 self.assertTrue('next' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1345 self.assertTrue('prev' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1346 self.assertEqual(results['data']['@links']['self'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1347 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue?@page_index=2&@page_size=%s"%page_size)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1348 self.assertEqual(results['data']['@links']['next'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1349 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue?@page_index=3&@page_size=%s"%page_size)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1350 self.assertEqual(results['data']['@links']['prev'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1351 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue?@page_index=1&@page_size=%s"%page_size)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1352 self.assertEqual(results['data']['@links']['self'][0]['rel'],
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1353 'self')
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1354 self.assertEqual(results['data']['@links']['next'][0]['rel'],
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1355 'next')
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1356 self.assertEqual(results['data']['@links']['prev'][0]['rel'],
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1357 'prev')
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
1358
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1359 # Retrieve page 3
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1360 form = cgi.FieldStorage()
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1361 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1362 cgi.MiniFieldStorage('@page_size', page_size),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1363 cgi.MiniFieldStorage('@page_index', 3)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1364 ]
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1365 results = self.server.get_collection('issue', form)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1366 self.assertEqual(self.dummy_client.response_code, 200)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1367 self.assertEqual(len(results['data']['collection']), page_three_expected)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1368 self.assertTrue('@links' in results['data'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1369 self.assertTrue('self' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1370 self.assertFalse('next' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1371 self.assertTrue('prev' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1372 self.assertEqual(results['data']['@links']['self'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1373 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue?@page_index=3&@page_size=%s"%page_size)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1374 self.assertEqual(results['data']['@links']['prev'][0]['uri'],
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1375 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue?@page_index=2&@page_size=%s"%page_size)
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1376
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1377 # Verify that page_index is optional
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1378 # Should start at page 1
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1379 form = cgi.FieldStorage()
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1380 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
1381 cgi.MiniFieldStorage('@page_size', page_size),
5639
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1382 ]
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1383 results = self.server.get_collection('issue', form)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1384 self.assertEqual(self.dummy_client.response_code, 200)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1385 self.assertEqual(len(results['data']['collection']), page_size)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1386 self.assertTrue('@links' in results['data'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1387 self.assertTrue('self' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1388 self.assertTrue('next' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1389 self.assertFalse('prev' in results['data']['@links'])
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1390 self.assertEqual(page_one_results, results)
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1391
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1392 # FIXME add tests for out of range once we decide what response
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1393 # is needed to:
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1394 # page_size < 0
f576957cbb1f Add support for prev/next/self links when returning paginated results.
John Rouillard <rouilj@ieee.org>
parents: 5630
diff changeset
1395 # page_index < 0
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1396
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1397 def testRestRateLimit(self):
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1398
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1399 calls_per_interval = 20
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1400 interval_sec = 60
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1401 wait_time_str = str(int(interval_sec/calls_per_interval))
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1402
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1403 self.db.config['WEB_API_CALLS_PER_INTERVAL'] = calls_per_interval
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1404 self.db.config['WEB_API_INTERVAL_IN_SEC'] = interval_sec
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1405
5733
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
1406 # Otk code never passes through the
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
1407 # retry loop. Not sure why but I can force it
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
1408 # through the loop by setting the internal _db_type
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
1409 # setting once the db is created by the previous command.
5734
5cd9ac3daed7 Fixes for mysql and postgresql primary db.
John Rouillard <rouilj@ieee.org>
parents: 5733
diff changeset
1410 try:
5cd9ac3daed7 Fixes for mysql and postgresql primary db.
John Rouillard <rouilj@ieee.org>
parents: 5733
diff changeset
1411 self.db.Otk._db_type = whichdb("%s/%s"%(self.db.Otk.dir, self.db.Otk.name))
5cd9ac3daed7 Fixes for mysql and postgresql primary db.
John Rouillard <rouilj@ieee.org>
parents: 5733
diff changeset
1412 except AttributeError:
5cd9ac3daed7 Fixes for mysql and postgresql primary db.
John Rouillard <rouilj@ieee.org>
parents: 5733
diff changeset
1413 # if dir attribute doesn't exist the primary db is not
5cd9ac3daed7 Fixes for mysql and postgresql primary db.
John Rouillard <rouilj@ieee.org>
parents: 5733
diff changeset
1414 # sqlite or anydbm. So don't need to exercise code.
5cd9ac3daed7 Fixes for mysql and postgresql primary db.
John Rouillard <rouilj@ieee.org>
parents: 5733
diff changeset
1415 pass
5733
62bdcb874433 Test code was not testing the loop that retries otk database updates
John Rouillard <rouilj@ieee.org>
parents: 5732
diff changeset
1416
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
1417 start_time = utcnow()
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1418 # don't set an accept header; json should be the default
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1419 # use up all our allowed api calls
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1420 for i in range(calls_per_interval):
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1421 # i is 0 ... calls_per_interval
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1422 self.client_error_message = []
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1423 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1424 results = self.server.dispatch('GET',
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1425 "/rest/data/user/%s/realname"%self.joeid,
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1426 self.empty_form)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1427
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
1428 loop_time = utcnow()
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1429 self.assertLess((loop_time-start_time).total_seconds(),
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1430 int(wait_time_str),
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1431 "Test system is too slow to complete test as configured")
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1432
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1433 # is successful
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1434 self.assertEqual(self.server.client.response_code, 200)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1435 # does not have Retry-After header as we have
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1436 # suceeded with this query
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1437 self.assertFalse("Retry-After" in
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1438 self.server.client.additional_headers)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1439 # remaining count is correct
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1440 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1441 self.server.client.additional_headers["X-RateLimit-Remaining"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1442 str(self.db.config['WEB_API_CALLS_PER_INTERVAL'] -1 - i)
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1443 )
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1444
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1445 # trip limit
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1446 self.server.client.additional_headers.clear()
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1447 results = self.server.dispatch('GET',
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1448 "/rest/data/user/%s/realname"%self.joeid,
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1449 self.empty_form)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1450 print(results)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1451 self.assertEqual(self.server.client.response_code, 429)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1452
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1453 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1454 self.server.client.additional_headers["X-RateLimit-Limit"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1455 str(self.db.config['WEB_API_CALLS_PER_INTERVAL']))
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1456 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1457 self.server.client.additional_headers["X-RateLimit-Limit-Period"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1458 str(self.db.config['WEB_API_INTERVAL_IN_SEC']))
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1459 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1460 self.server.client.additional_headers["X-RateLimit-Remaining"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1461 '0')
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1462 # value will be almost 60. Allow 1-2 seconds for all 20 rounds.
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1463 self.assertAlmostEqual(
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1464 float(self.server.client.additional_headers["X-RateLimit-Reset"]),
6660
32c6e98e5a21 Larger delta for X-RateLimit-Reset tests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 6554
diff changeset
1465 59, delta=5)
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1466 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1467 str(self.server.client.additional_headers["Retry-After"]),
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1468 wait_time_str) # check as string
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1469
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1470 print("Reset:", self.server.client.additional_headers["X-RateLimit-Reset"])
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
1471 print("Now realtime pre-sleep:", utcnow())
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1472 # sleep as requested so we can do another login
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1473 sleep(float(wait_time_str) + 0.1)
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
1474 print("Now realtime post-sleep:", utcnow())
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1475
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1476 # this should succeed
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1477 self.server.client.additional_headers.clear()
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1478 results = self.server.dispatch('GET',
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1479 "/rest/data/user/%s/realname"%self.joeid,
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1480 self.empty_form)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1481 print(results)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1482 print("Reset:", self.server.client.additional_headers["X-RateLimit-Reset-date"])
7587
8f29e4ea05ce fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents: 7582
diff changeset
1483 print("Now realtime:", utcnow())
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1484 print("Now ts header:", self.server.client.additional_headers["Now"])
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1485 print("Now date header:", self.server.client.additional_headers["Now-date"])
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1486
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1487 self.assertEqual(self.server.client.response_code, 200)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1488
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1489 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1490 self.server.client.additional_headers["X-RateLimit-Limit"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1491 str(self.db.config['WEB_API_CALLS_PER_INTERVAL']))
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1492 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1493 self.server.client.additional_headers["X-RateLimit-Limit-Period"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1494 str(self.db.config['WEB_API_INTERVAL_IN_SEC']))
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1495 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1496 self.server.client.additional_headers["X-RateLimit-Remaining"],
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1497 '0')
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1498 self.assertFalse("Retry-After" in
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1499 self.server.client.additional_headers)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1500 # we still need to wait a minute for everything to clear
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1501 self.assertAlmostEqual(
5937
5d0873a4de4a fix rate limit headers - were ints/floats need to be strings
John Rouillard <rouilj@ieee.org>
parents: 5879
diff changeset
1502 float(self.server.client.additional_headers["X-RateLimit-Reset"]),
6660
32c6e98e5a21 Larger delta for X-RateLimit-Reset tests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 6554
diff changeset
1503 59, delta=5)
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1504
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1505 # and make sure we need to wait another three seconds
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1506 # as we consumed the last api call
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1507 results = self.server.dispatch('GET',
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1508 "/rest/data/user/%s/realname"%self.joeid,
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1509 self.empty_form)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1510
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1511 self.assertEqual(self.server.client.response_code, 429)
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1512 self.assertEqual(
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1513 str(self.server.client.additional_headers["Retry-After"]),
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1514 wait_time_str) # check as string
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1515
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1516 json_dict = json.loads(b2s(results))
7555
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1517 self.assertEqual(
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1518 json_dict['error']['msg'],
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1519 "Api rate limits exceeded. Please wait: %s seconds." %
451232f83244 test: Modify testRestRateLimit test to report when system is too slow.
John Rouillard <rouilj@ieee.org>
parents: 7389
diff changeset
1520 wait_time_str)
5732
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1521
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1522 # reset rest params
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1523 self.db.config['WEB_API_CALLS_PER_INTERVAL'] = 0
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1524 self.db.config['WEB_API_INTERVAL_IN_SEC'] = 3600
0e6ed3d72f92 Rest rate limiting code first commit. It is a bit rough and turned off
John Rouillard <rouilj@ieee.org>
parents: 5729
diff changeset
1525
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1526 def testEtagGeneration(self):
5668
a4bb88a1a643 A fix for https://issues.roundup-tracker.org/issue2551034
John Rouillard <rouilj@ieee.org>
parents: 5656
diff changeset
1527 ''' Make sure etag generation is stable
a4bb88a1a643 A fix for https://issues.roundup-tracker.org/issue2551034
John Rouillard <rouilj@ieee.org>
parents: 5656
diff changeset
1528
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1529 This mocks date.Date() when creating the target to be
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1530 etagged. Differing dates make this test impossible.
5668
a4bb88a1a643 A fix for https://issues.roundup-tracker.org/issue2551034
John Rouillard <rouilj@ieee.org>
parents: 5656
diff changeset
1531 '''
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1532 from roundup import date
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1533
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1534 originalDate = date.Date
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1535
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1536 dummy=date.Date('2000-06-26.00:34:02.0')
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1537
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1538 # is a closure the best way to return a static Date object??
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1539 def dummyDate(adate=None):
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1540 def dummyClosure(adate=None, translator=None):
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1541 return dummy
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1542 return dummyClosure
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1543
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1544 date.Date = dummyDate()
5728
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1545 try:
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1546 newuser = self.db.user.create(
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1547 username='john',
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1548 password=password.Password('random1', scheme='plaintext'),
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1549 address='random1@home.org',
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1550 realname='JohnRandom',
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1551 roles='User,Admin'
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1552 )
5668
a4bb88a1a643 A fix for https://issues.roundup-tracker.org/issue2551034
John Rouillard <rouilj@ieee.org>
parents: 5656
diff changeset
1553
5728
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1554 # verify etag matches what we calculated in the past
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1555 node = self.db.user.getnode(newuser)
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1556 etag = calculate_etag(node, self.db.config['WEB_SECRET_KEY'])
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1557 items = node.items(protected=True) # include every item
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1558 print(repr(sorted(items)))
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1559 print(etag)
6256
29c6dc8ed004 Test handling of unset transitive link field.
John Rouillard <rouilj@ieee.org>
parents: 6185
diff changeset
1560 self.assertEqual(etag, '"07c3a7f214d394cf46220e294a5a53c8"')
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1561
5728
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1562 # modify key and verify we have a different etag
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1563 etag = calculate_etag(node, self.db.config['WEB_SECRET_KEY'] + "a")
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1564 items = node.items(protected=True) # include every item
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1565 print(repr(sorted(items)))
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1566 print(etag)
6256
29c6dc8ed004 Test handling of unset transitive link field.
John Rouillard <rouilj@ieee.org>
parents: 6185
diff changeset
1567 self.assertNotEqual(etag, '"07c3a7f214d394cf46220e294a5a53c8"')
5668
a4bb88a1a643 A fix for https://issues.roundup-tracker.org/issue2551034
John Rouillard <rouilj@ieee.org>
parents: 5656
diff changeset
1568
5728
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1569 # change data and verify we have a different etag
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1570 node.username="Paul"
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1571 etag = calculate_etag(node, self.db.config['WEB_SECRET_KEY'])
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1572 items = node.items(protected=True) # include every item
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1573 print(repr(sorted(items)))
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1574 print(etag)
6256
29c6dc8ed004 Test handling of unset transitive link field.
John Rouillard <rouilj@ieee.org>
parents: 6185
diff changeset
1575 self.assertEqual(etag, '"d655801d3a6d51e32891531b06ccecfa"')
5728
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1576 finally:
bfd28644fe43 In suggestion from Joseph Myers, put test code in a try: finally: block
John Rouillard <rouilj@ieee.org>
parents: 5727
diff changeset
1577 date.Date = originalDate
5668
a4bb88a1a643 A fix for https://issues.roundup-tracker.org/issue2551034
John Rouillard <rouilj@ieee.org>
parents: 5656
diff changeset
1578
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1579 def testEtagProcessing(self):
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1580 '''
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1581 Etags can come from two places:
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
1582 If-Match http header
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1583 @etags value posted in the form
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1584
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1585 Both will be checked if availble. If either one
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1586 fails, the etag check will fail.
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1587
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1588 Run over header only, etag in form only, both,
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1589 each one broke and no etag. Use the put command
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
1590 to trigger the etag checking code.
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1591 '''
6539
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1592 for mode in ('header', 'header-gzip', 'etag', 'etag-br',
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1593 'both', 'brokenheader', 'brokenetag', 'none'):
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1594 try:
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1595 # clean up any old header
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
1596 del(self.headers)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1597 except AttributeError:
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1598 pass
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1599
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1600 form = cgi.FieldStorage()
5726
e199d0ae4a25 issue2551033: prevent reverse engineering hidden data by using etags
John Rouillard <rouilj@ieee.org>
parents: 5711
diff changeset
1601 etag = calculate_etag(self.db.user.getnode(self.joeid),
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
1602 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1603 form.list = [
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1604 cgi.MiniFieldStorage('data', 'Joe Doe Doe'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1605 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1606
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1607 if mode == 'header':
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
1608 print("Mode = %s"%mode)
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
1609 self.headers = {'if-match': etag}
6539
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1610 elif mode == 'header-gzip':
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1611 print("Mode = %s"%mode)
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1612 gzip_etag = etag[:-1] + "-gzip" + etag[-1:]
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1613 self.headers = {'if-match': gzip_etag}
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1614 elif mode == 'etag':
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
1615 print("Mode = %s"%mode)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1616 form.list.append(cgi.MiniFieldStorage('@etag', etag))
6539
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1617 elif mode == 'etag-br':
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1618 print("Mode = %s"%mode)
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1619 br_etag = etag[:-1] + "-br" + etag[-1:]
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1620 form.list.append(cgi.MiniFieldStorage('@etag', br_etag))
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1621 elif mode == 'both':
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
1622 print("Mode = %s"%mode)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
1623 self.headers = {'etag': etag}
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1624 form.list.append(cgi.MiniFieldStorage('@etag', etag))
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1625 elif mode == 'brokenheader':
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
1626 print("Mode = %s"%mode)
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
1627 self.headers = {'if-match': 'bad'}
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1628 form.list.append(cgi.MiniFieldStorage('@etag', etag))
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1629 elif mode == 'brokenetag':
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
1630 print("Mode = %s"%mode)
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
1631 self.headers = {'if-match': etag}
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1632 form.list.append(cgi.MiniFieldStorage('@etag', 'bad'))
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1633 elif mode == 'none':
5645
7f4d19867123 Make print into function call for python3 compatibility.
John Rouillard <rouilj@ieee.org>
parents: 5643
diff changeset
1634 print( "Mode = %s"%mode)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1635 else:
6539
f8df7fed18f6 issue2551175 - Make ETag content-encoding aware.
John Rouillard <rouilj@ieee.org>
parents: 6525
diff changeset
1636 self.fail("unknown mode '%s' found"%mode)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1637
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1638 results = self.server.put_attribute(
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1639 'user', self.joeid, 'realname', form
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1640 )
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1641 if mode not in ('brokenheader', 'brokenetag', 'none'):
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1642 self.assertEqual(self.dummy_client.response_code, 200)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1643 else:
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1644 self.assertEqual(self.dummy_client.response_code, 412)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
1645
5993
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1646 def testBinaryFieldStorage(self):
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1647 ''' attempt to exercise all paths in the BinaryFieldStorage
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1648 class
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1649 '''
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1650
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1651 expected={ "data": {
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1652 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1",
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1653 "id": "1"
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1654 }
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1655 }
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1656
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1657 body=b'{ "title": "Joe Doe has problems", \
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1658 "nosy": [ "1", "3" ], \
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1659 "assignedto": "2", \
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1660 "abool": true, \
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1661 "afloat": 2.3, \
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1662 "anint": 567890 \
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1663 }'
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1664 env = { "CONTENT_TYPE": "application/json",
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1665 "CONTENT_LENGTH": len(body),
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1666 "REQUEST_METHOD": "POST"
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1667 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1668 self.server.client.env.update(env)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1669
5993
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1670 headers={"accept": "application/json; version=1",
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1671 "content-type": env['CONTENT_TYPE'],
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1672 "content-length": env['CONTENT_LENGTH'],
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1673 }
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1674 self.headers=headers
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1675 # we need to generate a FieldStorage the looks like
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1676 # FieldStorage(None, None, 'string') rather than
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1677 # FieldStorage(None, None, [])
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1678 body_file=BytesIO(body) # FieldStorage needs a file
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1679 form = client.BinaryFieldStorage(body_file,
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1680 headers=headers,
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1681 environ=env)
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1682 self.server.client.request.headers.get=self.get_header
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1683 results = self.server.dispatch(env["REQUEST_METHOD"],
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1684 "/rest/data/issue",
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1685 form)
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1686 json_dict = json.loads(b2s(results))
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1687 self.assertEqual(json_dict,expected)
a0ab2c5d1c2a Add test for BinaryFieldStorage.
John Rouillard <rouilj@ieee.org>
parents: 5987
diff changeset
1688
7372
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1689
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1690 def testDispatchGet(self):
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1691 self.create_sampledata()
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1692
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1693 form = cgi.FieldStorage()
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1694 self.server.client.request.headers.get=self.get_header
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1695
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1696 for item in [ "55", "issue1", "1" ]:
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1697 print("test item: '%s'" % item)
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1698 results = self.server.dispatch("GET",
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1699 "/rest/data/issue/%s" % item,
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1700 form)
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1701 json_dict = json.loads(b2s(results))
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1702 try:
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1703 self.assertEqual(json_dict['error']['status'], 404)
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1704 except KeyError as e:
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1705 if e.args[0] == "error" and item == "1":
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1706 pass
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1707 else:
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1708 self.assertTrue(False)
886a5c767d7e Invalid REST item spec returns 404 rather than 400.
John Rouillard <rouilj@ieee.org>
parents: 7183
diff changeset
1709
5690
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1710 def testDispatchPost(self):
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1711 """
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1712 run POST through rest dispatch(). This also tests
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1713 sending json payload through code as dispatch is the
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1714 code that changes json payload into something we can
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1715 process.
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1716 """
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1717
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1718 # TEST #0
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1719 # POST: issue make joe assignee and admin and demo as
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1720 # nosy
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1721 # simulate: /rest/data/issue
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1722 body=b'{ "title": "Joe Doe has problems", \
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1723 "nosy": [ "1", "3" ], \
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1724 "assignedto": "2", \
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1725 "abool": true, \
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1726 "afloat": 2.3, \
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1727 "anint": 567890 \
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1728 }'
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1729 env = { "CONTENT_TYPE": "application/json",
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1730 "CONTENT_LENGTH": len(body),
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1731 "REQUEST_METHOD": "POST"
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1732 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1733 self.server.client.env.update(env)
5690
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1734 headers={"accept": "application/json; version=1",
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1735 "content-type": env['CONTENT_TYPE'],
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1736 "content-length": env['CONTENT_LENGTH'],
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1737 }
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1738 self.headers=headers
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1739 # we need to generate a FieldStorage the looks like
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1740 # FieldStorage(None, None, 'string') rather than
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1741 # FieldStorage(None, None, [])
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1742 body_file=BytesIO(body) # FieldStorage needs a file
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1743 form = client.BinaryFieldStorage(body_file,
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1744 headers=headers,
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1745 environ=env)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1746 self.server.client.request.headers.get=self.get_header
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1747 results = self.server.dispatch(env["REQUEST_METHOD"],
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1748 "/rest/data/issue",
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1749 form)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1750
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1751 print(results)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1752 self.assertEqual(self.server.client.response_code, 201)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1753 json_dict = json.loads(b2s(results))
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1754 self.assertEqual(json_dict['data']['link'],
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1755 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1")
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1756 self.assertEqual(json_dict['data']['id'], "1")
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1757 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
5690
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1758 results = self.server.dispatch('GET',
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1759 "/rest/data/issue/1", self.empty_form)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1760 print(results)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1761 json_dict = json.loads(b2s(results))
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1762 self.assertEqual(json_dict['data']['link'],
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1763 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue/1")
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1764 self.assertEqual(json_dict['data']['attributes']['abool'], True)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1765 self.assertEqual(json_dict['data']['attributes']['afloat'], 2.3)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1766 self.assertEqual(json_dict['data']['attributes']['anint'], 567890)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1767 self.assertEqual(len(json_dict['data']['attributes']['nosy']), 3)
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1768 self.assertEqual(json_dict['data']['attributes']\
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1769 ['assignedto']['link'],
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1770 "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user/2")
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
1771
6317
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1772
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1773 def testDispatchDelete(self):
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1774 """
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1775 run Delete through rest dispatch().
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1776 """
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1777
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1778 # TEST #0
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1779 # Delete class raises unauthorized error
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1780 # simulate: /rest/data/issue
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1781 env = { "REQUEST_METHOD": "DELETE"
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1782 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1783 self.server.client.env.update(env)
6317
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1784 headers={"accept": "application/json; version=1",
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1785 }
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1786 self.headers=headers
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1787 self.server.client.request.headers.get=self.get_header
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1788 results = self.server.dispatch(env["REQUEST_METHOD"],
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1789 "/rest/data/issue",
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1790 self.empty_form)
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1791
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1792 print(results)
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1793 self.assertEqual(self.server.client.response_code, 403)
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1794 json_dict = json.loads(b2s(results))
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1795
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1796 self.assertEqual(json_dict['error']['msg'],
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1797 "Deletion of a whole class disabled")
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1798
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
1799
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1800 def testDispatchBadContent(self):
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1801 """
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1802 runthrough rest dispatch() with bad content_type patterns.
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1803 """
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1804
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1805 # simulate: /rest/data/issue
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1806 body=b'{ "title": "Joe Doe has problems", \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1807 "nosy": [ "1", "3" ], \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1808 "assignedto": "2", \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1809 "abool": true, \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1810 "afloat": 2.3, \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1811 "anint": 567890 \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1812 }'
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1813 env = { "CONTENT_TYPE": "application/jzot",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1814 "CONTENT_LENGTH": len(body),
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1815 "REQUEST_METHOD": "POST"
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1816 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
1817 self.server.client.env.update(env)
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1818
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1819 headers={"accept": "application/json; version=1",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1820 "content-type": env['CONTENT_TYPE'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1821 "content-length": env['CONTENT_LENGTH'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1822 }
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1823
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1824 self.headers=headers
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1825 # we need to generate a FieldStorage the looks like
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1826 # FieldStorage(None, None, 'string') rather than
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1827 # FieldStorage(None, None, [])
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1828 body_file=BytesIO(body) # FieldStorage needs a file
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1829 form = client.BinaryFieldStorage(body_file,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1830 headers=headers,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1831 environ=env)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1832 self.server.client.request.headers.get=self.get_header
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1833 results = self.server.dispatch(env["REQUEST_METHOD"],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1834 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1835 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1836
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1837 print(results)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1838 self.assertEqual(self.server.client.response_code, 415)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1839 json_dict = json.loads(b2s(results))
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1840 self.assertEqual(json_dict['error']['msg'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1841 "Unable to process input of type application/jzot")
8094
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1842 self.assertNotIn("Accept-Patch",
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1843 self.server.client.additional_headers)
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1844 self.server.client.additional_headers = {}
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1845
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1846
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1847 # test with PATCH verb to verify Accept-Patch is correct
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1848 results = self.server.dispatch("PATCH",
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1849 "/rest/data/issue",
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1850 form)
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1851 self.assertEqual(self.server.client.response_code, 415)
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1852 json_dict = json.loads(b2s(results))
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1853 self.assertEqual(json_dict['error']['msg'],
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1854 "Unable to process input of type application/jzot")
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1855 self.assertIn("Accept-Patch",
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1856 self.server.client.additional_headers)
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1857 self.assertEqual(self.server.client.additional_headers["Accept-Patch"],
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1858 "application/json, application/x-www-form-urlencoded" )
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1859 self.server.client.additional_headers = {}
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1860
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1861 # Test GET as well. I am not sure if this should pass or not.
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1862 # Arguably GET doesn't use any form/json input but....
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1863 results = self.server.dispatch('GET',
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1864 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1865 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1866 print(results)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1867 self.assertEqual(self.server.client.response_code, 415)
8094
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1868 self.assertNotIn("Accept-Patch",
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1869 self.server.client.additional_headers)
8e310a7b5e09 issue2551131 - Return accept-patch if patch body not accepted (415 code)
John Rouillard <rouilj@ieee.org>
parents: 7854
diff changeset
1870 self.server.client.additional_headers = {}
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
1871
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1872 def testdetermine_output_formatBadAccept(self):
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1873 dof = self.server.determine_output_format
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1874
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1875 # simulate: /rest/data/issue expect failure unknown accept settings
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1876 body=b'{ "title": "Joe Doe has problems", \
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1877 "nosy": [ "1", "3" ], \
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1878 "assignedto": "2", \
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1879 "abool": true, \
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1880 "afloat": 2.3, \
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1881 "anint": 567890 \
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1882 }'
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1883 env = { "CONTENT_TYPE": "application/json",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1884 "CONTENT_LENGTH": len(body),
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1885 "REQUEST_METHOD": "POST"
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1886 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1887 self.server.client.env.update(env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1888 headers={"accept": "application/zot; version=1; q=0.5",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1889 "content-type": env['CONTENT_TYPE'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1890 "content-length": env['CONTENT_LENGTH'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1891 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1892
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1893 self.headers=headers
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1894 # we need to generate a FieldStorage the looks like
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1895 # FieldStorage(None, None, 'string') rather than
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1896 # FieldStorage(None, None, [])
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1897 body_file=BytesIO(body) # FieldStorage needs a file
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1898 form = client.BinaryFieldStorage(body_file,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1899 headers=headers,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1900 environ=env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1901 self.server.client.request.headers.get=self.get_header
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1902
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1903 (output_type, uri, error) = dof("/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1904
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1905 self.assertEqual(self.server.client.response_code, 406)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1906 self.assertIn(b"Requested content type(s) 'application/zot; version=1; q=0.5' not available.\nAcceptable mime types are: */*, application/json",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1907 s2b(error['error']['msg']))
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1908
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1909 # simulate: /rest/data/issue works, multiple acceptable output, one
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1910 # is valid
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1911 self.server.client.response_code = ""
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1912 env = { "CONTENT_TYPE": "application/json",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1913 "CONTENT_LENGTH": len(body),
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1914 "REQUEST_METHOD": "POST"
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1915 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1916 self.server.client.env.update(env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1917 headers={"accept": "application/zot; version=1; q=0.75, "
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1918 "application/json; version=1; q=0.5",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1919 "content-type": env['CONTENT_TYPE'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1920 "content-length": env['CONTENT_LENGTH'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1921 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1922
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1923 self.headers=headers
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1924 # we need to generate a FieldStorage the looks like
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1925 # FieldStorage(None, None, 'string') rather than
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1926 # FieldStorage(None, None, [])
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1927 body_file=BytesIO(body) # FieldStorage needs a file
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1928 form = client.BinaryFieldStorage(body_file,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1929 headers=headers,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1930 environ=env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1931 self.server.client.request.headers.get=self.get_header
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1932 (output_type, uri, error) = dof("/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1933
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1934 self.assertEqual(self.server.client.response_code, "")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1935 self.assertEqual(output_type, "json")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1936 self.assertEqual(uri, "/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1937 self.assertEqual(error, None)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1938
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1939 # test 3 accept is empty. This triggers */* so passes
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1940 self.server.client.response_code = ""
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1941 headers={"accept": "",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1942 "content-type": env['CONTENT_TYPE'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1943 "content-length": env['CONTENT_LENGTH'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1944 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1945
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1946 self.headers=headers
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1947 # we need to generate a FieldStorage the looks like
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1948 # FieldStorage(None, None, 'string') rather than
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1949 # FieldStorage(None, None, [])
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1950 body_file=BytesIO(body) # FieldStorage needs a file
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1951 form = client.BinaryFieldStorage(body_file,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1952 headers=headers,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1953 environ=env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1954 self.server.client.request.headers.get=self.get_header
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1955 (output_type, uri, error) = dof("/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1956
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1957 self.assertEqual(self.server.client.response_code, "")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1958 self.assertEqual(output_type, "json")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1959 self.assertEqual(uri, "/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1960 self.assertEqual(error, None)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1961
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1962 # test 4 accept is random junk.
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1963 headers={"accept": "Xyzzy I am not a mime, type;",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1964 "content-type": env['CONTENT_TYPE'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1965 "content-length": env['CONTENT_LENGTH'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1966 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1967
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1968 self.headers=headers
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1969 # we need to generate a FieldStorage the looks like
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1970 # FieldStorage(None, None, 'string') rather than
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1971 # FieldStorage(None, None, [])
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1972 body_file=BytesIO(body) # FieldStorage needs a file
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1973 form = client.BinaryFieldStorage(body_file,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1974 headers=headers,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1975 environ=env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1976 self.server.client.request.headers.get=self.get_header
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1977 (output_type, uri, error) = dof("/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1978
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1979 self.assertEqual(self.server.client.response_code, 400)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1980 self.assertEqual(output_type, None)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1981 self.assertEqual(uri, "/rest/data/issue")
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
1982 self.assertIn('Unable to parse Accept Header. Invalid media type: Xyzzy I am not a mime. Acceptable types: */*, application/json', error['error']['msg'])
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1983
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1984 # test 5 accept mimetype is ok, param is not
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1985 headers={"accept": "*/*; foo",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1986 "content-type": env['CONTENT_TYPE'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1987 "content-length": env['CONTENT_LENGTH'],
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1988 }
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1989
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1990 self.headers=headers
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1991 # we need to generate a FieldStorage the looks like
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1992 # FieldStorage(None, None, 'string') rather than
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1993 # FieldStorage(None, None, [])
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1994 body_file=BytesIO(body) # FieldStorage needs a file
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1995 form = client.BinaryFieldStorage(body_file,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1996 headers=headers,
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1997 environ=env)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1998 self.server.client.request.headers.get=self.get_header
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
1999 (output_type, uri, error) = dof("/rest/data/issue")
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2000
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2001 self.assertEqual(self.server.client.response_code, 400)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2002 self.assertEqual(output_type, None)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2003 self.assertEqual(uri, "/rest/data/issue")
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2004 self.assertIn('Unable to parse Accept Header. Invalid param: foo. Acceptable types: */*, application/json', error['error']['msg'])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2005
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2006 # test 6: test paths:
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2007 #
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2008 test_suite = [
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2009 (# use binary_content on a class that doesn't support it
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2010 {"path": "/rest/data/issue/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2011 "accept": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2012 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2013 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2014 "uri": "/rest/data/issue/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2015 "error": None
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2016 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2017 (# use invalid class
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2018 {"path": "/rest/data/notissue/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2019 "accept": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2020 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2021 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2022 "uri": "/rest/data/notissue/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2023 "error": None
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2024 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2025 (# use invalid id
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2026 {"path": "/rest/data/issue/99/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2027 "accept": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2028 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2029 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2030 "uri": "/rest/data/issue/99/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2031 "error": None
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2032 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2033 ]
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2034
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2035 for test in test_suite:
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2036 self.server.client.response_code = ""
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2037 env = { "CONTENT_TYPE": "application/json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2038 "CONTENT_LENGTH": len(body),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2039 "REQUEST_METHOD": "GET"
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2040 }
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2041 self.server.client.env.update(env)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2042 headers={"accept": test["accept"] or
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2043 "application/zot; version=1; q=0.75, "
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2044 "application/json; version=1; q=0.5",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2045 "content-type": env['CONTENT_TYPE'],
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2046 "content-length": env['CONTENT_LENGTH'],
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2047 }
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2048
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2049 self.headers=headers
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2050 # we need to generate a FieldStorage the looks like
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2051 # FieldStorage(None, None, 'string') rather than
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2052 # FieldStorage(None, None, [])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2053 body_file=BytesIO(body) # FieldStorage needs a file
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2054 form = client.BinaryFieldStorage(
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2055 body_file,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2056 headers=headers,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2057 environ=env)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2058 self.server.client.request.headers.get=self.get_header
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2059 (output_type, uri, error) = dof(test["path"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2060
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2061 self.assertEqual(self.server.client.response_code,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2062 test["response_code"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2063 self.assertEqual(output_type, test["output_type"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2064 self.assertEqual(uri, test["uri"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2065 self.assertEqual(error, test["error"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2066
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2067 # test 7: test paths:
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2068 #
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2069 test_suite = [
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2070 (# use wildcard accept on item and get back json output
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2071 {"path": "/rest/data/file/1",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2072 "accept": "*/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2073 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2074 "output_type": "json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2075 "uri": "/rest/data/file/1",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2076 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2077 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2078 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2079 (# use wildcard accept and get back file's actual mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2080 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2081 "accept": "*/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2082 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2083 "output_type": "image/png",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2084 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2085 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2086 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2087
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2088 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2089 (# use json accept and get back json
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2090 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2091 "accept": "application/json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2092 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2093 "output_type": "json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2094 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2095 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2096 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2097 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2098 (# use json accept with invalid number version and get back error
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2099 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2100 "accept": "application/json; q=0.5; version=22",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2101 "response_code": 406,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2102 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2103 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2104 "error": {'error': {'status': 406, 'msg': 'Unrecognized api version: 22. See /rest without specifying api version for supported versions.'}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2105 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2106 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2107 (# use json accept with invalid string version and get back error
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2108 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2109 "accept": "application/json; q=0.5; version=z",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2110 "response_code": 406,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2111 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2112 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2113 "error": {'error': {'status': 406, 'msg': 'Unrecognized api version: z. See /rest without specifying api version for supported versions.'}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2114 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2115 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2116 (# use octet-stream accept and get back octet-stream mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2117 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2118 "accept": "application/octet-stream; q=0.9, */*; q=0.5",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2119 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2120 "output_type": "application/octet-stream",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2121 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2122 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2123 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2124 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2125 (# use image/png accept and get back image/png mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2126 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2127 "accept": "application/octet-stream; q=0.9, image/png",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2128 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2129 "output_type": "image/png",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2130 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2131 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2132 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2133 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2134 (# use invalid accept and get back error
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2135 {"path": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2136 "accept": "image/svg+html",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2137 "response_code": 406,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2138 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2139 "uri": "/rest/data/file/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2140 "error": {'error':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2141 {'status': 406, 'msg': "Requested content type(s) 'image/svg+html' not available.\nAcceptable mime types are: */*, application/octet-stream, image/png"}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2142 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2143 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2144 (# use wildcard accept and get back msg's actual mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2145 {"path": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2146 "accept": "*/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2147 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2148 "output_type": "text/markdown",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2149 "uri": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2150 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2151 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2152 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2153 (# use octet-stream accept and get back octet-stream mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2154 {"path": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2155 "accept": "application/octet-stream; q=0.9, */*; q=0.5",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2156 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2157 "output_type": "application/octet-stream",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2158 "uri": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2159 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2160 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2161 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2162
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2163 (# use wildcard text accept and get back msg's actual mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2164 {"path": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2165 "accept": "text/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2166 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2167 "output_type": "text/markdown",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2168 "uri": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2169 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2170 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2171 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2172 (# use wildcard text accept and get back file's actual mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2173 {"path": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2174 "accept": "text/markdown",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2175 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2176 "output_type": "text/markdown",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2177 "uri": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2178 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2179 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2180 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2181 (# use text/plain accept and get back test/plain
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2182 {"path": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2183 "accept": "text/plain",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2184 "response_code": 406,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2185 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2186 "uri": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2187 "error": {'error':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2188 {'status': 406, 'msg':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2189 "Requested content type(s) 'text/plain' not available.\nAcceptable mime types are: */*, application/octet-stream, text/*, text/markdown"}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2190 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2191 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2192 (# use wildcard accept and get back default msg mime type
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2193 {"path": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2194 "accept": "*/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2195 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2196 "output_type": "text/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2197 "uri": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2198 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2199 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2200 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2201 (# use text/* and get back text/*
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2202 {"path": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2203 "accept": "text/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2204 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2205 "output_type": "text/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2206 "uri": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2207 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2208 "has_nosniff": True,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2209 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2210 (# use text/markdown and get back error
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2211 {"path": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2212 "accept": "text/markdown",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2213 "response_code": 406,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2214 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2215 "uri": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2216 "error": {'error':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2217 {'status': 406, 'msg':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2218 "Requested content type(s) 'text/markdown' not available.\nAcceptable mime types are: */*, application/octet-stream, text/*"}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2219 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2220 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2221 (# use error accept and get back error
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2222 {"path": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2223 "accept": "text/markdown, q=2",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2224 "response_code": 400,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2225 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2226 "uri": "/rest/data/msg/1/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2227 "error": {'error':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2228 {'status': 400, 'msg':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2229 'Unable to parse Accept Header. Invalid media type: q=2. Acceptable types: */*, application/json'}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2230 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2231 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2232 (# use text/* but override with extension of .json get back json
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2233 {"path": "/rest/data/msg/2/binary_content.json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2234 "accept": "text/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2235 "response_code": "",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2236 "output_type": "json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2237 "uri": "/rest/data/msg/2/binary_content",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2238 "error": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2239 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2240 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2241 (# use text/* but override with extension of .jon get back error
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2242 {"path": "/rest/data/msg/2/binary_content.jon",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2243 "accept": "text/*",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2244 "response_code": 406,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2245 "output_type": None,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2246 "uri": "/rest/data/msg/2/binary_content.jon",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2247 "error": {'error':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2248 {'status': 406, 'msg':
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2249 "Content type 'jon' requested in URL is not available.\nAcceptable types: json\n"}},
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2250 "has_nosniff": False,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2251 }),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2252 ]
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2253
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2254 for test in test_suite:
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2255 print(test)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2256 self.server.client.response_code = ""
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2257 self.server.client.additional_headers = {}
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2258 env = { "CONTENT_TYPE": "application/json",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2259 "CONTENT_LENGTH": len(body),
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2260 "REQUEST_METHOD": "GET"
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2261 }
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2262 self.server.client.env.update(env)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2263 headers={"accept": test["accept"] or
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2264 "application/zot; version=1; q=0.75, "
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2265 "application/json; version=1; q=0.5",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2266 "content-type": env['CONTENT_TYPE'],
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2267 "content-length": env['CONTENT_LENGTH'],
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2268 }
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2269
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2270 self.headers=headers
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2271 # we need to generate a FieldStorage the looks like
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2272 # FieldStorage(None, None, 'string') rather than
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2273 # FieldStorage(None, None, [])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2274 body_file=BytesIO(body) # FieldStorage needs a file
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2275 form = client.BinaryFieldStorage(
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2276 body_file,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2277 headers=headers,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2278 environ=env)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2279 self.server.client.request.headers.get=self.get_header
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2280 (output_type, uri, error) = dof(test["path"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2281
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2282 self.assertEqual(self.server.client.response_code,
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2283 test["response_code"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2284 self.assertEqual(output_type, test["output_type"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2285 self.assertEqual(uri, test["uri"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2286 print(error)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2287 self.assertEqual(error, test["error"])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2288 if test["has_nosniff"]:
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2289 self.assertIn("X-Content-Type-Options",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2290 self.server.client.additional_headers)
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2291 self.assertEqual("nosniff",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2292 self.server.client.additional_headers['X-Content-Type-Options'])
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2293 else:
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2294 self.assertNotIn("X-Content-Type-Options",
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2295 self.server.client.additional_headers)
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2296
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2297 def testDispatchBadAccept(self):
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2298 # simulate: /rest/data/issue expect failure unknown accept settings
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2299 body=b'{ "title": "Joe Doe has problems", \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2300 "nosy": [ "1", "3" ], \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2301 "assignedto": "2", \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2302 "abool": true, \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2303 "afloat": 2.3, \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2304 "anint": 567890 \
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2305 }'
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2306 env = { "CONTENT_TYPE": "application/json",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2307 "CONTENT_LENGTH": len(body),
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2308 "REQUEST_METHOD": "POST"
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2309 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2310 self.server.client.env.update(env)
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2311 headers={"accept": "application/zot; version=1; q=0.5",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2312 "content-type": env['CONTENT_TYPE'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2313 "content-length": env['CONTENT_LENGTH'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2314 }
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2315
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2316 self.headers=headers
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2317 # we need to generate a FieldStorage the looks like
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2318 # FieldStorage(None, None, 'string') rather than
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2319 # FieldStorage(None, None, [])
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2320 body_file=BytesIO(body) # FieldStorage needs a file
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2321 form = client.BinaryFieldStorage(body_file,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2322 headers=headers,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2323 environ=env)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2324 self.server.client.request.headers.get=self.get_header
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2325 results = self.server.dispatch(env["REQUEST_METHOD"],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2326 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2327 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2328 print(results)
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2329 json_dict = json.loads(b2s(results))
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2330 self.assertEqual(self.server.client.response_code, 406)
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2331 self.assertIn("Requested content type(s) 'application/zot; version=1; q=0.5' not available.\nAcceptable mime types are: */*, application/json",
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2332 json_dict['error']['msg'])
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2333
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2334 # simulate: /rest/data/issue works, multiple acceptable output, one
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2335 # is valid
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2336 env = { "CONTENT_TYPE": "application/json",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2337 "CONTENT_LENGTH": len(body),
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2338 "REQUEST_METHOD": "POST"
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2339 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2340 self.server.client.env.update(env)
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2341 headers={"accept": "application/zot; version=1; q=0.75, "
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2342 "application/json; version=1; q=0.5",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2343 "content-type": env['CONTENT_TYPE'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2344 "content-length": env['CONTENT_LENGTH'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2345 }
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2346
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2347 self.headers=headers
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2348 # we need to generate a FieldStorage the looks like
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2349 # FieldStorage(None, None, 'string') rather than
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2350 # FieldStorage(None, None, [])
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2351 body_file=BytesIO(body) # FieldStorage needs a file
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2352 form = client.BinaryFieldStorage(body_file,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2353 headers=headers,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2354 environ=env)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2355 self.server.client.request.headers.get=self.get_header
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2356 results = self.server.dispatch(env["REQUEST_METHOD"],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2357 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2358 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2359
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2360 print(results)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2361 self.assertEqual(self.server.client.response_code, 201)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2362 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2363 self.assertEqual(json_dict['data']['id'], "1")
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2364
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2365
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2366 # test 3 accept is empty. This triggers */* so passes
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2367 headers={"accept": "",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2368 "content-type": env['CONTENT_TYPE'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2369 "content-length": env['CONTENT_LENGTH'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2370 }
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2371
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2372 self.headers=headers
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2373 # we need to generate a FieldStorage the looks like
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2374 # FieldStorage(None, None, 'string') rather than
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2375 # FieldStorage(None, None, [])
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2376 body_file=BytesIO(body) # FieldStorage needs a file
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2377 form = client.BinaryFieldStorage(body_file,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2378 headers=headers,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2379 environ=env)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2380 self.server.client.request.headers.get=self.get_header
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2381 results = self.server.dispatch(env["REQUEST_METHOD"],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2382 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2383 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2384
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2385 print(results)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2386 self.assertEqual(self.server.client.response_code, 201)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2387 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2388 self.assertEqual(json_dict['data']['id'], "2")
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2389
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2390 # test 4 accept is random junk.
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2391 headers={"accept": "Xyzzy I am not a mime, type;",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2392 "content-type": env['CONTENT_TYPE'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2393 "content-length": env['CONTENT_LENGTH'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2394 }
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2395
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2396 self.headers=headers
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2397 # we need to generate a FieldStorage the looks like
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2398 # FieldStorage(None, None, 'string') rather than
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2399 # FieldStorage(None, None, [])
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2400 body_file=BytesIO(body) # FieldStorage needs a file
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2401 form = client.BinaryFieldStorage(body_file,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2402 headers=headers,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2403 environ=env)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2404 self.server.client.request.headers.get=self.get_header
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2405 results = self.server.dispatch(env["REQUEST_METHOD"],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2406 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2407 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2408
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2409 print(results)
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2410 self.assertEqual(self.server.client.response_code, 400)
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2411 json_dict = json.loads(b2s(results))
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2412 self.assertIn('Unable to parse Accept Header. Invalid media type: Xyzzy I am not a mime. Acceptable types: */*, application/json', json_dict['error']['msg'])
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2413
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2414 # test 5 accept mimetype is ok, param is not
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2415 headers={"accept": "*/*; foo",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2416 "content-type": env['CONTENT_TYPE'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2417 "content-length": env['CONTENT_LENGTH'],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2418 }
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2419
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2420 self.headers=headers
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2421 # we need to generate a FieldStorage the looks like
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2422 # FieldStorage(None, None, 'string') rather than
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2423 # FieldStorage(None, None, [])
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2424 body_file=BytesIO(body) # FieldStorage needs a file
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2425 form = client.BinaryFieldStorage(body_file,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2426 headers=headers,
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2427 environ=env)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2428 self.server.client.request.headers.get=self.get_header
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2429 results = self.server.dispatch(env["REQUEST_METHOD"],
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2430 "/rest/data/issue",
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2431 form)
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2432
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2433 print(results)
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2434 self.assertEqual(self.server.client.response_code, 400)
6311
be8d5a8e090a Fix uncaught error when parsing rest headers, document
John Rouillard <rouilj@ieee.org>
parents: 6256
diff changeset
2435 json_dict = json.loads(b2s(results))
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
2436 self.assertIn('Unable to parse Accept Header. Invalid param: foo. Acceptable types: */*, application/json', json_dict['error']['msg'])
5690
4aae822e2cb4 Added a few comments and a test that fails with the pre-patched code
John Rouillard <rouilj@ieee.org>
parents: 5686
diff changeset
2437
8220
818751637b77 fix: make rest.py still load on python2, do not test bad json
John Rouillard <rouilj@ieee.org>
parents: 8218
diff changeset
2438 @skip_on_py2
8218
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2439 def testBadJson(self):
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2440 '''Run some JSON we don't accept through the wringer
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2441 '''
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2442 body=b'{ "title": "Joe Doe has problems", \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2443 "nosy": [ "1", "3" ], \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2444 "assignedto": "2", \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2445 "abool": true, \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2446 "afloat": 2.3, \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2447 "anint": Infinity }'
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2448
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2449 expected={ "error":
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2450 {"status": 400,
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2451 "msg": ("Unacceptable number: Infinity. JSON is: "
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2452 + b2s(body)),
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2453 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2454 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2455
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2456 env = { "CONTENT_TYPE": "application/json",
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2457 "CONTENT_LENGTH": len(body),
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2458 "REQUEST_METHOD": "PUT"
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2459 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2460 self.server.client.env.update(env)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2461 headers={"accept": "application/zot; version=1; q=0.5",
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2462 "content-type": env['CONTENT_TYPE'],
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2463 "content-length": env['CONTENT_LENGTH'],
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2464 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2465
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2466 self.headers=headers
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2467 # we need to generate a FieldStorage the looks like
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2468 # FieldStorage(None, None, 'string') rather than
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2469 # FieldStorage(None, None, [])
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2470 body_file=BytesIO(body) # FieldStorage needs a file
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2471 form = client.BinaryFieldStorage(body_file,
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2472 headers=headers,
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2473 environ=env)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2474 self.server.client.request.headers.get=self.get_header
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2475 results = self.server.dispatch(env["REQUEST_METHOD"],
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2476 "/rest/data/issue/1",
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2477 form)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2478
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2479 self.assertEqual(json.loads(results), expected)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2480
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2481 body=b'{ "title": "Joe Doe has problems", \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2482 nosy: [ "1", "3" ], \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2483 "assignedto": "2", \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2484 "abool": true, \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2485 "afloat": 2.3, \
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2486 "anint": Infinity }'
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2487 self.maxDiff = None
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2488 expected={ "error":
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2489 {"status": 400,
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2490 "msg": ("Expecting property name enclosed in double "
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2491 "quotes: line 1 column 53 (char 52). JSON is: "
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2492 + b2s(body)),
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2493 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2494 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2495
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2496 env = { "CONTENT_TYPE": "application/json",
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2497 "CONTENT_LENGTH": len(body),
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2498 "REQUEST_METHOD": "PUT"
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2499 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2500 self.server.client.env.update(env)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2501 headers={"accept": "application/zot; version=1; q=0.5",
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2502 "content-type": env['CONTENT_TYPE'],
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2503 "content-length": env['CONTENT_LENGTH'],
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2504 }
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2505
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2506 self.headers=headers
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2507 # we need to generate a FieldStorage the looks like
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2508 # FieldStorage(None, None, 'string') rather than
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2509 # FieldStorage(None, None, [])
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2510 body_file=BytesIO(body) # FieldStorage needs a file
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2511 form = client.BinaryFieldStorage(body_file,
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2512 headers=headers,
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2513 environ=env)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2514 self.server.client.request.headers.get=self.get_header
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2515 results = self.server.dispatch(env["REQUEST_METHOD"],
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2516 "/rest/data/issue/1",
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2517 form)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2518
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2519 self.assertEqual(json.loads(results), expected)
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2520
6185
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2521 def testStatsGen(self):
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2522 # check stats being returned by put and get ops
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2523 # using dispatch which parses the @stats query param
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2524
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2525 # find correct py2/py3 list comparison ignoring order
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2526 try:
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2527 list_test = self.assertCountEqual # py3
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2528 except AttributeError:
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2529 list_test = self.assertItemsEqual # py2.7+
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2530
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2531 # get stats
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2532 form = cgi.FieldStorage()
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2533 form.list = [
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2534 cgi.MiniFieldStorage('@stats', 'True'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2535 ]
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2536 results = self.server.dispatch('GET',
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2537 "/rest/data/user/1/realname",
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2538 form)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2539 self.assertEqual(self.dummy_client.response_code, 200)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2540 json_dict = json.loads(b2s(results))
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2541
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2542 # check that @stats are defined
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2543 self.assertTrue( '@stats' in json_dict['data'] )
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2544 # check that the keys are present
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2545 # not validating values as that changes
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2546 valid_fields= [ us2u('elapsed'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2547 us2u('cache_hits'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2548 us2u('cache_misses'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2549 us2u('get_items'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2550 us2u('filtering') ]
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2551 list_test(valid_fields,json_dict['data']['@stats'].keys())
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2552
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2553 # Make sure false value works to suppress @stats
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2554 form = cgi.FieldStorage()
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2555 form.list = [
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2556 cgi.MiniFieldStorage('@stats', 'False'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2557 ]
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2558 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
6185
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2559 results = self.server.dispatch('GET',
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2560 "/rest/data/user/1/realname",
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2561 form)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2562 self.assertEqual(self.dummy_client.response_code, 200)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2563 json_dict = json.loads(b2s(results))
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2564 print(results)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2565 # check that @stats are not defined
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2566 self.assertTrue( '@stats' not in json_dict['data'] )
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2567
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2568 # Make sure non-true value works to suppress @stats
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2569 # false will always work
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2570 form = cgi.FieldStorage()
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2571 form.list = [
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2572 cgi.MiniFieldStorage('@stats', 'random'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2573 ]
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2574 results = self.server.dispatch('GET',
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2575 "/rest/data/user/1/realname",
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2576 form)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2577 self.assertEqual(self.dummy_client.response_code, 200)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2578 json_dict = json.loads(b2s(results))
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2579 print(results)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2580 # check that @stats are not defined
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2581 self.assertTrue( '@stats' not in json_dict['data'] )
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2582
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2583 # if @stats is not defined there should be no stats
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2584 results = self.server.dispatch('GET',
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2585 "/rest/data/user/1/realname",
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2586 self.empty_form)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2587 self.assertEqual(self.dummy_client.response_code, 200)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2588 json_dict = json.loads(b2s(results))
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2589
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2590 # check that @stats are not defined
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2591 self.assertTrue( '@stats' not in json_dict['data'] )
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2592
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2593
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2594
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2595 # change admin's realname via a normal web form
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2596 # This generates a FieldStorage that looks like:
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2597 # FieldStorage(None, None, [])
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2598 # use etag from header
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2599 #
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2600 # Also use GET on the uri via the dispatch to retrieve
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2601 # the results from the db.
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2602 etag = calculate_etag(self.db.user.getnode('1'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2603 self.db.config['WEB_SECRET_KEY'])
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2604 headers={"if-match": etag,
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2605 "accept": "application/vnd.json.test-v1+json",
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2606 }
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2607 form = cgi.FieldStorage()
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2608 form.list = [
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2609 cgi.MiniFieldStorage('data', 'Joe Doe'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2610 cgi.MiniFieldStorage('@apiver', '1'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2611 cgi.MiniFieldStorage('@stats', 'true'),
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2612 ]
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2613 self.headers = headers
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2614 self.server.client.request.headers.get = self.get_header
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2615 self.db.setCurrentUser('admin') # must be admin to change user
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2616 self.server.client.env.update({'REQUEST_METHOD': 'PUT'})
6185
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2617 results = self.server.dispatch('PUT',
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2618 "/rest/data/user/1/realname",
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2619 form)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2620 self.assertEqual(self.dummy_client.response_code, 200)
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2621 json_dict = json.loads(b2s(results))
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2622 list_test(valid_fields,json_dict['data']['@stats'].keys())
1cb2375015f0 Enable timing stats reporting in REST interface.
John Rouillard <rouilj@ieee.org>
parents: 6090
diff changeset
2623
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2624 def testDispatch(self):
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2625 """
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2626 run changes through rest dispatch(). This also tests
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2627 sending json payload through code as dispatch is the
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2628 code that changes json payload into something we can
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2629 process.
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2630 """
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2631 # TEST #1
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2632 # PUT: joe's 'realname' using json data.
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2633 # simulate: /rest/data/user/<id>/realname
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2634 # use etag in header
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2635 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2636 self.db.config['WEB_SECRET_KEY'])
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2637 body=b'{ "data": "Joe Doe 1" }'
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2638 env = { "CONTENT_TYPE": "application/json",
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2639 "CONTENT_LENGTH": len(body),
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2640 "REQUEST_METHOD": "PUT",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2641 "HTTP_ORIGIN": "https://invalid.origin"
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2642 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2643 self.server.client.env.update(env)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2644
5686
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2645 headers={"accept": "application/json; version=1",
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2646 "content-type": env['CONTENT_TYPE'],
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2647 "content-length": env['CONTENT_LENGTH'],
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2648 "if-match": etag
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2649 }
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2650 self.headers=headers
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2651 # we need to generate a FieldStorage the looks like
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2652 # FieldStorage(None, None, 'string') rather than
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2653 # FieldStorage(None, None, [])
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2654 body_file=BytesIO(body) # FieldStorage needs a file
5656
d26d2590cd8c Implement different workaround for https://bugs.python.org/issue27777
John Rouillard <rouilj@ieee.org>
parents: 5655
diff changeset
2655 form = client.BinaryFieldStorage(body_file,
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2656 headers=headers,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2657 environ=env)
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
2658 self.server.client.request.headers.get=self.get_header
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2659 results = self.server.dispatch('PUT',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2660 "/rest/data/user/%s/realname"%self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2661 form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2662
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2663 # invalid origin, no credentials allowed.
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2664 self.assertNotIn("Access-Control-Allow-Credentials",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2665 self.server.client.additional_headers)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2666 self.assertEqual(self.server.client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2667 results = self.server.get_element('user', self.joeid, self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2668 self.assertEqual(self.dummy_client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2669 self.assertEqual(results['data']['attributes']['realname'],
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2670 'Joe Doe 1')
5686
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2671
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2672
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2673 # substitute the version with an unacceptable version
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2674 # and verify it returns 400 code.
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2675 self.headers["accept"] = "application/json; version=1.1"
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2676 body_file=BytesIO(body) # FieldStorage needs a file
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2677 form = client.BinaryFieldStorage(body_file,
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2678 headers=headers,
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2679 environ=env)
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2680 self.server.client.request.headers.get=self.get_header
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2681 results = self.server.dispatch('PUT',
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2682 "/rest/data/user/%s/realname"%self.joeid,
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2683 form)
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2684 self.assertEqual(self.server.client.response_code, 406)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2685 del(self.headers)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2686
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2687 # TEST #2
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2688 # Set joe's 'realname' using json data.
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2689 # simulate: /rest/data/user/<id>/realname
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2690 # use etag in payload
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2691 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2692 self.db.config['WEB_SECRET_KEY'])
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2693 etagb = etag.strip ('"')
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2694 body=s2b('{ "@etag": "\\"%s\\"", "data": "Joe Doe 2" }'%etagb)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2695 env = { "CONTENT_TYPE": "application/json",
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2696 "CONTENT_LENGTH": len(body),
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2697 "REQUEST_METHOD": "PUT",
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2698 }
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2699 self.headers=None # have FieldStorage get len from env.
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2700 body_file=BytesIO(body) # FieldStorage needs a file
5656
d26d2590cd8c Implement different workaround for https://bugs.python.org/issue27777
John Rouillard <rouilj@ieee.org>
parents: 5655
diff changeset
2701 form = client.BinaryFieldStorage(body_file,
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2702 headers=None,
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2703 environ=env)
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
2704 self.server.client.request.headers.get=self.get_header
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2705
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2706 headers={"accept": "application/json",
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2707 "content-type": env['CONTENT_TYPE'],
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2708 "if-match": etag
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2709 }
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2710 self.headers=headers # set for dispatch
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2711
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2712 results = self.server.dispatch('PUT',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2713 "/rest/data/user/%s/realname"%self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2714 form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2715
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2716 self.assertEqual(self.server.client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2717 results = self.server.get_element('user', self.joeid, self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2718 self.assertEqual(self.dummy_client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2719 self.assertEqual(results['data']['attributes']['realname'],
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2720 'Joe Doe 2')
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2721 del(self.headers)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2722
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2723 # TEST #3
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2724 # change Joe's realname via a normal web form
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2725 # This generates a FieldStorage that looks like:
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2726 # FieldStorage(None, None, [])
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2727 # use etag from header
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2728 #
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2729 # Also use GET on the uri via the dispatch to retrieve
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2730 # the results from the db.
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2731 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2732 self.db.config['WEB_SECRET_KEY'])
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2733 headers={"if-match": etag,
5686
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2734 "accept": "application/vnd.json.test-v1+json",
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2735 }
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2736 form = cgi.FieldStorage()
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2737 form.list = [
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2738 cgi.MiniFieldStorage('data', 'Joe Doe'),
5686
eb51c0d9c9bf Move @apiver version extraction code after the input is parsed for
John Rouillard <rouilj@ieee.org>
parents: 5682
diff changeset
2739 cgi.MiniFieldStorage('@apiver', '1'),
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2740 ]
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2741 self.headers = headers
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
2742 self.server.client.request.headers.get = self.get_header
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2743 results = self.server.dispatch('PUT',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2744 "/rest/data/user/%s/realname"%self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2745 form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2746 self.assertEqual(self.dummy_client.response_code, 200)
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2747 self.server.client.env.update({'REQUEST_METHOD': "GET"})
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2748 results = self.server.dispatch('GET',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2749 "/rest/data/user/%s/realname"%self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2750 self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2751 self.assertEqual(self.dummy_client.response_code, 200)
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2752 json_dict = json.loads(b2s(results))
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2753
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2754 self.assertEqual(json_dict['data']['data'], 'Joe Doe')
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2755 self.assertEqual(json_dict['data']['link'],
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2756 "http://tracker.example/cgi-bin/"
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2757 "roundup.cgi/bugs/rest/data/user/3/realname")
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2758 self.assertIn(json_dict['data']['type'], ("<class 'str'>",
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2759 "<type 'str'>"))
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2760 self.assertEqual(json_dict['data']["id"], "3")
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2761 del(self.headers)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2762
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2763
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2764 # TEST #4
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2765 # PATCH: joe's email address with json
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2766 # save address so we can use it later
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2767 stored_results = self.server.get_element('user', self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2768 self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2769 self.assertEqual(self.dummy_client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2770
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2771 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2772 self.db.config['WEB_SECRET_KEY'])
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2773 etagb = etag.strip ('"')
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2774 body=s2b('{ "address": "demo2@example.com", "@etag": "\\"%s\\""}'%etagb)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2775 env = { "CONTENT_TYPE": "application/json",
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2776 "CONTENT_LENGTH": len(body),
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2777 "REQUEST_METHOD": "PATCH"
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2778 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2779 self.server.client.env.update(env)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2780 headers={"accept": "application/json",
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2781 "content-type": env['CONTENT_TYPE'],
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2782 "content-length": len(body)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2783 }
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2784 self.headers=headers
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2785 body_file=BytesIO(body) # FieldStorage needs a file
5656
d26d2590cd8c Implement different workaround for https://bugs.python.org/issue27777
John Rouillard <rouilj@ieee.org>
parents: 5655
diff changeset
2786 form = client.BinaryFieldStorage(body_file,
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2787 headers=headers,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2788 environ=env)
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
2789 self.server.client.request.headers.get=self.get_header
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2790 results = self.server.dispatch('PATCH',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2791 "/rest/data/user/%s"%self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2792 form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2793
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2794 self.assertEqual(self.server.client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2795 results = self.server.get_element('user', self.joeid, self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2796 self.assertEqual(self.dummy_client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2797 self.assertEqual(results['data']['attributes']['address'],
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2798 'demo2@example.com')
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2799
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2800 # and set it back reusing env and headers from last test
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2801 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2802 self.db.config['WEB_SECRET_KEY'])
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2803 etagb = etag.strip ('"')
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2804 body=s2b('{ "address": "%s", "@etag": "\\"%s\\""}'%(
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2805 stored_results['data']['attributes']['address'],
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
2806 etagb))
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2807 # reuse env and headers from prior test.
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2808 body_file=BytesIO(body) # FieldStorage needs a file
5656
d26d2590cd8c Implement different workaround for https://bugs.python.org/issue27777
John Rouillard <rouilj@ieee.org>
parents: 5655
diff changeset
2809 form = client.BinaryFieldStorage(body_file,
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2810 headers=headers,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2811 environ=env)
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
2812 self.server.client.request.headers.get=self.get_header
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2813 results = self.server.dispatch('PATCH',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2814 "/rest/data/user/%s"%self.joeid,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2815 form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2816
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2817 self.assertEqual(self.server.client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2818 results = self.server.get_element('user', self.joeid, self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2819 self.assertEqual(self.dummy_client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2820 self.assertEqual(results['data']['attributes']['address'],
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2821 'random@home.org')
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2822 del(self.headers)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2823
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2824 # TEST #5
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2825 # POST: create new issue
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2826 # no etag needed
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2827 etag = "not needed"
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2828 body=b'{ "title": "foo bar", "priority": "critical" }'
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2829 env = { "CONTENT_TYPE": "application/json",
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2830 "CONTENT_LENGTH": len(body),
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2831 "REQUEST_METHOD": "POST"
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2832 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2833 self.server.client.env.update(env)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2834 headers={"accept": "application/json",
5655
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2835 "content-type": env['CONTENT_TYPE'],
207e0f5d551c Merge in non-conflicting changes from ba67e397f063
John Rouillard <rouilj@ieee.org>
parents: 5651 5653
diff changeset
2836 "content-length": len(body)
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2837 }
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2838 self.headers=headers
5651
a02ef29b4242 Fix REST tests for Python 3.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5650
diff changeset
2839 body_file=BytesIO(body) # FieldStorage needs a file
5656
d26d2590cd8c Implement different workaround for https://bugs.python.org/issue27777
John Rouillard <rouilj@ieee.org>
parents: 5655
diff changeset
2840 form = client.BinaryFieldStorage(body_file,
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2841 headers=headers,
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2842 environ=env)
5650
e8ca7072c629 Fix Python 3 issues in REST code.
Joseph Myers <jsm@polyomino.org.uk>
parents: 5647
diff changeset
2843 self.server.client.request.headers.get=self.get_header
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2844 results = self.server.dispatch('POST',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2845 "/rest/data/issue",
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2846 form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2847
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2848 self.assertEqual(self.server.client.response_code, 201)
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2849 json_dict = json.loads(b2s(results))
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2850 issue_id=json_dict['data']['id']
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2851 results = self.server.get_element('issue',
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2852 str(issue_id), # must be a string not unicode
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2853 self.empty_form)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2854 self.assertEqual(self.dummy_client.response_code, 200)
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2855 self.assertEqual(results['data']['attributes']['title'],
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
2856 'foo bar')
5653
ba67e397f063 Fix string/bytes issues under python 3.
John Rouillard <rouilj@ieee.org>
parents: 5647
diff changeset
2857
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2858 # TEST #6
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2859 # POST: an invalid class
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2860 # no etag needed
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2861 etag = "not needed"
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2862 body=b'{ "title": "foo bar", "priority": "critical" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2863 env = { "CONTENT_TYPE": "application/json",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2864 "CONTENT_LENGTH": len(body),
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2865 "REQUEST_METHOD": "POST"
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2866 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2867 self.server.client.env.update(env)
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2868 headers={"accept": "application/json",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2869 "content-type": env['CONTENT_TYPE'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2870 "content-length": len(body)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2871 }
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2872 self.headers=headers
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2873 body_file=BytesIO(body) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2874 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2875 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
2876 environ=env)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2877 self.server.client.request.headers.get=self.get_header
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2878 results = self.server.dispatch('POST',
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2879 "/rest/data/nonissue",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2880 form)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2881
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2882 self.assertEqual(self.server.client.response_code, 404)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2883 json_dict = json.loads(b2s(results))
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2884 status=json_dict['error']['status']
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2885 msg=json_dict['error']['msg']
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2886 self.assertEqual(status, 404)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2887 self.assertEqual(msg, 'Class nonissue not found')
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2888
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2889 # TEST #7
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2890 # POST: status without key field of name
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2891 # also test that version spec in accept header is accepted
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2892 # no etag needed
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2893 etag = "not needed"
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2894 body=b'{ "order": 5 }'
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2895 env = { "CONTENT_TYPE": "application/json",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2896 "CONTENT_LENGTH": len(body),
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2897 "REQUEST_METHOD": "POST"
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2898 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2899 self.server.client.env.update(env)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2900 headers={"accept": "application/json; version=1",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2901 "content-type": env['CONTENT_TYPE'],
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2902 "content-length": len(body)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2903 }
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2904 self.headers=headers
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2905 body_file=BytesIO(body) # FieldStorage needs a file
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2906 form = client.BinaryFieldStorage(body_file,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2907 headers=headers,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2908 environ=env)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2909 self.server.client.request.headers.get=self.get_header
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2910 self.db.setCurrentUser('admin') # must be admin to create status
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2911 results = self.server.dispatch('POST',
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2912 "/rest/data/status",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2913 form)
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2914 self.server.client.env.update(env)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2915 self.assertEqual(self.server.client.response_code, 400)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2916 json_dict = json.loads(b2s(results))
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2917 status=json_dict['error']['status']
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2918 msg=json_dict['error']['msg']
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2919 self.assertEqual(status, 400)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2920 self.assertEqual(msg, "Must provide the 'name' property.")
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2921
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2922
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2923 # TEST #8
5984
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2924 # DELETE: delete issue 1 also test return type by extension
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2925 # test bogus extension as well.
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2926 etag = calculate_etag(self.db.issue.getnode("1"),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
2927 self.db.config['WEB_SECRET_KEY'])
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2928 etagb = etag.strip ('"')
8218
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2929 env = {"REQUEST_METHOD": "DELETE" }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2930 self.server.client.env.update(env)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2931 # use text/plain header and request json output by appending
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2932 # .json to the url.
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2933 headers={"accept": "text/plain",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2934 "if-match": '"%s"'%etagb,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2935 "content-length": 0,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2936 }
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2937 self.headers=headers
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2938 self.server.client.request.headers.get=self.get_header
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2939 self.db.setCurrentUser('admin') # must be admin to delete issue
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2940 results = self.server.dispatch('DELETE',
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2941 "/rest/data/issue/1.json",
8218
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
2942 self.empty_form)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2943 self.assertEqual(self.server.client.response_code, 200)
5984
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2944 print(results)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2945 json_dict = json.loads(b2s(results))
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2946 status=json_dict['data']['status']
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2947 self.assertEqual(status, 'ok')
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
2948
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2949 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
5986
8b88fb789208 still trying to get testing to pass.
John Rouillard <rouilj@ieee.org>
parents: 5985
diff changeset
2950 results = self.server.dispatch('GET',
8b88fb789208 still trying to get testing to pass.
John Rouillard <rouilj@ieee.org>
parents: 5985
diff changeset
2951 "/rest/data/issuetitle:=asdf.jon",
5984
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2952 form)
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2953 self.assertEqual(self.server.client.response_code, 406)
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2954 print(results)
5985
f1191a470598 Fix test to account for mixxing dicttoxml.
John Rouillard <rouilj@ieee.org>
parents: 5984
diff changeset
2955 try: # only verify local copy not system installed copy
f1191a470598 Fix test to account for mixxing dicttoxml.
John Rouillard <rouilj@ieee.org>
parents: 5984
diff changeset
2956 from roundup.dicttoxml import dicttoxml
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2957 includexml = ', xml'
5985
f1191a470598 Fix test to account for mixxing dicttoxml.
John Rouillard <rouilj@ieee.org>
parents: 5984
diff changeset
2958 except ImportError:
f1191a470598 Fix test to account for mixxing dicttoxml.
John Rouillard <rouilj@ieee.org>
parents: 5984
diff changeset
2959 includexml = ''
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2960
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2961 json_dict = json.loads(b2s(results))
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2962 response= ("Content type 'jon' requested in URL is not available.\n"
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2963 "Acceptable types: json%s\n") % includexml
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2964 self.assertEqual(json_dict['error']['msg'], response)
5984
25a813415d59 issue2551069 - when unsupported type is found report type
John Rouillard <rouilj@ieee.org>
parents: 5937
diff changeset
2965
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
2966 # TEST #9
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2967 # GET: test that version can be set with accept:
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2968 # ... ; version=z
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2969 # or
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2970 # application/vnd.x.y-vz+json
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2971 # or
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2972 # @apiver
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
2973 # simulate: /rest/data/issue
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2974 form = cgi.FieldStorage()
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2975 form.list = [
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2976 cgi.MiniFieldStorage('@apiver', 'L'),
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2977 ]
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2978
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2979 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
2980
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2981 headers={"accept": "application/json; notversion=z" }
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2982 self.headers=headers
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2983 self.server.client.request.headers.get=self.get_header
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2984 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2985 "/rest/data/issue/1", form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
2986 print("9a: " + b2s(results))
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2987 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2988 # note bad @apiver returns 400 not 406.
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
2989 self.assertEqual(json_dict['error']['status'], 406)
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2990 self.assertEqual(json_dict['error']['msg'],
6513
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
2991 "Unrecognized api version: L. See /rest without "
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
2992 "specifying api version for supported versions.")
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2993
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
2994 headers={"accept": "application/json; version=z" }
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
2995 self.headers=headers
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
2996 self.server.client.request.headers.get=self.get_header
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
2997 results = self.server.dispatch('GET',
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
2998 "/rest/data/issue/1", form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
2999 print("9b: " + b2s(results))
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3000 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3001 self.assertEqual(self.server.client.response_code, 406)
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3002 self.assertEqual(json_dict['error']['status'], 406)
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3003 self.assertEqual(json_dict['error']['msg'],
6513
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
3004 "Unrecognized api version: z. See /rest without "
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
3005 "specifying api version for supported versions.")
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3006
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3007 headers={"accept": "application/vnd.roundup.test-vz+json" }
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3008 self.headers=headers
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3009 self.server.client.request.headers.get=self.get_header
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3010 results = self.server.dispatch('GET',
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3011 "/rest/data/issue/1", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3012 print("9c:" + b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3013 self.assertEqual(self.server.client.response_code, 406)
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3014 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3015 self.assertEqual(json_dict['error']['status'], 406)
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3016 self.assertEqual(json_dict['error']['msg'],
6513
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
3017 "Unrecognized api version: z. See /rest without "
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
3018 "specifying api version for supported versions.")
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3019
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3020 # verify that version priority is correct; should be version=...
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3021 headers={"accept": "application/vnd.roundup.test-vz+json; version=a"
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3022 }
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3023 self.headers=headers
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3024 self.server.client.request.headers.get=self.get_header
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3025 results = self.server.dispatch('GET',
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3026 "/rest/data/issue/1", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3027 print("9d: " + b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3028 self.assertEqual(self.server.client.response_code, 406)
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3029 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3030 self.assertEqual(json_dict['error']['status'], 406)
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3031 self.assertEqual(json_dict['error']['msg'],
6513
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
3032 "Unrecognized api version: a. See /rest without "
1d6c986b3f72 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6512
diff changeset
3033 "specifying api version for supported versions.")
5741
9c2e51aae18a Test to make sure version selection via accept header is parsed correctly.
John Rouillard <rouilj@ieee.org>
parents: 5734
diff changeset
3034
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3035 # TEST #10
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3036 # check /rest and /rest/summary and /rest/notthere
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3037 expected_rest = {
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3038 "data": {
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3039 "supported_versions": [
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3040 1
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3041 ],
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3042 "default_version": 1,
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3043 "links": [
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3044 {
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3045 "rel": "self",
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3046 "uri": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest"
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3047 },
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3048 {
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3049 "rel": "data",
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3050 "uri": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data"
6525
c505c774a94d Mutiple changes to REST code.
John Rouillard <rouilj@ieee.org>
parents: 6513
diff changeset
3051 },
c505c774a94d Mutiple changes to REST code.
John Rouillard <rouilj@ieee.org>
parents: 6513
diff changeset
3052 {
c505c774a94d Mutiple changes to REST code.
John Rouillard <rouilj@ieee.org>
parents: 6513
diff changeset
3053 "rel": "summary",
c505c774a94d Mutiple changes to REST code.
John Rouillard <rouilj@ieee.org>
parents: 6513
diff changeset
3054 "uri": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/summary"
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3055 }
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3056 ]
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3057 }
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3058 }
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3059
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3060 self.headers={}
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3061 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3062 "/rest", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3063 print("10a: " + b2s(results))
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3064 self.assertEqual(self.server.client.response_code, 200)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3065 results_dict = json.loads(b2s(results))
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3066 self.assertEqual(results_dict, expected_rest)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3067
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3068
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3069 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3070 "/rest/", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3071 print("10b: " + b2s(results))
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3072 self.assertEqual(self.server.client.response_code, 200)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3073 results_dict = json.loads(b2s(results))
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3074 self.assertEqual(results_dict, expected_rest)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3075
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3076 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3077 "/rest/summary", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3078 print("10c: " + b2s(results))
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3079 self.assertEqual(self.server.client.response_code, 200)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3080
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3081 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3082 "/rest/summary/", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3083 print("10d: " + b2s(results))
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3084 self.assertEqual(self.server.client.response_code, 200)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3085
5746
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3086 expected_data = {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3087 "data": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3088 "issue": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3089 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/issue"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3090 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3091 "priority": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3092 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/priority"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3093 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3094 "user": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3095 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/user"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3096 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3097 "query": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3098 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/query"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3099 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3100 "status": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3101 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/status"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3102 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3103 "keyword": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3104 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/keyword"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3105 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3106 "msg": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3107 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/msg"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3108 },
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3109 "file": {
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3110 "link": "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/file"
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3111 }
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3112 }
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3113 }
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3114
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3115 results = self.server.dispatch('GET',
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3116 "/rest/data", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3117 print("10e: " + b2s(results))
5746
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3118 self.assertEqual(self.server.client.response_code, 200)
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3119 results_dict = json.loads(b2s(results))
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3120 self.assertEqual(results_dict, expected_data)
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3121
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3122 results = self.server.dispatch('GET',
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3123 "/rest/data/", self.empty_form)
5987
ea3485c67f94 Sort accept keys to make p2 and p3 ordering the same
John Rouillard <rouilj@ieee.org>
parents: 5986
diff changeset
3124 print("10f: " + b2s(results))
5746
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3125 self.assertEqual(self.server.client.response_code, 200)
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3126 results_dict = json.loads(b2s(results))
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3127 self.assertEqual(results_dict, expected_data)
fea2b6e54492 Check /rest/data.
John Rouillard <rouilj@ieee.org>
parents: 5744
diff changeset
3128
5742
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3129 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3130 "/rest/notthere", self.empty_form)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3131 self.assertEqual(self.server.client.response_code, 404)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3132
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3133 results = self.server.dispatch('GET',
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3134 "/rest/notthere/", self.empty_form)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3135 self.assertEqual(self.server.client.response_code, 404)
97d7faebef0a Test @apiver version parsing using bogus version. Test /rest, /rest/
John Rouillard <rouilj@ieee.org>
parents: 5741
diff changeset
3136
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3137 del(self.headers)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3138
8182
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3139 # TEST #11
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3140 # GET: test that /binary_content can be downloaded
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3141 form = cgi.FieldStorage()
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3142
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3143 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3144
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3145 headers={"accept": "*/*" }
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3146 self.headers=headers
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3147 self.server.client.request.headers.get=self.get_header
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3148 results = self.server.dispatch('GET',
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3149 "/rest/data/file/1/binary_content", form)
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3150
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3151 self.assertEqual(results, b'PNG\x01abcdefghi\njklmnop')
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3152 self.assertEqual(self.server.client.additional_headers['Content-Type'],
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3153 'image/png')
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3154 self.assertNotIn("ETag", self.server.client.additional_headers)
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3155 self.assertEqual(
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3156 self.server.client.additional_headers["X-Content-Type-Options"],
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3157 "nosniff")
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3158
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3159 print("11: " + b2s(results))
5fcc1a379564 feat: add test download via /binary_content via dispatch - issue2551068
John Rouillard <rouilj@ieee.org>
parents: 8180
diff changeset
3160
5744
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3161 def testAcceptHeaderParsing(self):
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3162 self.server.client.env['REQUEST_METHOD'] = 'GET'
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3163
5744
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3164 # TEST #1
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3165 # json highest priority
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3166 self.server.client.request.headers.get=self.get_header
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3167 headers={"accept": "application/json; version=1,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3168 "application/xml; q=0.5; version=2,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3169 "text/plain; q=0.75; version=2"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3170 }
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3171 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3172 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3173 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3174 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3175 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3176 self.assertEqual(self.server.client.response_code, 200)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3177 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3178 "application/json")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3179
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3180 # TEST #2
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3181 # text highest priority
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3182 headers={"accept": "application/json; q=0.5; version=1,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3183 "application/xml; q=0.25; version=2,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3184 "text/plain; q=1.0; version=3"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3185 }
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3186 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3187 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3188 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3189 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3190 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3191 self.assertEqual(self.server.client.response_code, 200)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3192 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3193 "application/json")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3194
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3195 # TEST #3
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3196 # no acceptable type
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3197 headers={"accept": "text/plain; q=1.0; version=2"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3198 }
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3199 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3200 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3201 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3202 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3203 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3204 self.assertEqual(self.server.client.response_code, 406)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3205 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3206 "application/json")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3207
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3208 # TEST #4
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3209 # no accept header, should use default json
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3210 headers={}
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3211 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3212 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3213 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3214 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3215 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3216 self.assertEqual(self.server.client.response_code, 200)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3217 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3218 "application/json")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3219
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3220 # TEST #5
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3221 # wildcard accept header, should use default json
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3222 headers={ "accept": "*/*"}
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3223 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3224 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3225 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3226 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3227 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3228 self.assertEqual(self.server.client.response_code, 200)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3229 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3230 "application/json")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3231
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3232 # TEST #6
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3233 # invalid q factor if not ignored/demoted
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3234 # application/json is selected with invalid version
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3235 # and errors.
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3236 # this ends up choosing */* which triggers json.
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3237 self.server.client.request.headers.get=self.get_header
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3238 headers={"accept": "application/json; q=1.5; version=99,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3239 "*/*; q=0.9; version=1,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3240 "text/plain; q=3.75; version=2"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3241 }
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3242 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3243 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3244 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3245 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3246 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3247 self.assertEqual(self.server.client.response_code, 200)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3248 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3249 "application/json")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3250
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3251
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3252 '''
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3253 # only works if dicttoxml.py is installed.
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3254 # not installed for testing
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3255 # TEST #7
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3256 # xml wins
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3257 headers={"accept": "application/json; q=0.5; version=2,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3258 "application/xml; q=0.75; version=1,"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3259 "text/plain; q=1.0; version=2"
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3260 }
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3261 self.headers=headers
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3262 results = self.server.dispatch('GET',
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3263 "/rest/data/status/1",
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3264 self.empty_form)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3265 print(results)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3266 self.assertEqual(self.server.client.response_code, 200)
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3267 self.assertEqual(self.server.client.additional_headers['Content-Type'],
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3268 "application/xml")
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3269 '''
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3270
6316
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3271 # TEST #8
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3272 # invalid api version
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3273 # application/json is selected with invalid version
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3274 self.server.client.request.headers.get=self.get_header
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3275 headers={"accept": "application/json; version=99"
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3276 }
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3277 self.headers=headers
6509
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3278 results = self.server.dispatch('GET',
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3279 "/rest/data/status/1",
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3280 self.empty_form)
6316
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3281 print(results)
6509
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3282 json_dict = json.loads(b2s(results))
8177
2967f37e73e4 refactor: issue2551289. invalid REST Accept header stops request
John Rouillard <rouilj@ieee.org>
parents: 8094
diff changeset
3283 self.assertEqual(self.server.client.response_code, 406)
6509
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3284 self.assertEqual(self.server.client.additional_headers['Content-Type'],
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3285 "application/json")
1fc765ef6379 Fix 204 responses, hangs and crashes with REST.
John Rouillard <rouilj@ieee.org>
parents: 6366
diff changeset
3286 self.assertEqual(json_dict['error']['msg'],
6512
b54bb529d701 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6509
diff changeset
3287 "Unrecognized api version: 99. See /rest "
b54bb529d701 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6509
diff changeset
3288 "without specifying api version for "
b54bb529d701 Fix test.
John Rouillard <rouilj@ieee.org>
parents: 6509
diff changeset
3289 "supported versions.")
6316
323661f7c89c Test handling of invalid API version using Accept header.
John Rouillard <rouilj@ieee.org>
parents: 6314
diff changeset
3290
5743
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3291 def testMethodOverride(self):
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3292 # TEST #1
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3293 # Use GET, PUT, PATCH to tunnel DELETE expect error
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3294
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3295 body=b'{ "order": 5 }'
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3296 env = { "CONTENT_TYPE": "application/json",
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3297 "CONTENT_LENGTH": len(body),
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3298 "REQUEST_METHOD": "POST"
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3299 }
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3300 self.server.client.request.headers.get=self.get_header
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3301 for method in ( "GET", "PUT", "PATCH" ):
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3302 headers={"accept": "application/json; version=1",
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3303 "content-type": env['CONTENT_TYPE'],
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3304 "content-length": len(body),
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3305 "x-http-method-override": "DElETE",
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3306 }
8218
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
3307 body_file=BytesIO(body) # FieldStorage needs a file
32aaf5dc562b fix(REST): issue2551383; improve errors for bad json, fix PUT docs
John Rouillard <rouilj@ieee.org>
parents: 8182
diff changeset
3308
5743
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3309 self.headers=headers
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3310 form = client.BinaryFieldStorage(body_file,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3311 headers=headers,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3312 environ=env)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3313 self.db.setCurrentUser('admin') # must be admin to create status
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3314
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3315 self.server.client.env.update({'REQUEST_METHOD': method})
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3316
5743
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3317 results = self.server.dispatch(method,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3318 "/rest/data/status",
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3319 form)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3320
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3321 self.assertEqual(self.server.client.response_code, 400)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3322 json_dict = json.loads(b2s(results))
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3323 status=json_dict['error']['status']
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3324 msg=json_dict['error']['msg']
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3325 self.assertEqual(status, 400)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3326 self.assertEqual(msg, "X-HTTP-Method-Override: DElETE must be "
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3327 "used with POST method not %s."%method)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3328
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3329 # TEST #2
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3330 # DELETE: delete issue 1 via post tunnel
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3331 self.assertFalse(self.db.status.is_retired("1"))
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3332 etag = calculate_etag(self.db.status.getnode("1"),
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3333 self.db.config['WEB_SECRET_KEY'])
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3334 etagb = etag.strip ('"')
5744
d4de45cde106 Accept header parsing fixes. Now return first acceptable match rather
John Rouillard <rouilj@ieee.org>
parents: 5743
diff changeset
3335 headers={"accept": "application/json; q=1.0, application/xml; q=0.75",
5743
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3336 "if-match": '"%s"'%etagb,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3337 "content-length": 0,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3338 "x-http-method-override": "DElETE"
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3339 }
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3340 self.headers=headers
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3341 body_file=BytesIO(b'') # FieldStorage needs a file
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3342 form = client.BinaryFieldStorage(body_file,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3343 headers=headers,
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3344 environ=env)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3345 self.server.client.request.headers.get=self.get_header
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3346 self.db.setCurrentUser('admin') # must be admin to delete issue
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3347 self.server.client.env.update({'REQUEST_METHOD': 'POST'})
5743
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3348 results = self.server.dispatch('POST',
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3349 "/rest/data/status/1",
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3350 form)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3351 print(results)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3352 self.assertEqual(self.server.client.response_code, 200)
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3353 json_dict = json.loads(b2s(results))
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3354 status=json_dict['data']['status']
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3355 self.assertEqual(status, 'ok')
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3356 self.assertTrue(self.db.status.is_retired("1"))
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3357
60299cd36670 Basic tests for tunneling of methods via x-http-method-override.
John Rouillard <rouilj@ieee.org>
parents: 5742
diff changeset
3358
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3359 def testPostPOE(self):
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3360 ''' test post once exactly: get POE url, create issue
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3361 using POE url. Use dispatch entry point.
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3362 '''
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3363 import time
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3364 # setup environment
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3365 etag = "not needed"
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3366 empty_body=b''
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3367 env = { "CONTENT_TYPE": "application/json",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3368 "CONTENT_LENGTH": len(empty_body),
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3369 "REQUEST_METHOD": "POST"
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3370 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3371 self.server.client.env.update(env)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3372
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3373 headers={"accept": "application/json",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3374 "content-type": env['CONTENT_TYPE'],
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3375 "content-length": len(empty_body)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3376 }
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3377 self.headers=headers
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3378 # use empty_body to test code path for missing/empty json
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3379 body_file=BytesIO(empty_body) # FieldStorage needs a file
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3380 form = client.BinaryFieldStorage(body_file,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3381 headers=headers,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3382 environ=env)
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3383
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3384 ## Obtain the POE url.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3385 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3386 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3387 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3388 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3389
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3390 self.assertEqual(self.server.client.response_code, 200)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3391 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3392 url=json_dict['data']['link']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3393
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3394 # strip tracker web prefix leaving leading /.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3395 url = url[len(self.db.config['TRACKER_WEB'])-1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3396
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3397 ## create an issue using poe url.
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3398 body=b'{ "title": "foo bar", "priority": "critical" }'
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3399 env = { "CONTENT_TYPE": "application/json",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3400 "CONTENT_LENGTH": len(body),
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3401 "REQUEST_METHOD": "POST"
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3402 }
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3403 self.server.client.env.update(env)
5711
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3404 headers={"accept": "application/json",
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3405 "content-type": env['CONTENT_TYPE'],
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3406 "content-length": len(body)
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3407 }
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3408 self.headers=headers
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3409 body_file=BytesIO(body) # FieldStorage needs a file
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3410 form = client.BinaryFieldStorage(body_file,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3411 headers=headers,
aea2cc142c1b Added some more rest testing and make sure api version is valid.
John Rouillard <rouilj@ieee.org>
parents: 5710
diff changeset
3412 environ=env)
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3413 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3414 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3415 url,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3416 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3417
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3418 self.assertEqual(self.server.client.response_code, 201)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3419 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3420 issue_id=json_dict['data']['id']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3421 results = self.server.get_element('issue',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3422 str(issue_id), # must be a string not unicode
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3423 self.empty_form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3424 self.assertEqual(self.dummy_client.response_code, 200)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3425 self.assertEqual(results['data']['attributes']['title'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3426 'foo bar')
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3427
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3428 ## Reuse POE url. It will fail.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3429 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3430 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3431 url,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3432 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3433 # get the last component stripping the trailing /
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3434 poe=url[url.rindex('/')+1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3435 self.assertEqual(self.server.client.response_code, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3436 results = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3437 self.assertEqual(results['error']['status'], 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3438 self.assertEqual(results['error']['msg'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3439 "POE token \'%s\' not valid."%poe)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3440
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3441 ## Try using GET on POE url. Should fail with method not
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3442 ## allowed (405)
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3443 self.server.client.env.update({'REQUEST_METHOD': 'GET'})
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3444 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3445 results = self.server.dispatch('GET',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3446 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3447 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3448 self.assertEqual(self.server.client.response_code, 405)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3449
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3450
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3451 ## Try creating generic POE url.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3452 body_poe=b'{"generic": "null", "lifetime": "100" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3453 body_file=BytesIO(body_poe) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3454 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3455 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3456 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3457 self.server.client.request.headers.get=self.get_header
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
3458 self.server.client.env.update({'REQUEST_METHOD': 'POST'})
5710
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3459 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3460 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3461 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3462 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3463 url=json_dict['data']['link']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3464
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3465 # strip tracker web prefix leaving leading /.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3466 url = url[len(self.db.config['TRACKER_WEB'])-1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3467 url = url.replace('/issue/', '/keyword/')
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3468
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3469 body_keyword=b'{"name": "keyword"}'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3470 body_file=BytesIO(body_keyword) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3471 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3472 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3473 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3474 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3475 url,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3476 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3477 self.assertEqual(self.server.client.response_code, 201)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3478 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3479 url=json_dict['data']['link']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3480 id=json_dict['data']['id']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3481 self.assertEqual(id, "1")
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3482 self.assertEqual(url, "http://tracker.example/cgi-bin/roundup.cgi/bugs/rest/data/keyword/1")
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3483
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3484 ## Create issue POE url and try to use for keyword.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3485 ## This should fail.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3486 body_poe=b'{"lifetime": "100" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3487 body_file=BytesIO(body_poe) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3488 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3489 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3490 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3491 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3492 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3493 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3494 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3495 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3496 url=json_dict['data']['link']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3497
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3498 # strip tracker web prefix leaving leading /.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3499 url = url[len(self.db.config['TRACKER_WEB'])-1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3500 url = url.replace('/issue/', '/keyword/')
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3501
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3502 body_keyword=b'{"name": "keyword"}'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3503 body_file=BytesIO(body_keyword) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3504 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3505 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3506 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3507 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3508 url,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3509 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3510 poe=url[url.rindex('/')+1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3511 self.assertEqual(self.server.client.response_code, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3512 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3513 stat=json_dict['error']['status']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3514 msg=json_dict['error']['msg']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3515 self.assertEqual(stat, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3516 self.assertEqual(msg, "POE token '%s' not valid for keyword, was generated for class issue"%poe)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3517
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3518
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3519 ## Create POE with 10 minute lifetime and verify
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3520 ## expires is within 10 minutes.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3521 body_poe=b'{"lifetime": "30" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3522 body_file=BytesIO(body_poe) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3523 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3524 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3525 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3526 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3527 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3528 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3529 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3530 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3531 expires=int(json_dict['data']['expires'])
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3532 # allow up to 3 seconds between time stamp creation
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3533 # done under dispatch and this point.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3534 expected=int(time.time() + 30)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3535 print("expected=%d, expires=%d"%(expected,expires))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3536 self.assertTrue((expected - expires) < 3 and (expected - expires) >= 0)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3537
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3538
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3539 ## Use a token created above as joe by a different user.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3540 self.db.setCurrentUser('admin')
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3541 url=json_dict['data']['link']
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3542 # strip tracker web prefix leaving leading /.
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3543 url = url[len(self.db.config['TRACKER_WEB'])-1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3544 body_file=BytesIO(body_keyword) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3545 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3546 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3547 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3548 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3549 url,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3550 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3551 print(results)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3552 self.assertEqual(self.server.client.response_code, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3553 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3554 # get the last component stripping the trailing /
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3555 poe=url[url.rindex('/')+1:]
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3556 self.assertEqual(json_dict['error']['msg'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3557 "POE token '%s' not valid."%poe)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3558
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3559 ## Create POE with bogus lifetime
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3560 body_poe=b'{"lifetime": "10.2" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3561 body_file=BytesIO(body_poe) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3562 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3563 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3564 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3565 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3566 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3567 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3568 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3569 self.assertEqual(self.server.client.response_code, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3570 print(results)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3571 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3572 self.assertEqual(json_dict['error']['msg'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3573 "Value \'lifetime\' must be an integer specify "
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3574 "lifetime in seconds. Got 10.2.")
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3575
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3576 ## Create POE with lifetime > 1 hour
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3577 body_poe=b'{"lifetime": "3700" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3578 body_file=BytesIO(body_poe) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3579 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3580 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3581 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3582 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3583 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3584 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3585 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3586 self.assertEqual(self.server.client.response_code, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3587 print(results)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3588 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3589 self.assertEqual(json_dict['error']['msg'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3590 "Value 'lifetime' must be between 1 second and 1 "
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3591 "hour (3600 seconds). Got 3700.")
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3592
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3593 ## Create POE with lifetime < 1 second
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3594 body_poe=b'{"lifetime": "-1" }'
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3595 body_file=BytesIO(body_poe) # FieldStorage needs a file
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3596 form = client.BinaryFieldStorage(body_file,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3597 headers=headers,
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3598 environ=env)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3599 self.server.client.request.headers.get=self.get_header
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3600 results = self.server.dispatch('POST',
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3601 "/rest/data/issue/@poe",
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3602 form)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3603 self.assertEqual(self.server.client.response_code, 400)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3604 print(results)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3605 json_dict = json.loads(b2s(results))
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3606 self.assertEqual(json_dict['error']['msg'],
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3607 "Value 'lifetime' must be between 1 second and 1 "
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3608 "hour (3600 seconds). Got -1.")
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3609 del(self.headers)
0b79bfcb3312 Add support for making an idempotent POST. This allows retrying a POST
John Rouillard <rouilj@ieee.org>
parents: 5708
diff changeset
3610
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3611 def testPutElement(self):
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3612 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3613 Change joe's 'realname'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3614 Check if we can't change admin's detail
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3615 """
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3616 # fail to change Joe's realname via attribute uri
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3617 # no etag
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3618 form = cgi.FieldStorage()
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3619 form.list = [
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3620 cgi.MiniFieldStorage('data', 'Joe Doe Doe')
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3621 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3622 results = self.server.put_attribute(
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3623 'user', self.joeid, 'realname', form
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3624 )
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3625 self.assertEqual(self.dummy_client.response_code, 412)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3626 results = self.server.get_attribute(
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3627 'user', self.joeid, 'realname', self.empty_form
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3628 )
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3629 self.assertEqual(self.dummy_client.response_code, 200)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3630 self.assertEqual(results['data']['data'], 'Joe Random')
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3631
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
3632 # change Joe's realname via attribute uri - etag in header
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3633 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3634 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3635 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3636 form.list = [
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3637 cgi.MiniFieldStorage('data', 'Joe Doe Doe'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3638 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3639
5674
6dc4dba1c225 REST: Use If-Match header for incoming requests
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5673
diff changeset
3640 self.headers = {'if-match': etag } # use etag in header
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3641 results = self.server.put_attribute(
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3642 'user', self.joeid, 'realname', form
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3643 )
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3644 self.assertEqual(self.dummy_client.response_code, 200)
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3645 results = self.server.get_attribute(
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3646 'user', self.joeid, 'realname', self.empty_form
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3647 )
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3648 self.assertEqual(self.dummy_client.response_code, 200)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3649 self.assertEqual(results['data']['data'], 'Joe Doe Doe')
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
3650 del(self.headers)
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3651
5643
a60cbbcc9309 Added support for accepting application/json payload in addition to
John Rouillard <rouilj@ieee.org>
parents: 5639
diff changeset
3652 # Reset joe's 'realname'. etag in body
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3653 # Also try to set protected items. The protected items should
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3654 # be ignored on put_element to make it easy to get the item
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3655 # with all fields, change one field and put the result without
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3656 # having to filter out protected items.
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3657 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3658 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3659 self.db.config['WEB_SECRET_KEY'])
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3660 form.list = [
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3661 cgi.MiniFieldStorage('creator', '3'),
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3662 cgi.MiniFieldStorage('realname', 'Joe Doe'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3663 cgi.MiniFieldStorage('@etag', etag)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3664 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3665 results = self.server.put_element('user', self.joeid, form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3666 self.assertEqual(self.dummy_client.response_code, 200)
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3667 results = self.server.get_element('user', self.joeid, self.empty_form)
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3668 self.assertEqual(self.dummy_client.response_code, 200)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3669 self.assertEqual(results['data']['attributes']['realname'], 'Joe Doe')
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3670
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3671 # We are joe, so check we can't change admin's details
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3672 results = self.server.put_element('user', '1', form)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3673 self.assertEqual(self.dummy_client.response_code, 403)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3674 self.assertEqual(results['error']['status'], 403)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3675
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3676 # Try to reset joe's 'realname' and add a broken prop.
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3677 # This should result in no change to the name and
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3678 # a 400 UsageError stating prop does not exist.
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3679 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3680 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3681 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3682 form.list = [
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3683 cgi.MiniFieldStorage('JustKidding', '3'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3684 cgi.MiniFieldStorage('realname', 'Joe Doe'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3685 cgi.MiniFieldStorage('@etag', etag)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3686 ]
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3687 results = self.server.put_element('user', self.joeid, form)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3688 expected= {'error': {'status': 400,
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3689 'msg': UsageError('Property JustKidding not '
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3690 'found in class user')}}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3691 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3692 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3693 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3694 type(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3695 self.assertEqual(self.dummy_client.response_code, 400)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3696 results = self.server.get_element('user', self.joeid, self.empty_form)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3697 self.assertEqual(self.dummy_client.response_code, 200)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3698 self.assertEqual(results['data']['attributes']['realname'], 'Joe Doe')
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3699
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3700 def testPutAttribute(self):
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3701 # put protected property
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3702 # make sure we don't have permission issues
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3703 self.db.setCurrentUser('admin')
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3704 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3705 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3706 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3707 form.list = [
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3708 cgi.MiniFieldStorage('data', '3'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3709 cgi.MiniFieldStorage('@etag', etag)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3710 ]
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3711 results = self.server.put_attribute(
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3712 'user', self.joeid, 'creator', form
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3713 )
5707
f9a762678af6 Change some 400 errors to 405 (method not allowed) errors where user is
John Rouillard <rouilj@ieee.org>
parents: 5706
diff changeset
3714 expected= {'error': {'status': 405, 'msg':
f9a762678af6 Change some 400 errors to 405 (method not allowed) errors where user is
John Rouillard <rouilj@ieee.org>
parents: 5706
diff changeset
3715 AttributeError('\'"creator", "actor", "creation" and '
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3716 '"activity" are reserved\'')}}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3717 print(results)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3718 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3719 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3720 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3721 type(expected['error']['msg']))
5707
f9a762678af6 Change some 400 errors to 405 (method not allowed) errors where user is
John Rouillard <rouilj@ieee.org>
parents: 5706
diff changeset
3722 self.assertEqual(self.dummy_client.response_code, 405)
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3723
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3724 # put invalid property
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3725 # make sure we don't have permission issues
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3726 self.db.setCurrentUser('admin')
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3727 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3728 etag = calculate_etag(self.db.user.getnode(self.joeid),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3729 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3730 form.list = [
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3731 cgi.MiniFieldStorage('data', '3'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3732 cgi.MiniFieldStorage('@etag', etag)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3733 ]
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3734 results = self.server.put_attribute(
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3735 'user', self.joeid, 'youMustBeKiddingMe', form
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3736 )
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3737 expected= {'error': {'status': 400,
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3738 'msg': UsageError("'youMustBeKiddingMe' "
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3739 "is not a property of user")}}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3740 print(results)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3741 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3742 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3743 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3744 type(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3745 self.assertEqual(self.dummy_client.response_code, 400)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3746
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3747 def testPost(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3748 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3749 Post a new issue with title: foo
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3750 Verify the information of the created issue
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3751 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3752 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3753 form.list = [
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3754 cgi.MiniFieldStorage('title', 'foo')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3755 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3756 results = self.server.post_collection('issue', form)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3757 self.assertEqual(self.dummy_client.response_code, 201)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3758 issueid = results['data']['id']
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3759 results = self.server.get_element('issue', issueid, self.empty_form)
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3760 self.assertEqual(self.dummy_client.response_code, 200)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3761 self.assertEqual(results['data']['attributes']['title'], 'foo')
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3762 self.assertEqual(self.db.issue.get(issueid, "tx_Source"), 'web')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3763
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3764 def testPostFile(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3765 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3766 Post a new file with content: hello\r\nthere
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3767 Verify the information of the created file
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3768 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3769 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3770 form.list = [
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3771 cgi.MiniFieldStorage('content', 'hello\r\nthere')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3772 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3773 results = self.server.post_collection('file', form)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3774 self.assertEqual(self.dummy_client.response_code, 201)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3775 fileid = results['data']['id']
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3776 results = self.server.get_element('file', fileid, self.empty_form)
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3777 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3778 self.assertEqual(self.dummy_client.response_code, 200)
8180
d02ce1d14acd feat: issue2551068 - Provide way to retrieve file/msg data via rest endpoint.
John Rouillard <rouilj@ieee.org>
parents: 8177
diff changeset
3779 self.assertIn("link", results['attributes']['content'])
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3780
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3781 # File content is only shown with verbose=3
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3782 form = cgi.FieldStorage()
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3783 form.list = [
6317
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
3784 cgi.MiniFieldStorage('@verbose', '3'),
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
3785 cgi.MiniFieldStorage('@protected', 'true')
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3786 ]
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3787 results = self.server.get_element('file', fileid, form)
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3788 results = results['data']
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3789 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3790 self.assertEqual(results['attributes']['content'], 'hello\r\nthere')
6317
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
3791 self.assertIn('creator', results['attributes']) # added by @protected
ea0becc9fdb9 Test delete of class and use of @protected.
John Rouillard <rouilj@ieee.org>
parents: 6316
diff changeset
3792 self.assertEqual(results['attributes']['creator']['username'], "joe")
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3793
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3794 def testAuthDeniedPut(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3795 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3796 Test unauthorized PUT request
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3797 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3798 # Wrong permissions (caught by roundup security module).
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3799 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3800 form.list = [
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3801 cgi.MiniFieldStorage('realname', 'someone')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3802 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3803 results = self.server.put_element('user', '1', form)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3804 self.assertEqual(self.dummy_client.response_code, 403)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3805 self.assertEqual(results['error']['status'], 403)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3806
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3807 def testAuthDeniedPost(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3808 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3809 Test unauthorized POST request
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3810 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3811 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3812 form.list = [
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3813 cgi.MiniFieldStorage('username', 'blah')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3814 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3815 results = self.server.post_collection('user', form)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3816 self.assertEqual(self.dummy_client.response_code, 403)
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3817 self.assertEqual(results['error']['status'], 403)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3818
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3819 def testAuthAllowedPut(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3820 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3821 Test authorized PUT request
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3822 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3823 self.db.setCurrentUser('admin')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3824 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3825 form.list = [
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3826 cgi.MiniFieldStorage('realname', 'someone')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3827 ]
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3828 try:
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3829 self.server.put_element('user', '2', form)
5602
c40d04915e23 Python3 fixes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5601
diff changeset
3830 except Unauthorised as err:
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3831 self.fail('raised %s' % err)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3832 finally:
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3833 self.db.setCurrentUser('joe')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3834
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3835 def testAuthAllowedPost(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3836 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3837 Test authorized POST request
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3838 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3839 self.db.setCurrentUser('admin')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3840 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3841 form.list = [
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3842 cgi.MiniFieldStorage('username', 'blah')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3843 ]
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3844 try:
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3845 self.server.post_collection('user', form)
5602
c40d04915e23 Python3 fixes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5601
diff changeset
3846 except Unauthorised as err:
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3847 self.fail('raised %s' % err)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3848 finally:
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3849 self.db.setCurrentUser('joe')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3850
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3851 def testDeleteAttributeUri(self):
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3852 """
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3853 Test Delete an attribute
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3854 """
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3855 self.maxDiff = 4000
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3856 # create a new issue with userid 1 in the nosy list
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3857 issue_id = self.db.issue.create(title='foo', nosy=['1'])
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3858
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3859 # No etag, so this should return 412 - Precondition Failed
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3860 # With no changes
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3861 results = self.server.delete_attribute(
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3862 'issue', issue_id, 'nosy', self.empty_form
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3863 )
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3864 self.assertEqual(self.dummy_client.response_code, 412)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3865 results = self.server.get_element('issue', issue_id, self.empty_form)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3866 results = results['data']
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3867 self.assertEqual(self.dummy_client.response_code, 200)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3868 self.assertEqual(len(results['attributes']['nosy']), 1)
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3869 self.assertListEqual(results['attributes']['nosy'],
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3870 [{'id': '1', 'link': self.url_pfx + 'user/1'}])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3871
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3872 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3873 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3874 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3875 form.list.append(cgi.MiniFieldStorage('@etag', etag))
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3876 # remove the title and nosy
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3877 results = self.server.delete_attribute(
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3878 'issue', issue_id, 'title', form
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3879 )
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3880 self.assertEqual(self.dummy_client.response_code, 200)
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3881
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3882 del(form.list[-1])
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3883 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3884 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3885 form.list.append(cgi.MiniFieldStorage('@etag', etag))
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3886 results = self.server.delete_attribute(
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3887 'issue', issue_id, 'nosy', form
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3888 )
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3889 self.assertEqual(self.dummy_client.response_code, 200)
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3890
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3891 # verify the result
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3892 results = self.server.get_element('issue', issue_id, self.terse_form)
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3893 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3894 self.assertEqual(self.dummy_client.response_code, 200)
5585
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3895 self.assertEqual(len(results['attributes']['nosy']), 0)
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3896 self.assertListEqual(results['attributes']['nosy'], [])
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3897 self.assertEqual(results['attributes']['title'], None)
8725c09802b8 Added test cases for the element URI methods
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5583
diff changeset
3898
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3899 # delete protected property
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3900 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3901 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3902 form.list.append(cgi.MiniFieldStorage('@etag', etag))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3903 results = self.server.delete_attribute(
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3904 'issue', issue_id, 'creator', form
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3905 )
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3906 expected= {'error': {
5707
f9a762678af6 Change some 400 errors to 405 (method not allowed) errors where user is
John Rouillard <rouilj@ieee.org>
parents: 5706
diff changeset
3907 'status': 405,
f9a762678af6 Change some 400 errors to 405 (method not allowed) errors where user is
John Rouillard <rouilj@ieee.org>
parents: 5706
diff changeset
3908 'msg': AttributeError("Attribute 'creator' can not be updated for class issue.")
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3909 }}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3910
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3911 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3912 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3913 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3914 type(expected['error']['msg']))
5707
f9a762678af6 Change some 400 errors to 405 (method not allowed) errors where user is
John Rouillard <rouilj@ieee.org>
parents: 5706
diff changeset
3915 self.assertEqual(self.dummy_client.response_code, 405)
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3916
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3917 # delete required property
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3918 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3919 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3920 form.list.append(cgi.MiniFieldStorage('@etag', etag))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3921 results = self.server.delete_attribute(
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3922 'issue', issue_id, 'requireme', form
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3923 )
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3924 expected= {'error': {'status': 400,
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3925 'msg': UsageError("Attribute 'requireme' is "
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3926 "required by class issue and can not be deleted.")}}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3927 print(results)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3928 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3929 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3930 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3931 type(expected['error']['msg']))
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3932 self.assertEqual(str(results['error']['msg']),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3933 str(expected['error']['msg']))
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3934 self.assertEqual(self.dummy_client.response_code, 400)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3935
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3936 # delete bogus property
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3937 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3938 self.db.config['WEB_SECRET_KEY'])
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3939 form.list.append(cgi.MiniFieldStorage('@etag', etag))
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3940 results = self.server.delete_attribute(
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3941 'issue', issue_id, 'nosuchprop', form
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3942 )
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3943 expected= {'error': {'status': 400,
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3944 'msg': UsageError("Attribute 'nosuchprop' not valid "
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3945 "for class issue.")}}
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3946 print(results)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3947 self.assertEqual(results['error']['status'],
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3948 expected['error']['status'])
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3949 self.assertEqual(type(results['error']['msg']),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3950 type(expected['error']['msg']))
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3951 self.assertEqual(str(results['error']['msg']),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3952 str(expected['error']['msg']))
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3953 self.assertEqual(self.dummy_client.response_code, 400)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
3954
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3955 def testPatchAdd(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3956 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3957 Test Patch op 'Add'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3958 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3959 # create a new issue with userid 1 in the nosy list
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3960 issue_id = self.db.issue.create(title='foo', nosy=['1'])
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3961
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3962 # fail to add userid 2 to the nosy list
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3963 # no etag
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3964 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3965 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3966 cgi.MiniFieldStorage('@op', 'add'),
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3967 cgi.MiniFieldStorage('nosy', '2')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3968 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3969 results = self.server.patch_element('issue', issue_id, form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3970 self.assertEqual(self.dummy_client.response_code, 412)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3971
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3972 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3973 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3974 form = cgi.FieldStorage()
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3975 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3976 cgi.MiniFieldStorage('@op', 'add'),
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3977 cgi.MiniFieldStorage('nosy', '2'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3978 cgi.MiniFieldStorage('@etag', etag)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3979 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
3980 results = self.server.patch_element('issue', issue_id, form)
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3981 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3982
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3983 # verify the result
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
3984 results = self.server.get_element('issue', issue_id, self.terse_form)
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
3985 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
3986 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3987 self.assertListEqual(results['attributes']['nosy'], ['1', '2'])
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
3988
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3989 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
3990 self.db.config['WEB_SECRET_KEY'])
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3991 form = cgi.FieldStorage()
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3992 form.list = [
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3993 cgi.MiniFieldStorage('@op', 'add'),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3994 cgi.MiniFieldStorage('data', '3'),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3995 cgi.MiniFieldStorage('@etag', etag)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3996 ]
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3997 results = self.server.patch_attribute('issue', issue_id, 'nosy', form)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3998 self.assertEqual(self.dummy_client.response_code, 200)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
3999
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4000 # verify the result
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4001 results = self.server.get_element('issue', issue_id, self.terse_form)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4002 results = results['data']
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4003 self.assertEqual(self.dummy_client.response_code, 200)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4004 self.assertListEqual(results['attributes']['nosy'], ['1', '2', '3'])
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4005
6350
6a69584d117e Remove length checks. We check list content explicitly.
John Rouillard <rouilj@ieee.org>
parents: 6318
diff changeset
4006
5747
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4007 # patch with no new_val/data
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4008 etag = calculate_etag(self.db.issue.getnode(issue_id),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4009 self.db.config['WEB_SECRET_KEY'])
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4010 form = cgi.FieldStorage()
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4011 form.list = [
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4012 cgi.MiniFieldStorage('@op', 'add'),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4013 cgi.MiniFieldStorage('data', ''),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4014 cgi.MiniFieldStorage('@etag', etag)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4015 ]
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4016 results = self.server.patch_attribute('issue', issue_id, 'nosy', form)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4017 self.assertEqual(self.dummy_client.response_code, 200)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4018
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4019 # verify the result
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4020 results = self.server.get_element('issue', issue_id, self.terse_form)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4021 results = results['data']
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4022 self.assertEqual(self.dummy_client.response_code, 200)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4023 self.assertListEqual(results['attributes']['nosy'], ['1', '2', '3'])
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4024
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4025 # patch invalid property
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4026 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4027 self.db.config['WEB_SECRET_KEY'])
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4028 form = cgi.FieldStorage()
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4029 form.list = [
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4030 cgi.MiniFieldStorage('@op', 'add'),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4031 cgi.MiniFieldStorage('data', '3'),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4032 cgi.MiniFieldStorage('@etag', etag)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4033 ]
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4034 results = self.server.patch_attribute('issue', issue_id, 'notGoingToWork', form)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4035 self.assertEqual(self.dummy_client.response_code, 400)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4036 print(results)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4037 expected={'error': {'status': 400,
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4038 'msg': UsageError(
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4039 HyperdbValueError(
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4040 "'notGoingToWork' is not a property of issue",),)}}
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4041 self.assertEqual(results['error']['status'],
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4042 expected['error']['status'])
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4043 self.assertEqual(type(results['error']['msg']),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4044 type(expected['error']['msg']))
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4045 self.assertEqual(str(results['error']['msg']),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4046 str(expected['error']['msg']))
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4047
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4048 def testPatchReplace(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4049 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4050 Test Patch op 'Replace'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4051 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4052 # create a new issue with userid 1 in the nosy list and status = 1
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4053 issue_id = self.db.issue.create(title='foo', nosy=['1'], status='1')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4054
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4055 # fail to replace userid 2 to the nosy list and status = 3
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4056 # no etag.
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4057 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4058 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4059 cgi.MiniFieldStorage('@op', 'replace'),
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4060 cgi.MiniFieldStorage('nosy', '2'),
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4061 cgi.MiniFieldStorage('status', '3')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4062 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
4063 results = self.server.patch_element('issue', issue_id, form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4064 self.assertEqual(self.dummy_client.response_code, 412)
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4065 results = self.server.get_element('issue', issue_id, self.terse_form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4066 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
4067 self.assertEqual(self.dummy_client.response_code, 200)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4068 self.assertEqual(results['attributes']['status'], '1')
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4069 self.assertListEqual(results['attributes']['nosy'], ['1'])
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4070
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4071 # replace userid 2 to the nosy list and status = 3
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4072 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4073 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4074 form = cgi.FieldStorage()
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4075 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4076 cgi.MiniFieldStorage('@op', 'replace'),
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4077 cgi.MiniFieldStorage('nosy', '2'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4078 cgi.MiniFieldStorage('status', '3'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4079 cgi.MiniFieldStorage('@etag', etag)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4080 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4081 results = self.server.patch_element('issue', issue_id, form)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4082 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4083 # verify the result
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4084 results = self.server.get_element('issue', issue_id, self.terse_form)
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
4085 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
4086 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4087 self.assertEqual(results['attributes']['status'], '3')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4088 self.assertListEqual(results['attributes']['nosy'], ['2'])
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4089
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4090 # replace status = 2 using status attribute
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4091 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4092 self.db.config['WEB_SECRET_KEY'])
5706
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4093 form = cgi.FieldStorage()
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4094 form.list = [
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4095 cgi.MiniFieldStorage('@op', 'replace'),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4096 cgi.MiniFieldStorage('data', '2'),
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4097 cgi.MiniFieldStorage('@etag', etag)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4098 ]
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4099 results = self.server.patch_attribute('issue', issue_id, 'status',
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4100 form)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4101 self.assertEqual(self.dummy_client.response_code, 200)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4102 # verify the result
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4103 results = self.server.get_element('issue', issue_id, self.terse_form)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4104 results = results['data']
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4105 self.assertEqual(self.dummy_client.response_code, 200)
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4106 self.assertEqual(results['attributes']['status'], '2')
dfca6136dd7b Add more tests including call to patch an attribute with add and
John Rouillard <rouilj@ieee.org>
parents: 5705
diff changeset
4107
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4108 # try to set a protected prop. It should fail.
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4109 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4110 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4111 form = cgi.FieldStorage()
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4112 form.list = [
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4113 cgi.MiniFieldStorage('@op', 'replace'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4114 cgi.MiniFieldStorage('creator', '2'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4115 cgi.MiniFieldStorage('@etag', etag)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4116 ]
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4117 results = self.server.patch_element('issue', issue_id, form)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4118 expected= {'error': {'status': 400,
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4119 'msg': KeyError('"creator", "actor", "creation" and "activity" are reserved',)}}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4120 print(results)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4121 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4122 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4123 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4124 type(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4125 self.assertEqual(str(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4126 str(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4127 self.assertEqual(self.dummy_client.response_code, 400)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4128
5708
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4129 # try to set a protected prop using patch_attribute. It should
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4130 # fail with a 405 bad/unsupported method.
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4131 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4132 self.db.config['WEB_SECRET_KEY'])
5708
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4133 form = cgi.FieldStorage()
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4134 form.list = [
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4135 cgi.MiniFieldStorage('@op', 'replace'),
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4136 cgi.MiniFieldStorage('data', '2'),
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4137 cgi.MiniFieldStorage('@etag', etag)
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4138 ]
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4139 results = self.server.patch_attribute('issue', issue_id, 'creator',
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4140 form)
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4141 expected= {'error': {'status': 405,
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4142 'msg': AttributeError("Attribute 'creator' can not be updated for class issue.",)}}
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4143 print(results)
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4144 self.assertEqual(results['error']['status'],
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4145 expected['error']['status'])
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4146 self.assertEqual(type(results['error']['msg']),
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4147 type(expected['error']['msg']))
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4148 self.assertEqual(str(results['error']['msg']),
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4149 str(expected['error']['msg']))
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4150 self.assertEqual(self.dummy_client.response_code, 405)
ad786c394788 Add another test case: change protected attribute using patch_attribute.
John Rouillard <rouilj@ieee.org>
parents: 5707
diff changeset
4151
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4152 def testPatchRemoveAll(self):
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4153 """
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4154 Test Patch Action 'Remove'
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4155 """
5595
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4156 # create a new issue with userid 1 and 2 in the nosy list
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4157 issue_id = self.db.issue.create(title='foo', nosy=['1', '2'])
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4158
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4159 # fail to remove the nosy list and the title
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4160 # no etag
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4161 form = cgi.FieldStorage()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4162 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4163 cgi.MiniFieldStorage('@op', 'remove'),
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4164 cgi.MiniFieldStorage('nosy', ''),
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4165 cgi.MiniFieldStorage('title', '')
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4166 ]
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
4167 results = self.server.patch_element('issue', issue_id, form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4168 self.assertEqual(self.dummy_client.response_code, 412)
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4169 results = self.server.get_element('issue', issue_id, self.terse_form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4170 results = results['data']
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4171 self.assertEqual(self.dummy_client.response_code, 200)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4172 self.assertEqual(results['attributes']['title'], 'foo')
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4173 self.assertEqual(len(results['attributes']['nosy']), 2)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4174 self.assertEqual(results['attributes']['nosy'], ['1', '2'])
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4175
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4176 # remove the nosy list and the title
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4177 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4178 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4179 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4180 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4181 cgi.MiniFieldStorage('@op', 'remove'),
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4182 cgi.MiniFieldStorage('nosy', ''),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4183 cgi.MiniFieldStorage('title', ''),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4184 cgi.MiniFieldStorage('@etag', etag)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4185 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4186 results = self.server.patch_element('issue', issue_id, form)
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
4187 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4188
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4189 # verify the result
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4190 results = self.server.get_element('issue', issue_id, self.terse_form)
5591
a25d79e874cb Added filtering and pagination
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5588
diff changeset
4191 results = results['data']
5588
6b3a9655a7d9 Move decorator to outside of the class
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5586
diff changeset
4192 self.assertEqual(self.dummy_client.response_code, 200)
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4193 self.assertEqual(results['attributes']['title'], None)
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4194 self.assertEqual(len(results['attributes']['nosy']), 0)
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4195 self.assertEqual(results['attributes']['nosy'], [])
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
4196
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4197 # try to remove a protected prop. It should fail.
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4198 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4199 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4200 form = cgi.FieldStorage()
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4201 form.list = [
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4202 cgi.MiniFieldStorage('@op', 'remove'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4203 cgi.MiniFieldStorage('creator', '2'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4204 cgi.MiniFieldStorage('@etag', etag)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4205 ]
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4206 results = self.server.patch_element('issue', issue_id, form)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4207 expected= {'error': {'status': 400,
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4208 'msg': KeyError('"creator", "actor", "creation" and "activity" are reserved',)}}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4209 print(results)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4210 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4211 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4212 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4213 type(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4214 self.assertEqual(str(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4215 str(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4216 self.assertEqual(self.dummy_client.response_code, 400)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4217
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4218 # try to remove a required prop. it should fail
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4219 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4220 self.db.config['WEB_SECRET_KEY'])
5705
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4221 form.list = [
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4222 cgi.MiniFieldStorage('@op', 'remove'),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4223 cgi.MiniFieldStorage('requireme', ''),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4224 cgi.MiniFieldStorage('@etag', etag)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4225 ]
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4226 results = self.server.patch_element('issue', issue_id, form)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4227 expected= {'error': {'status': 400,
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4228 'msg': UsageError("Attribute 'requireme' is required by class issue and can not be removed.")
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4229 }}
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4230 print(results)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4231 self.assertEqual(results['error']['status'],
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4232 expected['error']['status'])
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4233 self.assertEqual(type(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4234 type(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4235 self.assertEqual(str(results['error']['msg']),
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4236 str(expected['error']['msg']))
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4237 self.assertEqual(self.dummy_client.response_code, 400)
457fc482e6b1 Method PUT: ignore specification of protected properties which can not
John Rouillard <rouilj@ieee.org>
parents: 5690
diff changeset
4238
5599
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4239 def testPatchAction(self):
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4240 """
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4241 Test Patch Action 'Action'
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4242 """
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4243 # create a new issue with userid 1 and 2 in the nosy list
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4244 issue_id = self.db.issue.create(title='foo')
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4245
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4246 # fail to execute action retire
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4247 # no etag
5599
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4248 form = cgi.FieldStorage()
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4249 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4250 cgi.MiniFieldStorage('@op', 'action'),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4251 cgi.MiniFieldStorage('@action_name', 'retire')
5599
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4252 ]
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4253 results = self.server.patch_element('issue', issue_id, form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4254 self.assertEqual(self.dummy_client.response_code, 412)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4255 self.assertFalse(self.db.issue.is_retired(issue_id))
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4256
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4257 # execute action retire
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4258 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4259 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4260 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4261 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4262 cgi.MiniFieldStorage('@op', 'action'),
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4263 cgi.MiniFieldStorage('@action_name', 'retire'),
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4264 cgi.MiniFieldStorage('@etag', etag)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4265 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4266 results = self.server.patch_element('issue', issue_id, form)
5599
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4267 self.assertEqual(self.dummy_client.response_code, 200)
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4268
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4269 # verify the result
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4270 self.assertTrue(self.db.issue.is_retired(issue_id))
a76d88673375 Added Patch operator 'action'
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5595
diff changeset
4271
5747
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4272 # execute action restore
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4273 form = cgi.FieldStorage()
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4274 etag = calculate_etag(self.db.issue.getnode(issue_id),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4275 self.db.config['WEB_SECRET_KEY'])
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4276 form.list = [
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4277 cgi.MiniFieldStorage('@op', 'action'),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4278 cgi.MiniFieldStorage('@action_name', 'restore'),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4279 cgi.MiniFieldStorage('@etag', etag)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4280 ]
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4281 results = self.server.patch_element('issue', issue_id, form)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4282 self.assertEqual(self.dummy_client.response_code, 200)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4283
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4284 # verify the result
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4285 self.assertTrue(not self.db.issue.is_retired(issue_id))
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4286
6318
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4287 def testPatchBadAction(self):
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4288 """
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4289 Test Patch Action 'Unknown'
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4290 """
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4291 # create a new issue with userid 1 and 2 in the nosy list
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4292 issue_id = self.db.issue.create(title='foo')
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4293
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4294 # execute action retire
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4295 form = cgi.FieldStorage()
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4296 etag = calculate_etag(self.db.issue.getnode(issue_id),
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4297 self.db.config['WEB_SECRET_KEY'])
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4298 form.list = [
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4299 cgi.MiniFieldStorage('@op', 'action'),
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4300 cgi.MiniFieldStorage('@action_name', 'unknown'),
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4301 cgi.MiniFieldStorage('@etag', etag)
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4302 ]
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4303 results = self.server.patch_element('issue', issue_id, form)
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4304 self.assertEqual(self.dummy_client.response_code, 400)
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4305 # verify the result, note order of allowed elements changes
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4306 # for python2/3 so just check prefix.
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4307 self.assertIn('action "unknown" is not supported, allowed: ',
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4308 results['error']['msg'].args[0])
ec853cef2f09 Add test for invaild action in rest.py patch_element.
John Rouillard <rouilj@ieee.org>
parents: 6317
diff changeset
4309
5595
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4310 def testPatchRemove(self):
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4311 """
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4312 Test Patch Action 'Remove' only some element from a list
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4313 """
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4314 # create a new issue with userid 1, 2, 3 in the nosy list
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4315 issue_id = self.db.issue.create(title='foo', nosy=['1', '2', '3'])
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4316
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4317 # fail to remove the nosy list and the title
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4318 # no etag
5595
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4319 form = cgi.FieldStorage()
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4320 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4321 cgi.MiniFieldStorage('@op', 'remove'),
5595
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4322 cgi.MiniFieldStorage('nosy', '1, 2'),
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4323 ]
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4324 results = self.server.patch_element('issue', issue_id, form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4325 self.assertEqual(self.dummy_client.response_code, 412)
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4326 results = self.server.get_element('issue', issue_id, self.terse_form)
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4327 results = results['data']
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4328 self.assertEqual(self.dummy_client.response_code, 200)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4329 self.assertEqual(len(results['attributes']['nosy']), 3)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4330 self.assertEqual(results['attributes']['nosy'], ['1', '2', '3'])
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4331
5747
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4332 # remove 1 and 2 from the nosy list
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4333 form = cgi.FieldStorage()
5727
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4334 etag = calculate_etag(self.db.issue.getnode(issue_id),
8b5171f353eb issue2551033: actually use the key in hmac generation. Finally add
John Rouillard <rouilj@ieee.org>
parents: 5726
diff changeset
4335 self.db.config['WEB_SECRET_KEY'])
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4336 form.list = [
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4337 cgi.MiniFieldStorage('@op', 'remove'),
5630
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4338 cgi.MiniFieldStorage('nosy', '1, 2'),
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4339 cgi.MiniFieldStorage('@etag', etag)
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4340 ]
07abc8d36940 Add etag support to rest interface to prevent multiple users from
John Rouillard <rouilj@ieee.org>
parents: 5623
diff changeset
4341 results = self.server.patch_element('issue', issue_id, form)
5595
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4342 self.assertEqual(self.dummy_client.response_code, 200)
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4343
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4344 # verify the result
5672
a7211712b110 Fix tests for latest REST changes
Ralf Schlatterbeck <rsc@runtux.com>
parents: 5656
diff changeset
4345 results = self.server.get_element('issue', issue_id, self.terse_form)
5595
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4346 results = results['data']
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4347 self.assertEqual(self.dummy_client.response_code, 200)
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4348 self.assertEqual(len(results['attributes']['nosy']), 1)
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4349 self.assertEqual(results['attributes']['nosy'], ['3'])
65caddd54da2 Handle operation for patch separately
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5592
diff changeset
4350
5747
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4351 # delete last element: 3
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4352 etag = calculate_etag(self.db.issue.getnode(issue_id),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4353 self.db.config['WEB_SECRET_KEY'])
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4354 form = cgi.FieldStorage()
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4355 form.list = [
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4356 cgi.MiniFieldStorage('@op', 'remove'),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4357 cgi.MiniFieldStorage('data', '3'),
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4358 cgi.MiniFieldStorage('@etag', etag)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4359 ]
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4360 results = self.server.patch_attribute('issue', issue_id, 'nosy', form)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4361 self.assertEqual(self.dummy_client.response_code, 200)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4362
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4363 # verify the result
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4364 results = self.server.get_element('issue', issue_id, self.terse_form)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4365 results = results['data']
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4366 self.assertEqual(self.dummy_client.response_code, 200)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4367 self.assertEqual(len(results['attributes']['nosy']), 0)
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4368 self.assertListEqual(results['attributes']['nosy'], [])
17b38e209307 Test patch op=action restore; test patch add for attribute with no value.
John Rouillard <rouilj@ieee.org>
parents: 5746
diff changeset
4369
7156
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4370 def testRestExposeHeaders(self):
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4371
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4372 local_client = self.server.client
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4373 body = b'{ "data": "Joe Doe 1" }'
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4374 env = { "CONTENT_TYPE": "application/json",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4375 "CONTENT_LENGTH": len(body),
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4376 "REQUEST_METHOD": "PUT",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4377 "HTTP_ORIGIN": "http://tracker.example"
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4378 }
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4379 local_client.env.update(env)
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4380
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4381 local_client.db.config["WEB_ALLOWED_API_ORIGINS"] = " * "
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4382
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4383 headers={"accept": "application/json; version=1",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4384 "content-type": env['CONTENT_TYPE'],
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4385 "content-length": env['CONTENT_LENGTH'],
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4386 "origin": env['HTTP_ORIGIN']
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4387 }
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4388 self.headers=headers
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4389 # we need to generate a FieldStorage the looks like
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4390 # FieldStorage(None, None, 'string') rather than
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4391 # FieldStorage(None, None, [])
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4392 body_file=BytesIO(body) # FieldStorage needs a file
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4393 form = client.BinaryFieldStorage(body_file,
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4394 headers=headers,
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4395 environ=env)
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4396 local_client.request.headers.get=self.get_header
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4397 results = self.server.dispatch('PUT',
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4398 "/rest/data/user/%s/realname"%self.joeid,
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4399 form)
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4400
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4401 for header in [ "X-RateLimit-Limit",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4402 "X-RateLimit-Remaining",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4403 "X-RateLimit-Reset",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4404 "X-RateLimit-Limit-Period",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4405 "Retry-After",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4406 "Sunset",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4407 "Allow",
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4408 ]:
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4409 self.assertIn(
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4410 header,
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4411 self.server.client.additional_headers[
6f09103a6522 [issue2551263] expose headers to rest clients
John Rouillard <rouilj@ieee.org>
parents: 7155
diff changeset
4412 "Access-Control-Expose-Headers"])
7155
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4413
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4414 def testRestMatchWildcardOrigin(self):
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4415 # cribbed from testDispatch #1
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4416 # PUT: joe's 'realname' using json data.
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4417 # simulate: /rest/data/user/<id>/realname
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4418 # use etag in header
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4419
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4420 # verify that credential header is missing, valid allow origin
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4421 # header and vary includes origin.
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4422
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4423 local_client = self.server.client
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4424 etag = calculate_etag(self.db.user.getnode(self.joeid),
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4425 self.db.config['WEB_SECRET_KEY'])
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4426 body = b'{ "data": "Joe Doe 1" }'
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4427 env = { "CONTENT_TYPE": "application/json",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4428 "CONTENT_LENGTH": len(body),
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4429 "REQUEST_METHOD": "PUT",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4430 "HTTP_ORIGIN": "https://bad.origin"
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4431 }
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4432 local_client.env.update(env)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4433
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4434 local_client.db.config["WEB_ALLOWED_API_ORIGINS"] = " * "
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4435
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4436 headers={"accept": "application/json; version=1",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4437 "content-type": env['CONTENT_TYPE'],
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4438 "content-length": env['CONTENT_LENGTH'],
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4439 "if-match": etag,
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4440 "origin": env['HTTP_ORIGIN']
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4441 }
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4442 self.headers=headers
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4443 # we need to generate a FieldStorage the looks like
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4444 # FieldStorage(None, None, 'string') rather than
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4445 # FieldStorage(None, None, [])
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4446 body_file=BytesIO(body) # FieldStorage needs a file
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4447 form = client.BinaryFieldStorage(body_file,
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4448 headers=headers,
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4449 environ=env)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4450 local_client.request.headers.get=self.get_header
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4451 results = self.server.dispatch('PUT',
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4452 "/rest/data/user/%s/realname"%self.joeid,
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4453 form)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4454
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4455 self.assertNotIn("Access-Control-Allow-Credentials",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4456 local_client.additional_headers)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4457
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4458 self.assertIn("Access-Control-Allow-Origin",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4459 local_client.additional_headers)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4460 self.assertEqual(
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4461 headers['origin'],
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4462 local_client.additional_headers["Access-Control-Allow-Origin"])
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4463
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4464
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4465 self.assertIn("Vary", local_client.additional_headers)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4466 self.assertIn("Origin",
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4467 local_client.additional_headers['Vary'])
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4468
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4469 self.assertEqual(local_client.response_code, 200)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4470 results = self.server.get_element('user', self.joeid, self.empty_form)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4471 self.assertEqual(self.dummy_client.response_code, 200)
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4472 self.assertEqual(results['data']['attributes']['realname'],
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4473 'Joe Doe 1')
89a59e46b3af improve REST interface security
John Rouillard <rouilj@ieee.org>
parents: 6660
diff changeset
4474
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4475 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4476 def test_expired_jwt(self):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4477 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4478 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4479 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4480 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4481 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4482
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4483 secret = self.db.config.WEB_JWT_SECRET[0]
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4484
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4485 # verify library and tokens are correct
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4486 self.assertRaises(jwt.exceptions.InvalidTokenError,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4487 jwt.decode, self.jwt['expired'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4488 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4489 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4490 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4491
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4492 result = jwt.decode(self.jwt['user'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4493 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4494 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4495 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4496 self.assertEqual(self.claim['user'],result)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4497
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4498 result = jwt.decode(self.jwt['user:email'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4499 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4500 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4501 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4502 self.assertEqual(self.claim['user:email'],result)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4503
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4504 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4505 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4506 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4507 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4508 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4509 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4510 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4511 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4512 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4513 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4514 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4515 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4516 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4517 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4518 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4519 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4520 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4521 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4522 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4523 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4524 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4525 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4526
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4527 # set up for expired token first
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4528 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['expired']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4529 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4530
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4531 # this will be the admin still as auth failed
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4532 self.assertEqual('1', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4533 self.assertEqual(out[0], b'Invalid Login - Signature has expired')
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4534 del(out[0])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4535
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4536 @skip_jwt
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4537 def test_user_jwt_key_rotation_mutlisig(self):
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4538 # self.dummy_client.main() closes database, so
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4539 # we need a new test with setup called for each test
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4540 out = []
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4541 def wh(s):
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4542 out.append(s)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4543
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4544 # verify library and tokens are correct
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4545 self.assertRaises(jwt.exceptions.InvalidTokenError,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4546 jwt.decode, self.jwt['expired'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4547 self.old_secret, algorithms=['HS256'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4548 audience=self.db.config.TRACKER_WEB,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4549 issuer=self.db.config.TRACKER_WEB)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4550
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4551 result = jwt.decode(self.jwt['user_new_secret'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4552 self.new_secret, algorithms=['HS256'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4553 audience=self.db.config.TRACKER_WEB,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4554 issuer=self.db.config.TRACKER_WEB)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4555 self.assertEqual(self.claim['user'],result)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4556
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4557 result = jwt.decode(self.jwt['user:email'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4558 self.old_secret, algorithms=['HS256'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4559 audience=self.db.config.TRACKER_WEB,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4560 issuer=self.db.config.TRACKER_WEB)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4561 self.assertEqual(self.claim['user:email'],result)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4562
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4563 # set environment for all jwt tests
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4564 env = {
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4565 'PATH_INFO': 'rest/data/user',
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4566 'HTTP_HOST': 'localhost',
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4567 'TRACKER_NAME': 'rounduptest',
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4568 "REQUEST_METHOD": "GET"
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4569 }
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4570
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4571 # test case where rotation key is used,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4572 # add spaces after ',' to test config system
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4573 self.db.config['WEB_JWT_SECRET'] = "%s, %s, " % (
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4574 self.new_secret, self.old_secret
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4575 )
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4576
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4577 self.dummy_client = client.Client(self.instance, MockNull(), env,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4578 [], None)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4579 self.dummy_client.db = self.db
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4580 self.dummy_client.request.headers.get = self.get_header
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4581 self.empty_form = cgi.FieldStorage()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4582 self.terse_form = cgi.FieldStorage()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4583 self.terse_form.list = [
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4584 cgi.MiniFieldStorage('@verbose', '0'),
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4585 ]
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4586 self.dummy_client.form = cgi.FieldStorage()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4587 self.dummy_client.form.list = [
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4588 cgi.MiniFieldStorage('@fields', 'username,address'),
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4589 ]
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4590 # accumulate json output for further analysis
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4591 self.dummy_client.write = wh
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4592
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4593 # set up for standard user role token
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4594 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['user']
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4595
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4596 self.dummy_client.main()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4597 print(out[0])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4598 json_dict = json.loads(b2s(out[0]))
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4599 print(json_dict)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4600 # user will be joe id 3 as auth works
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4601 self.assertTrue('3', self.db.getuid())
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4602 # there should be three items in the collection admin, anon, and joe
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4603 self.assertEqual(3, len(json_dict['data']['collection']))
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4604 # since this token has no access to email addresses, only joe
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4605 # should have email addresses. Order is by id by default.
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4606 self.assertFalse('address' in json_dict['data']['collection'][0])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4607 self.assertFalse('address' in json_dict['data']['collection'][1])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4608 self.assertTrue('address' in json_dict['data']['collection'][2])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4609 del(out[0])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4610 self.db.setCurrentUser('admin')
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4611
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4612 @skip_jwt
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4613 def test_user_jwt_key_rotation_sig_failure(self):
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4614 # self.dummy_client.main() closes database, so
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4615 # we need a new test with setup called for each test
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4616 out = []
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4617 def wh(s):
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4618 out.append(s)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4619
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4620 # verify library and tokens are correct
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4621 self.assertRaises(jwt.exceptions.InvalidTokenError,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4622 jwt.decode, self.jwt['expired'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4623 self.old_secret, algorithms=['HS256'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4624 audience=self.db.config.TRACKER_WEB,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4625 issuer=self.db.config.TRACKER_WEB)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4626
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4627 result = jwt.decode(self.jwt['user_new_secret'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4628 self.new_secret, algorithms=['HS256'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4629 audience=self.db.config.TRACKER_WEB,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4630 issuer=self.db.config.TRACKER_WEB)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4631 self.assertEqual(self.claim['user'],result)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4632
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4633 result = jwt.decode(self.jwt['user:email'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4634 self.old_secret, algorithms=['HS256'],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4635 audience=self.db.config.TRACKER_WEB,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4636 issuer=self.db.config.TRACKER_WEB)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4637 self.assertEqual(self.claim['user:email'],result)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4638
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4639 # set environment for all jwt tests
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4640 env = {
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4641 'PATH_INFO': 'rest/data/user',
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4642 'HTTP_HOST': 'localhost',
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4643 'TRACKER_NAME': 'rounduptest',
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4644 "REQUEST_METHOD": "GET"
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4645 }
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4646
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4647 self.dummy_client = client.Client(self.instance, MockNull(), env,
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4648 [], None)
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4649 self.dummy_client.db = self.db
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4650 self.dummy_client.request.headers.get = self.get_header
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4651 self.empty_form = cgi.FieldStorage()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4652 self.terse_form = cgi.FieldStorage()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4653 self.terse_form.list = [
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4654 cgi.MiniFieldStorage('@verbose', '0'),
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4655 ]
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4656 self.dummy_client.form = cgi.FieldStorage()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4657 self.dummy_client.form.list = [
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4658 cgi.MiniFieldStorage('@fields', 'username,address'),
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4659 ]
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4660 # accumulate json output for further analysis
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4661 self.dummy_client.write = wh
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4662
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4663 # test case where new json secret is in place
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4664 self.db.config['WEB_JWT_SECRET'] = self.new_secret
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4665
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4666 # set up for standard user role token
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4667 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['user']
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4668 self.dummy_client.main()
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4669 print(out[0])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4670
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4671 self.assertEqual(out[0],
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4672 b'Invalid Login - Signature verification failed')
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4673 del(out[0])
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4674 self.db.setCurrentUser('admin')
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4675
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4676 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4677 def test_user_jwt(self):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4678 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4679 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4680 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4681 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4682 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4683
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4684 secret = self.db.config.WEB_JWT_SECRET[0]
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4685
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4686 # verify library and tokens are correct
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4687 self.assertRaises(jwt.exceptions.InvalidTokenError,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4688 jwt.decode, self.jwt['expired'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4689 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4690 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4691 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4692
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4693 result = jwt.decode(self.jwt['user'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4694 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4695 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4696 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4697 self.assertEqual(self.claim['user'],result)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4698
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4699 result = jwt.decode(self.jwt['user:email'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4700 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4701 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4702 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4703 self.assertEqual(self.claim['user:email'],result)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4704
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4705 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4706 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4707 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4708 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4709 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4710 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4711 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4712 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4713 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4714 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4715 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4716 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4717 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4718 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4719 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4720 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4721 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4722 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4723 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4724 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4725 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4726 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4727
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4728 # set up for standard user role token
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4729 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['user']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4730 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4731 print(out[0])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4732 json_dict = json.loads(b2s(out[0]))
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4733 print(json_dict)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4734 # user will be joe id 3 as auth works
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4735 self.assertTrue('3', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4736 # there should be three items in the collection admin, anon, and joe
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4737 self.assertEqual(3, len(json_dict['data']['collection']))
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4738 # since this token has no access to email addresses, only joe
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4739 # should have email addresses. Order is by id by default.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4740 self.assertFalse('address' in json_dict['data']['collection'][0])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4741 self.assertFalse('address' in json_dict['data']['collection'][1])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4742 self.assertTrue('address' in json_dict['data']['collection'][2])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4743 del(out[0])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4744 self.db.setCurrentUser('admin')
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4745
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4746 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4747 def test_user_email_jwt(self):
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4748 '''tests "Rest Access" permission present case'''
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4749 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4750 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4751 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4752 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4753 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4754
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4755 secret = self.db.config.WEB_JWT_SECRET[0]
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4756
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4757 # verify library and tokens are correct
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4758 self.assertRaises(jwt.exceptions.InvalidTokenError,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4759 jwt.decode, self.jwt['expired'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4760 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4761 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4762 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4763
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4764 result = jwt.decode(self.jwt['user'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4765 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4766 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4767 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4768 self.assertEqual(self.claim['user'],result)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4769
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4770 result = jwt.decode(self.jwt['user:email'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4771 secret, algorithms=['HS256'],
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4772 audience=self.db.config.TRACKER_WEB,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4773 issuer=self.db.config.TRACKER_WEB)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4774 self.assertEqual(self.claim['user:email'],result)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4775
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4776 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4777 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4778 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4779 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4780 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4781 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4782 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4783 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4784 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4785 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4786 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4787 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4788 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4789 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4790 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4791 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4792 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4793 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4794 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4795 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4796 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4797 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4798
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4799 # set up for limited user:email role token
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4800 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['user:email']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4801 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4802 json_dict = json.loads(b2s(out[0]))
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4803 print(json_dict)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4804 # user will be joe id 3 as auth works
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4805 self.assertTrue('3', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4806 # there should be three items in the collection admin, anon, and joe
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4807 self.assertEqual(3, len(json_dict['data']['collection']))
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4808 # However this token has access to email addresses, so all three
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4809 # should have email addresses. Order is by id by default.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4810 self.assertTrue('address' in json_dict['data']['collection'][0])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4811 self.assertTrue('address' in json_dict['data']['collection'][1])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4812 self.assertTrue('address' in json_dict['data']['collection'][2])
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4813
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4814 @skip_jwt
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4815 def test_user_emailnorest_jwt(self):
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4816 '''tests "Rest Access" permission missing case'''
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4817 # self.dummy_client.main() closes database, so
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4818 # we need a new test with setup called for each test
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4819 out = []
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4820 def wh(s):
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4821 out.append(s)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4822
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4823 secret = self.db.config.WEB_JWT_SECRET[0]
5879
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4824
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4825 # verify library and tokens are correct
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4826 self.assertRaises(jwt.exceptions.InvalidTokenError,
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4827 jwt.decode, self.jwt['expired'],
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4828 secret, algorithms=['HS256'],
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4829 audience=self.db.config.TRACKER_WEB,
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4830 issuer=self.db.config.TRACKER_WEB)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4831
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4832 result = jwt.decode(self.jwt['user'],
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4833 secret, algorithms=['HS256'],
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4834 audience=self.db.config.TRACKER_WEB,
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4835 issuer=self.db.config.TRACKER_WEB)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4836 self.assertEqual(self.claim['user'],result)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4837
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4838 result = jwt.decode(self.jwt['user:email'],
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4839 secret, algorithms=['HS256'],
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4840 audience=self.db.config.TRACKER_WEB,
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4841 issuer=self.db.config.TRACKER_WEB)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4842 self.assertEqual(self.claim['user:email'],result)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4843
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4844 # set environment for all jwt tests
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4845 env = {
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4846 'PATH_INFO': 'rest/data/user',
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4847 'HTTP_HOST': 'localhost',
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4848 'TRACKER_NAME': 'rounduptest',
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4849 "REQUEST_METHOD": "GET"
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4850 }
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4851 self.dummy_client = client.Client(self.instance, MockNull(), env,
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4852 [], None)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4853 self.dummy_client.db = self.db
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4854 self.dummy_client.request.headers.get = self.get_header
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4855 self.empty_form = cgi.FieldStorage()
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4856 self.terse_form = cgi.FieldStorage()
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4857 self.terse_form.list = [
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4858 cgi.MiniFieldStorage('@verbose', '0'),
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4859 ]
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4860 self.dummy_client.form = cgi.FieldStorage()
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4861 self.dummy_client.form.list = [
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4862 cgi.MiniFieldStorage('@fields', 'username,address'),
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4863 ]
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4864 # accumulate json output for further analysis
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4865 self.dummy_client.write = wh
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4866
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4867 # set up for limited user:email role token
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4868 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['user:emailnorest']
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4869 self.dummy_client.main()
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4870 json_dict = json.loads(b2s(out[0]))
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4871 # user will be joe id 3 as auth works
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4872 self.assertTrue('1', self.db.getuid())
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4873 { "error": { "status": 403, "msg": "Forbidden." } }
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4874 self.assertTrue('error' in json_dict)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4875 self.assertTrue(json_dict['error']['status'], 403)
94a7669677ae add permissions to control user of rest and xmlrpc API interfaces.
John Rouillard <rouilj@ieee.org>
parents: 5878
diff changeset
4876 self.assertTrue(json_dict['error']['msg'], "Forbidden.")
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4877
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4878 @skip_jwt
7809
be6cb2e0d471 feat: add support for rotating jwt keys
John Rouillard <rouilj@ieee.org>
parents: 7587
diff changeset
4879 def test_admin_disabled_jwt(self):
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4880 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4881 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4882 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4883 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4884 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4885
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4886 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4887 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4888 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4889 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4890 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4891 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4892 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4893 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4894 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4895 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4896 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4897 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4898 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4899 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4900 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4901 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4902 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4903 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4904 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4905 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4906 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4907 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4908 # disable jwt validation by making secret too short
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4909 # use the default value for this in configure.py.
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4910 self.db.config['WEB_JWT_SECRET'] = "disabled"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4911 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['user']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4912 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4913 # user will be 1 as there is no auth
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4914 self.assertTrue('1', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4915 self.assertEqual(out[0], b'Invalid Login - Support for jwt disabled by admin.')
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4916
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4917 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4918 def test_bad_issue_jwt(self):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4919 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4920 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4921 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4922 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4923 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4924
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4925 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4926 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4927 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4928 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4929 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4930 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4931 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4932 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4933 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4934 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4935 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4936 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4937 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4938 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4939 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4940 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4941 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4942 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4943 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4944 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4945 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4946 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4947 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['badiss']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4948 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4949 # user will be 1 as there is no auth
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4950 self.assertTrue('1', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4951 self.assertEqual(out[0], b'Invalid Login - Invalid issuer')
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4952
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4953 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4954 def test_bad_audience_jwt(self):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4955 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4956 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4957 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4958 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4959 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4960
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4961 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4962 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4963 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4964 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4965 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4966 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4967 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4968 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4969 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4970 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4971 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4972 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4973 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4974 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4975 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4976 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4977 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4978 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4979 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4980 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4981 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4982 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4983 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['badaud']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4984 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4985 # user will be 1 as there is no auth
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4986 self.assertTrue('1', self.db.getuid())
7389
613f822f1f24 Support pyjwt-2.7.0 in test_bad_audience_jwt
John Rouillard <rouilj@ieee.org>
parents: 7372
diff changeset
4987 self.assertIn(out[0], [b'Invalid Login - Invalid audience',
613f822f1f24 Support pyjwt-2.7.0 in test_bad_audience_jwt
John Rouillard <rouilj@ieee.org>
parents: 7372
diff changeset
4988 b"Invalid Login - Audience doesn't match"])
5878
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4989
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4990 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4991 def test_bad_roles_jwt(self):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4992 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4993 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4994 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4995 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4996 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4997
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4998 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
4999 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5000 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5001 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5002 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5003 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5004 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5005 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5006 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5007 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5008 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5009 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5010 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5011 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5012 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5013 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5014 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5015 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5016 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5017 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5018 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5019 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5020 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['badroles']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5021 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5022 # user will be 1 as there is no auth
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5023 self.assertTrue('1', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5024 self.assertEqual(out[0], b'Invalid Login - Token roles are invalid.')
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5025
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5026 @skip_jwt
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5027 def test_bad_subject_jwt(self):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5028 # self.dummy_client.main() closes database, so
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5029 # we need a new test with setup called for each test
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5030 out = []
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5031 def wh(s):
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5032 out.append(s)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5033
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5034 # set environment for all jwt tests
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5035 env = {
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5036 'PATH_INFO': 'rest/data/user',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5037 'HTTP_HOST': 'localhost',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5038 'TRACKER_NAME': 'rounduptest',
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5039 "REQUEST_METHOD": "GET"
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5040 }
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5041 self.dummy_client = client.Client(self.instance, MockNull(), env,
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5042 [], None)
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5043 self.dummy_client.db = self.db
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5044 self.dummy_client.request.headers.get = self.get_header
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5045 self.empty_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5046 self.terse_form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5047 self.terse_form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5048 cgi.MiniFieldStorage('@verbose', '0'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5049 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5050 self.dummy_client.form = cgi.FieldStorage()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5051 self.dummy_client.form.list = [
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5052 cgi.MiniFieldStorage('@fields', 'username,address'),
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5053 ]
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5054 # accumulate json output for further analysis
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5055 self.dummy_client.write = wh
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5056 env['HTTP_AUTHORIZATION'] = 'bearer %s'%self.jwt['badsub']
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5057 self.dummy_client.main()
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5058 # user will be 1 as there is no auth
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5059 self.assertTrue('1', self.db.getuid())
1b57d8f3eb97 Add rudimentery experiment JSON Web Token (jwt) support
John Rouillard <rouilj@ieee.org>
parents: 5874
diff changeset
5060 self.assertEqual(out[0], b'Invalid Login - Token subject is invalid.')
5586
8f2fbc88e155 Fixed code convention
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5585
diff changeset
5061
5592
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
5062 def get_obj(path, id):
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
5063 return {
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
5064 'id': id,
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
5065 'link': path + id
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
5066 }
adcb5cbe82bd Add unittest for pagination and filtering
Chau Nguyen <dangchau1991@yahoo.com>
parents: 5591
diff changeset
5067
5583
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
5068 if __name__ == '__main__':
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
5069 runner = unittest.TextTestRunner()
c65d98a16780 Added rest unit test
Chau Nguyen <dangchau1991@yahoo.com>
parents:
diff changeset
5070 unittest.main(testRunner=runner)

Roundup Issue Tracker: http://roundup-tracker.org/