def test_signup(self): """Posting the required information to sign up should create an account, log the user into the account, create a profile for their account, and return a redirect to the profile page.""" data = { 'username': '******', 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'password1': 'test', 'password2': 'test', } response = http_post_response(self.url, self.view, data) eq_(response.status_code, 302, 'The response should redirect.') eq_(response.url, reverse('profile', host='foiamachine')) user = auth.models.User.objects.get(username=data['username']) ok_(user, 'The user should be created.') ok_(user.profile, 'The user should be given a profile.')
def test_post_view(self, mock_subscribe): """Posting an email to the list should add that email to our MailChimp list.""" form = NewsletterSignupForm({ 'email': '*****@*****.**', 'list': settings.MAILCHIMP_LIST_DEFAULT }) ok_(form.is_valid(), 'The form should validate.') response = http_post_response(self.url, self.view, form.data) mock_subscribe.assert_called_with( ANY, form.data['email'], form.data['list'], source='Newsletter Sign Up Form', url='https://localhost:8000/newsletter-post/', ) eq_( response.status_code, 302, 'Should redirect upon successful submission.' )
def test_create(self): """Posting a valid creation form should create a request and redirect to it.""" title = 'Test Request' request_language = 'Lorem ipsum' jurisdiction = StateJurisdictionFactory().id form = forms.FoiaMachineRequestForm({ 'title': title, 'status': 'started', 'request_language': request_language, 'jurisdiction': jurisdiction }) ok_(form.is_valid()) response = http_post_response(self.url, self.view, form.data, self.user) eq_(response.status_code, 302, 'When successful the view should redirect to the request.') foi = models.FoiaMachineRequest.objects.first() eq_(response.url, foi.get_absolute_url()) ok_(foi.communications.count() == 1, 'A communication should be created.')
def test_post(self): """Posting a valid ProjectForm should create the project.""" form = forms.ProjectCreateForm({ 'title': 'Cool Project', 'summary': 'Yo my project is cooler than LIFE!', 'image': test_image, 'tags': 'dogs, cats', 'private': True, 'featured': True }) ok_(form.is_valid(), 'The form should validate.') staff_user = UserFactory(is_staff=True) response = http_post_response(self.url, self.view, form.data, staff_user) project = models.Project.objects.last() eq_(response.status_code, 302, 'The response should redirect to the project when it is created.') ok_( staff_user in project.contributors.all(), 'The current user should automatically be added as a contributor.')
def test_donate(self): """Donations should have a token, email, and amount. An email receipt should be sent for the donation.""" token = 'test' email = '*****@*****.**' amount = 500 data = { 'stripe_token': token, 'stripe_email': email, 'stripe_amount': amount, 'type': 'one-time', } form = self.form(data) form.is_valid() ok_(form.is_valid(), 'The form should validate. %s' % form.errors) response = http_post_response(self.url, self.view, data) eq_( response.status_code, 302, 'A successful donation will return a redirection.' )
def test_donate(self): """Donations should have a token, email, and amount. An email receipt should be sent for the donation.""" token = "test" email = "*****@*****.**" amount = 500 data = { "stripe_token": token, "stripe_email": email, "stripe_amount": amount, "type": "one-time", } form = self.form(data) form.is_valid() ok_(form.is_valid(), "The form should validate. %s" % form.errors) response = http_post_response(self.url, self.view, data) eq_( response.status_code, 302, "A successful donation will return a redirection.", )
def test_create(self): """Posting a valid creation form should create a request and redirect to it.""" title = "Test Request" request_language = "Lorem ipsum" jurisdiction = StateJurisdictionFactory().id form = forms.FoiaMachineRequestForm({ "title": title, "status": "started", "request_language": request_language, "jurisdiction": jurisdiction, }) ok_(form.is_valid()) response = http_post_response(self.url, self.view, form.data, self.user) eq_( response.status_code, 302, "When successful the view should redirect to the request.", ) foi = models.FoiaMachineRequest.objects.first() eq_(response.url, foi.get_absolute_url()) ok_(foi.communications.count() == 1, "A communication should be created.")
def test_post(self): """Posting updated request info should update the request!""" new_jurisdiction = StateJurisdictionFactory() data = { "title": "New Title", "status": "done", "request_language": "Foo bar baz!", "jurisdiction": new_jurisdiction.id, } form = forms.FoiaMachineRequestForm(data, instance=self.foi) ok_(form.is_valid()) response = http_post_response(self.url, self.view, data, self.foi.user, **self.kwargs) self.foi.refresh_from_db() # we have to update the slug, because the title changed self.kwargs["slug"] = self.foi.slug eq_(response.status_code, 302) eq_(response.url, reverse("foi-detail", host="foiamachine", kwargs=self.kwargs)) eq_(self.foi.title, data["title"]) eq_(self.foi.status, data["status"]) eq_(self.foi.request_language, data["request_language"]) eq_(self.foi.jurisdiction, new_jurisdiction)
def test_post(self): """Posting updated request info should update the request!""" new_jurisdiction = StateJurisdictionFactory() data = { 'title': 'New Title', 'status': 'done', 'request_language': 'Foo bar baz!', 'jurisdiction': new_jurisdiction.id } form = forms.FoiaMachineRequestForm(data, instance=self.foi) ok_(form.is_valid()) response = http_post_response(self.url, self.view, data, self.foi.user, **self.kwargs) self.foi.refresh_from_db() # we have to update the slug, because the title changed self.kwargs['slug'] = self.foi.slug eq_(response.status_code, 302) eq_(response.url, reverse('foi-detail', host='foiamachine', kwargs=self.kwargs)) eq_(self.foi.title, data['title']) eq_(self.foi.status, data['status']) eq_(self.foi.request_language, data['request_language']) eq_(self.foi.jurisdiction, new_jurisdiction)
def http_get_post(url, view, data): """Performs both a GET and a POST on the same url and view.""" get_response = http_get_response(url, view) post_response = http_post_response(url, view, data) return (get_response, post_response)
def test_logged_in_post(self): """Posting valid data while logged in should redirect without creating a new user.""" user = UserFactory() response = http_post_response(self.url, self.view, self.data, user) eq_(response.status_code, 302) User.objects.get(username=self.data['username'])
def test_downgrade_logged_out(self, mock_unsubscribe): """Logged out users cannot downgrade.""" data = {'action': 'downgrade'} response = http_post_response(self.url, self.view, data) eq_(response.status_code, 200) ok_(not mock_unsubscribe.called)
def test_downgrade_not_pro(self, mock_unsubscribe): """A user who is not a pro cannot downgrade.""" data = {'action': 'downgrade'} response = http_post_response(self.url, self.view, data, self.user) eq_(response.status_code, 200) ok_(not mock_unsubscribe.called)
def test_upgrade_logged_out(self, mock_subscribe): """Logged out users should not be able to upgrade.""" data = {'action': 'upgrade', 'stripe_token': 'test'} response = http_post_response(self.url, self.view, data) eq_(response.status_code, 200) ok_(not mock_subscribe.called)
def test_upgrade(self, mock_subscribe): """Logged in users should be able to upgrade to Pro accounts.""" data = {'action': 'upgrade', 'stripe_token': 'test'} response = http_post_response(self.url, self.view, data, self.user) eq_(response.status_code, 200) mock_subscribe.assert_called_once_with(data['stripe_token'])
def test_post(self): """Posting to the delete view should delete the communication.""" data = {} user = self.comm.request.user http_post_response(self.url, self.view, data, user, **self.kwargs) self.comm.refresh_from_db()
def test_post(self): """Posting to the delete view should delete the request.""" data = {} http_post_response(self.url, self.view, data, self.foi.user, **self.kwargs) self.foi.refresh_from_db()