def test_foia_create(self): """Test creating a FOIA through the API""" attachment_url = "http://www.example.com/attachment.txt" self.mocker.get( attachment_url, headers={"Content-Type": "text/plain"}, text="Attachment content here", ) agency = AgencyFactory() user = UserFactory.create(membership__organization__number_requests=5) Token.objects.create(user=user) data = { "jurisdiction": agency.jurisdiction.pk, "agency": agency.pk, "document_request": "The document", "title": "Title", "attachments": [attachment_url], } headers = { "content-type": "application/json", "HTTP_AUTHORIZATION": "Token %s" % user.auth_token, } response = self.client.post(reverse("api-foia-list"), json.dumps(data), content_type="application/json", **headers) eq_(response.status_code, 201, response) eq_(len(response.json()["Requests"]), 1)
def test_foia_create(self): """Test creating a FOIA through the API""" attachment_url = 'http://www.example.com/attachment.txt' self.mocker.get( attachment_url, headers={'Content-Type': 'text/plain'}, text='Attachment content here', ) agency = AgencyFactory() user = UserFactory.create(membership__organization__number_requests=5) Token.objects.create(user=user) data = { 'jurisdiction': agency.jurisdiction.pk, 'agency': agency.pk, 'document_request': 'The document', 'title': 'Title', 'attachments': [attachment_url], } headers = { 'content-type': 'application/json', 'HTTP_AUTHORIZATION': 'Token %s' % user.auth_token, } response = self.client.post(reverse('api-foia-list'), json.dumps(data), content_type='application/json', **headers) eq_(response.status_code, 201, response)
def api_call(self, data=None, user_kwargs=None, code=201, status=None): """Helper for API calls""" if data is None: data = {} if 'agency' not in data: data['agency'] = AgencyFactory().pk, if 'title' not in data: data['title'] = 'Title' if 'document_request' not in data: data['document_request'] = 'Document Request' password = '******' user_kwargs_defaults = { 'password': password, 'profile__num_requests': 5, } if user_kwargs is not None: user_kwargs_defaults.update(user_kwargs) user = UserFactory.create(**user_kwargs_defaults) response = self.client.post( reverse('api-token-auth'), { 'username': user.username, 'password': password }, ) headers = { 'content-type': 'application/json', 'HTTP_AUTHORIZATION': 'Token %s' % response.json()['token'], } response = self.client.post(reverse('api-foia-list'), json.dumps(data), content_type='application/json', **headers) eq_(response.status_code, code, response) if status: eq_(response.json()['status'], status)
def test_foia_create(self, mock): """Test creating a FOIA through the API""" attachment_url = 'http://www.example.com/attachment.txt' mock.get( attachment_url, headers={'Content-Type': 'text/plain'}, text='Attachment content here', ) agency = AgencyFactory() password = '******' user = UserFactory.create( password=password, profile__num_requests=5, ) data = { 'jurisdiction': agency.jurisdiction.pk, 'agency': agency.pk, 'document_request': 'The document', 'title': 'Title', 'attachments': [attachment_url], } response = self.client.post( reverse('api-token-auth'), { 'username': user.username, 'password': password }, ) headers = { 'content-type': 'application/json', 'HTTP_AUTHORIZATION': 'Token %s' % response.json()['token'], } response = self.client.post(reverse('api-foia-list'), json.dumps(data), content_type='application/json', **headers) eq_(response.status_code, 201, response)