def test_new(self): """Tests that GET /applicationsettings/new returns an appropriate JSON object for creating a new application settings object. The properties of the JSON object are 'languages', 'users' and 'orthographies' and their values are arrays/lists. """ # Add some orthographies. orthography1 = h.generate_default_orthography1() orthography2 = h.generate_default_orthography2() Session.add_all([orthography1, orthography2]) Session.commit() # Get the data currently in the db (see websetup.py for the test data). data = { 'languages': h.get_languages(), 'users': h.get_mini_dicts_getter('User')(), 'orthographies': h.get_mini_dicts_getter('Orthography')() } # JSON.stringify and then re-Python-ify the data. This is what the data # should look like in the response to a simulated GET request. data = json.loads(json.dumps(data, cls=h.JSONOLDEncoder)) # GET /applicationsettings/new without params. Expect a JSON array for # every store. response = self.app.get(url('new_applicationsetting'), extra_environ=self.extra_environ_admin) resp = json.loads(response.body) assert response.content_type == 'application/json' assert resp['languages'] == data['languages'] assert resp['users'] == data['users'] assert resp['orthographies'] == data['orthographies'] assert response.content_type == 'application/json' # GET /applicationsettings/new with params. Param values are treated as # strings, not JSON. If any params are specified, the default is to # return a JSON array corresponding to store for the param. There are # three cases that will result in an empty JSON array being returned: # 1. the param is not specified # 2. the value of the specified param is an empty string # 3. the value of the specified param is an ISO 8601 UTC datetime # string that matches the most recent datetime_modified value of the # store in question. params = { # Value is empty string: 'languages' will not be in response. 'languages': '', # Value is any string: 'users' will be in response. 'users': 'anything can go here!', # Value is ISO 8601 UTC datetime string that does not match the most # recent Orthography.datetime_modified value: 'orthographies' *will* # be in the response. 'orthographies': datetime.datetime.utcnow().isoformat(), } response = self.app.get(url('new_applicationsetting'), params, extra_environ=self.extra_environ_admin) resp = json.loads(response.body) assert resp['languages'] == [] assert resp['users'] == data['users'] assert resp['orthographies'] == data['orthographies']
def test_edit(self): """Tests that GET /applicationsettings/id/edit returns a JSON object for editing an existing application settings. The JSON object is of the form {application_settings: {...}, data: {...}} or {'error': '...'} (and a 404 status code) depending on whether the id is valid or invalid/unspecified, respectively. """ # Not logged in: expect 401 Unauthorized response = self.app.get( url('edit_applicationsetting', id=100000000000), status=401) resp = json.loads(response.body) assert resp['error'] == u'Authentication is required to access this resource.' assert response.content_type == 'application/json' # Invalid id: expect 404 Not Found id = 100000000000 response = self.app.get(url('edit_applicationsetting', id=id), extra_environ=self.extra_environ_admin, status=404) assert json.loads(response.body)['error'] == \ u'There is no application settings with id %s' % id assert response.content_type == 'application/json' # No id: expect 404 Not Found response = self.app.get(url('edit_applicationsetting', id=''), status=404, extra_environ=self.extra_environ_admin) assert json.loads(response.body)['error'] == \ 'The resource could not be found.' # Add the default application settings. application_settings = add_default_application_settings() application_settings = h.get_application_settings() application_settings_id = application_settings.id # Valid id response = self.app.get(url('edit_applicationsetting', id=application_settings_id), extra_environ=self.extra_environ_admin) resp = json.loads(response.body) assert response.content_type == 'application/json' assert type(resp) == type({}) assert resp['application_settings']['object_language_name'] == \ application_settings.object_language_name # Valid id with GET params. Param values are treated as strings, not # JSON. If any params are specified, the default is to return a JSON # array corresponding to store for the param. There are three cases # that will result in an empty JSON array being returned: # 1. the param is not specified # 2. the value of the specified param is an empty string # 3. the value of the specified param is an ISO 8601 UTC datetime # string that matches the most recent datetime_modified value of the # store in question. # Get the data currently in the db (see websetup.py for the test data). data = { 'languages': h.get_languages(), 'users': h.get_mini_dicts_getter('User')(), 'orthographies': h.get_mini_dicts_getter('Orthography')() } # JSON.stringify and then re-Python-ify the data. This is what the data # should look like in the response to a simulated GET request. data = json.loads(json.dumps(data, cls=h.JSONOLDEncoder)) params = { # Value is a non-empty string: 'users' will be in response. 'users': 'give me some users!', # Value is empty string: 'languages' will not be in response. 'languages': '', # Value is ISO 8601 UTC datetime string that does not match the most # recent Orthography.datetime_modified value: 'orthographies' *will* # be in the response. 'orthographies': datetime.datetime.utcnow().isoformat(), } response = self.app.get(url('edit_applicationsetting', id=application_settings_id), params, extra_environ=self.extra_environ_admin) resp = json.loads(response.body) assert resp['data']['users'] == data['users'] assert resp['data']['languages'] == [] assert resp['data']['orthographies'] == data['orthographies'] # Invalid id with GET params. It should still return a 404 Not Found. params = { # If id were valid, this would cause a users array to be returned also. 'users': 'True', } response = self.app.get( url('edit_applicationsetting', id=id), params, extra_environ=self.extra_environ_admin, status=404) assert json.loads(response.body)['error'] == \ u'There is no application settings with id %s' % id