def test_creates_profile(self): """Should create a profile if a user doesn't have one.""" user_without_profile = UserFactory() self.login(user_without_profile) self.client.get(self.get_url()) profile = user_without_profile.get_profile() self.assertTrue(profile.pk)
def setUp(self): self.entry = factories.NewsEntryFactory(pub_date=now() - timedelta(days=1)) self.en_trans = self.entry.translations.get(language_code='en') self.user = UserFactory() self.admin = UserFactory(is_superuser=True)
def setUp(self): self.user = UserFactory() self.content_object = UserFactory() self.review = factories.ReviewFactory( user=self.user, reviewed_item=self.content_object) self.other_user = UserFactory()
def setUp(self): super(DashboardRenderWidgetViewTestCase, self).setUp() dashboard_widget_pool.discover_widgets() self.superuser = UserFactory(is_superuser=True) self.normal_user = UserFactory() self.data = { 'name': 'DummyWidget', }
def setUp(self): super(DashboardLastUpdateViewTestCase, self).setUp() self.superuser = UserFactory(is_superuser=True) self.normal_user = UserFactory() # This ensures that we have one last update in the database dashboard_widget_pool.get_widgets_that_need_update() time.sleep(1)
def test_create_event(self): user = UserFactory() content_object = UserFactory() event = ObjectEvent.create_event(user, content_object) self.assertIsInstance(event, ObjectEvent) self.assertEqual(ObjectEvent.objects.all().count(), 1) self.assertEqual(ObjectEventType.objects.all().count(), 1) # Test with a new content_object, created for this event event_content_object = UserFactory() event = ObjectEvent.create_event(user, content_object, event_content_object) self.assertEqual(event.event_content_object, event_content_object)
def setUp(self): self.user = UserFactory() # User needs this permission to access the create view. self.user.user_permissions.add( Permission.objects.get(codename="add_tinylink")) self.user.user_permissions.add( Permission.objects.get(codename="delete_tinylink")) self.tinylink = TinylinkFactory(user=self.user) self.second_user = UserFactory() self.second_user.user_permissions.add( Permission.objects.get(codename="add_tinylink")) self.second_user.user_permissions.add( Permission.objects.get(codename="delete_tinylink")) self.staff = UserFactory(is_staff=True, is_superuser=True)
def test_get_user(self): self.assertEqual(self.review.get_user(), 'Anonymous', msg=( 'Should return anonymous.')) self.user = UserFactory() self.review.user = self.user self.assertEqual(self.review.get_user(), self.user.email, msg=( 'Should return a user\'s email.'))
def test_form(self): data = { 'feedback-email': '*****@*****.**', 'feedback-message': 'Foo', 'feedback-url': 'http://www.example.com', } form = FeedbackForm(data=data) self.assertTrue(form.is_valid()) form.save() self.assertEqual(Message.objects.all().count(), 0) self.assertEqual(Feedback.objects.all().count(), 0) # Valid post data.update({'feedback-url': ''}) form = FeedbackForm(data=data) self.assertTrue(form.is_valid()) form.save() self.assertEqual(Message.objects.all().count(), 1) self.assertEqual(Feedback.objects.all().count(), 1) self.assertEqual(Feedback.objects.all()[0].message, 'Foo') self.assertEqual(Feedback.objects.all()[0].email, '*****@*****.**') Feedback.objects.all()[0].delete() Message.objects.all()[0].delete() # Valid post with user account user = UserFactory() data.update({'feedback-email': ''}) form = FeedbackForm(data=data, user=user) self.assertTrue(form.is_valid()) form.save() self.assertEqual(Message.objects.all().count(), 1) self.assertEqual(Feedback.objects.all().count(), 1) self.assertEqual(Feedback.objects.all()[0].message, 'Foo') self.assertEqual(Feedback.objects.all()[0].user, user)
def test_adds_tag_list_to_form(self): """ Should add the available tags for each given tag field to the form. This enables users to do this in their templates:: $(document).ready(function() { $('#id_skills').tagit({ allowSpaces: true ,availableTags: {{ form.available_tags_technical_skills|safe }} ,caseSensitive: false ,removeConfirmation: true }); } """ form = DummyModelForm(self.user, data=self.data) form.save() result = form.tags_tags_values() self.assertEqual(result, '["cinema", "family", "great day"]') result = form.global_tags_tags_values() self.assertEqual(result, '["bar", "foo"]') user2 = UserFactory() form = DummyModelForm(user2) result = form.tags_tags_values() self.assertEqual(result, '[]', msg=( 'A user should not be able to see the private tags of another' ' user.')) form = DummyModelForm() result = form.tags_tags_values() self.assertEqual(result, '[]', msg=( 'An anonymous user should not be able to see user specific tags.'))
def test_handler(self): user = UserFactory() profile = UserProfileFactory(user=user) #empty response facebook_extra_values(self, user, {}, {}) # This is how the response from facebook looks like. It contains many # more values but we only use these few. response = { 'birthday': '09/08/1982', 'gender': 'male', 'username': '******', 'first_name': 'Foo', 'last_name': 'Bar', 'location': { 'id': '101883206519751', 'name': 'Singapore, Singapore', }, } facebook_extra_values(self, user, response, {}) profile = UserProfile.objects.get(pk=profile.pk) self.assertEqual(profile.birthday, timezone.datetime(1982, 9, 8).date(), msg=('Should set the birthday correctly')) self.assertEqual(profile.gender, 'male', msg=('Should set the gender correctly')) self.assertEqual(profile.location, 'Singapore, Singapore', msg=('Should set the location correctly'))
def setUp(self): self.user = UserFactory() self.item = ItemFactory() self.paypal_response = { 'ACK': ['Success'], 'TOKEN': ['abc123'], } self.expected_response = { 'USER': settings.PAYPAL_USER, 'PWD': settings.PAYPAL_PWD, 'SIGNATURE': settings.PAYPAL_SIGNATURE, 'VERSION': '91.0', 'PAYMENTREQUEST_0_PAYMENTACTION': 'Sale', 'METHOD': 'SetExpressCheckout', 'L_PAYMENTREQUEST_0_NAME0': self.item.name, 'L_PAYMENTREQUEST_0_DESC0': self.item.description, 'L_PAYMENTREQUEST_0_AMT0': self.item.value, 'L_PAYMENTREQUEST_0_QTY0': 1, 'PAYMENTREQUEST_0_AMT': self.item.value, 'PAYMENTREQUEST_0_ITEMAMT': self.item.value, 'RETURNURL': settings.HOSTNAME + reverse('paypal_confirm'), 'CANCELURL': settings.HOSTNAME + reverse('paypal_canceled') } self.data = { 'item': self.item.pk, 'quantity': 1, } self.old_call_paypal = PayPalFormMixin.call_paypal PayPalFormMixin.call_paypal = Mock(return_value=self.paypal_response)
def setUp(self): """Creates a user and valid set of form data.""" self.user = UserFactory() self.data = { 'name': 'dummy', 'tags': 'great day,family, cinema ', 'global_tags': 'foo, bar', }
def test_save_with_user(self): """Should create a checkin when user is given.""" user = UserFactory() form = CheckinCreateForm(user=user, place=self.place, data=self.data) self.assertTrue(form.is_valid(), msg=('Errors: {0}'.format(form.errors.items()))) instance = form.save() self.assertTrue(instance.pk)
def setUp(self): self.tagged_item = factories.TaggedItemFactory() self.dummy = self.tagged_item.object self.tag = self.tagged_item.tag self.other_tagged_item = factories.TaggedItemFactory(object=self.dummy) factories.TaggedItemFactory() self.user_item = factories.TaggedItemFactory(object=UserFactory())
def test_method(self): user = UserFactory() create_profile_for_new_user(user) self.assertEqual( UserProfile.objects.all().count(), 1, msg=( 'When called with a new user it should create a new profile for' ' this user'))
def test_save(self): """Should create a new subscription.""" user = UserFactory() dummy = DummyModelFactory() form = SubscriptionCreateForm(user=user, content_object=dummy, data={}) self.assertTrue(form.is_valid(), msg=('Errors: {0}'.format(form.errors.items()))) instance = form.save() self.assertTrue(instance.pk)
def test_handler(self): user = UserFactory() new_users_handler(self, user, {}, {}) self.assertEqual( UserProfile.objects.all().count(), 1, msg=( 'When called with a new user it should create a new profile for' ' this user'))
def test_should_save_user_profile(self): user = UserFactory.create() user_registered_handler(self, user, {}) self.assertEqual( UserProfile.objects.all().count(), 1, msg=( 'When called with a new user it should create a new profile for' ' this user'))
class DatedValueFactory(factory.DjangoModelFactory): """Factory for the ``DatedValue`` model.""" FACTORY_FOR = DatedValue date = now() object = factory.LazyAttribute(lambda d: UserFactory()) object_id = factory.LazyAttribute(lambda d: d.object.id) type = factory.SubFactory( 'dated_values.tests.factories.DatedValueTypeFactory') value = Decimal('123.12345678')
def handle(self, *args, **options): # NOTE WARNING! # When you use this in a test run, due to it being spawned in a # subprocess, it will write into the real database, not the test-only # database! Still need to figure out how to get around this. # Keeping this though for manual testing. UserFactory() # many commands write to stderr, maybe we can fetch that later sys.stderr.write('Command output to stderr.\n') # maybe some also use stdout sys.stderr.write('Command output to stdout.\n')
def setUp(self): self.type1 = DatedValueTypeFactory() self.type2 = DatedValueTypeFactory() self.types = [self.type1, self.type2] self.user = UserFactory() self.staff = UserFactory(is_staff=True) self.superuser = UserFactory(is_superuser=True) self.ctype = ContentType.objects.get_for_model(User) self.data = { 'date': (now() + relativedelta(days=30)).strftime( app_settings.DATE_FORMAT), 'form-TOTAL_FORMS': u'2', 'form-INITIAL_FORMS': u'2', } for i in range(0, 2): for j in range(0, 14): self.data.update( {'form-{0}-value{1}'.format(i, j): '{0}.12'.format(j)})
def setUp(self): self.type1 = DatedValueTypeFactory() self.type2 = DatedValueTypeFactory() self.types = [self.type1, self.type2] self.user = UserFactory() self.data = { 'form-TOTAL_FORMS': u'2', 'form-INITIAL_FORMS': u'2', } for i in range(0, 2): for j in range(0, 14): self.data.update( {'form-{0}-value{1}'.format(i, j): '{0}.12'.format(j)})
def setUp(self): self.user = UserFactory() self.item = ItemFactory() self.paypal_response = { 'ACK': ['Success'], 'TOKEN': ['abc123'], } self.data = { 'item': self.item.pk, 'quantity': 1, }
def setUp(self): self.content_object = UserFactory() self.review = factories.ReviewFactory( reviewed_item=self.content_object) self.rating1 = factories.RatingFactory(review=self.review, value='4') # we create choices to simulate, that the previous value was the max for i in range(0, 5): factories.RatingCategoryChoiceFactory( ratingcategory=self.rating1.category, value=i) self.rating2 = factories.RatingFactory(review=self.review, value='6') # we create choices to simulate, that the previous value was the max for i in range(0, 7): factories.RatingCategoryChoiceFactory( ratingcategory=self.rating2.category, value=i)
def test_get_username(self): user = UserFactory() resp = get_username(details={'email': user.email}, user=user) self.assertEqual( resp, {'username': user.username}, msg=('Should respond the username. Response was {0}'.format(resp))) resp = get_username(details={'email': user.email}) self.assertEqual( resp, {'username': user.username}, msg=('Should respond the username. Response was {0}'.format(resp))) resp = get_username(details={'email': '*****@*****.**'}) self.assertTrue(resp['username'], msg=('Should return a generated username. Response was' ' {0}'.format(resp['username'])))
def setUp(self): super(GetFilesForDocumentTestCase, self).setUp() self.user = UserFactory() self.img = create_image() self.image_name = 'test_file.jpg' self.filename = os.path.join(settings.MEDIA_ROOT, self.image_name) self.img.save(self.filename, 'JPEG') self.file_obj = DjangoFile(open(self.filename), name=self.image_name) self.image = Image.objects.create(owner=self.user, original_filename=self.image_name, file=self.file_obj) self.doc_en = DocumentTitleENFactory(filer_file=self.image) self.doc_de = DocumentTitleDEFactory(document=self.doc_en.document, filer_file=self.image)
def test_tag(self): # create context mock request = RequestFactory().get('/') request.user = AnonymousUser() SessionMiddleware().process_request(request) request.session.save() context = RequestContext(request) # Returns empty dict if there is no authenticated user self.assertEqual(render_notifications(context), '\n') # Returns notifications.html request.user = UserFactory() ObjectEventFactory(user=request.user) self.assertTrue(render_notifications(context))
def test_render_tag(self): request = RequestFactory().get('/') request.user = AnonymousUser() c = RequestContext(request) t = Template('{% load feedback_tags %}{% feedback_form %}') self.assertIn('<input type="submit" value="Send Feedback" />', t.render(c)) # Should add the email field, because user is anonymous self.assertIn('email', t.render(c)) # Test with logged in user request.user = UserFactory() c = RequestContext(request) self.assertIn('<input type="submit" value="Send Feedback" />', t.render(c)) self.assertNotIn('email', t.render(c))
def setUp(self): self.user = UserFactory() self.transaction = PaymentTransactionFactory(user=self.user) self.item = ItemFactory() self.paypal_response = { 'ACK': ['Success'], 'TOKEN': ['abc123'], 'PAYMENTINFO_0_TRANSACTIONID': ['abc123'] } self.data = { 'item': self.item.pk, 'quantity': 1, } self.old_call_paypal = PayPalFormMixin.call_paypal PayPalFormMixin.call_paypal = Mock(return_value=self.paypal_response)
def setUp(self): self.content_object = UserFactory() self.user = UserFactory()
class ReviewCreateViewTestCase(ViewTestMixin, TestCase): def setUp(self): self.content_object = UserFactory() self.user = UserFactory() def get_view_name(self): return 'review_create' def get_view_kwargs(self): return { 'content_type': ContentType.objects.get_for_model( self.content_object), 'object_id': self.content_object.pk, } def user_has_perm(self, user): # Simulates permission for the current user if user.first_name: return True return False def test_view(self): wrong_kwargs = { 'content_type': 'Foo', 'object_id': self.content_object.pk, } self.is_not_callable(kwargs=wrong_kwargs) wrong_kwargs = { 'content_type': ContentType.objects.get_for_model( self.content_object), 'object_id': '999', } self.is_not_callable(kwargs=wrong_kwargs) self.is_callable(user=self.user, message=( 'View should be callable by an authenticated user.')) with self.settings(REVIEW_AVOID_MULTIPLE_REVIEWS=True): has_perm = lambda u, item: self.user_has_perm(u) with self.settings(REVIEW_PERMISSION_FUNCTION=has_perm): self.is_not_callable(method='post', user=self.user, message=( 'View should not be callable due to missing permissions.')) self.assertEqual(Review.objects.count(), 0, msg=( 'No reviews should\'ve been created.')) self.user.first_name = 'Foo' self.user.save() self.is_callable(method='post', user=self.user, message=( 'View should be callable by an authenticated user.')) self.assertEqual(Review.objects.count(), 1, msg=( 'One review should\'ve been created.')) self.is_callable( and_redirects_to=reverse('review_update', kwargs={ 'pk': Review.objects.all()[0].pk}), method='post', user=self.user, message=( 'View should redirect, if review alreasy exists.')) self.assertEqual(Review.objects.count(), 1, msg=( 'No new review should\'ve been created.')) with self.settings(REVIEW_ALLOW_ANONYMOUS=True): self.is_callable(anonymous=True, message=( 'View should be callable as an anonymous user.'))
def test_should_save_user_profile(self): user = UserFactory.create() user_registered_handler(self, user, {}) self.assertEqual(UserProfile.objects.all().count(), 1, msg=( 'When called with a new user it should create a new profile for' ' this user'))