def test_list_proposed_item_template_cant_edit_closed_and_not_owner( self, client, user, charity ): proposed = ProposedItem.objects.create( entity=charity, user=UserFactory.create(), closed=True ) _shortcut_test_proposed_template_test(proposed, client, user=user)
def test_clean_username(self): # A user with proto_user params does not exist yet. proto_user = UserFactory.build() form = UserCreationForm({ "email": proto_user.email, "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, "g-recaptcha-response": "PASSED", "terms": True, }) assert form.is_valid() assert form.clean_username() == proto_user.username # Creating a user. form.save() # The user with proto_user params already exists, # hence cannot be created. form = UserCreationForm({ "email": proto_user.email, "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, "g-recaptcha-response": "PASSED", "terms": True, }) assert not form.is_valid() assert len(form.errors) == 1 assert "username" in form.errors
def test_clean_terms(self): # A user with proto_user params does not exist yet. proto_user = UserFactory.build() form = UserCreationForm({ "email": proto_user.email, "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, "g-recaptcha-response": "PASSED", "terms": False, }) assert not form.is_valid() assert len(form.errors) == 1 assert "terms" in form.errors form = UserCreationForm({ "email": proto_user.email, "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, "g-recaptcha-response": "PASSED", "terms": True, }) assert form.is_valid()
def test_authenticated(self, user: User, rf: RequestFactory): request = rf.get("/fake-url/") request.user = UserFactory() response = user_detail_view(request, username=user.username) assert response.status_code == 200
def test_regular_voted_minority(self, admin_user, thread, charity): setup_thread(thread, charity) UserVoteFactory.create_batch(3, thread=thread, direction=True) UserVoteFactory.create_batch(5, thread=thread, direction=False) UserVoteFactory.create(thread=thread, direction=True, user=UserFactory.create(is_staff=True)) UserVoteFactory.create(thread=thread, direction=True, user=admin_user) assert WantedItem.objects.count() == 0
def test_complete_merge(self, admin_user, thread, charity): setup_thread(thread, charity) UserVoteFactory.create_batch(6, thread=thread, direction=True) UserVoteFactory.create_batch(4, thread=thread, direction=False) UserVoteFactory.create(thread=thread, direction=True, user=UserFactory.create(is_staff=True)) assert WantedItem.objects.count() == 0 UserVoteFactory.create(thread=thread, direction=True, user=admin_user) assert WantedItem.objects.count() == 10 assert ProposedItem.objects.count() == 1 assert ProposedItem.objects.first().closed is True
def setup_thread(thread, entity): thread.type = 4 user = UserFactory.create() items = [x.id for x in ItemFactory.create_batch(10)] proposed = ProposedItem.objects.create(user_id=user.id, entity=entity, item=items, item_condition=[2] * len(items)) thread.extra = { "OP_id": user.id, "entity_id": entity.id, "proposed_item_id": proposed.id, } thread.save()
def test_form_not_update_for_wrong_user(self, client, user, charity): item = [x.id for x in ItemFactory.create_batch(3)] name = [_random_string() for _ in range(3)] obj = ProposedItem.objects.create( user=user, entity=charity, item=item[:2], names=name[:2] ) client.force_login(UserFactory.create()) response = client.post( self.view_url, data={ "id": obj.id, "item": _format_list_to_str(item), "item_condition": _format_list_to_str([3] * len(item)), "names": _format_list_to_str(name), "names_condition": _format_list_to_str([3] * len(name)), }, ) assert response.status_code == 200 assert ProposedItem.objects.count() == 1 obj = ProposedItem.objects.first() assert obj.item == item[:2] assert obj.names == name[:2]
def handle(self, *args, **options): if not settings.DEBUG: raise Exception("You cannot use this command in production.") print("Creating Item app data.") org_1 = Charity.objects.create( name="Org 1", link="https://google.com/", description="Desc.", how_to_donate="Address", ) org_2 = Charity.objects.create( name="Org 2", link="https://google.com/", description="Desc.", how_to_donate="Address", ) org_3 = Charity.objects.create( name="Org 3", link="https://google.com/", description="Desc.", how_to_donate="Address", ) item_1 = Item.objects.create(name="pan") item_2 = Item.objects.create(name="canned food") Item.objects.create(name="chair") item_3 = Item.objects.create(name="fork") item_4 = Item.objects.create(name="blood") WantedItem.objects.create(item=item_1, charity=org_1) # 3 orgs WantedItem.objects.create(item=item_2, charity=org_1) # 2 orgs WantedItem.objects.create(item=item_3, charity=org_1) # 2 orgs WantedItem.objects.create(item=item_4, charity=org_1) # 1 org WantedItem.objects.create(item=item_1, charity=org_2) WantedItem.objects.create(item=item_2, charity=org_2) WantedItem.objects.create(item=item_1, charity=org_3) WantedItem.objects.create(item=item_3, charity=org_3) ProposedItem.objects.create( entity=org_1, user=UserFactory.create(), item=[item_1.id] + [x.id for x in ItemFactory.create_batch(10)], names=[Faker().bs() for _ in range(50)], ) ProposedItem.objects.create( entity=org_2, user=UserFactory.create(), item=[item_3.id] + [x.id for x in ItemFactory.create_batch(10)], names=[Faker().bs() for _ in range(50)], ) # For testing pagination - change to 1 in paginating WantedItemFactory.create_batch(25, item=item_1) # For testing proposed item, if user selects a generic # item, then all of its children are added as well. # If no parent, then select that only. GH #3 parent = Item.objects.create(name="parent1") [ Item.objects.create(parent=parent, name=f"child{x}") for x in range(7) ] # Also, during searching, if a generic item appears # we warn the user that they should check the list beforehand. print("Finished creating Item app data.")
def add_break_even_amount(thread): UserVoteFactory.create_batch(4, thread=thread, direction=True) UserVoteFactory.create_batch(4, thread=thread, direction=False) UserVoteFactory.create(thread=thread, direction=True, user=UserFactory.create(is_staff=True))
def test_case_sensitivity(self, rf: RequestFactory): request = rf.get("/fake-url/") request.user = UserFactory(username="******") with pytest.raises(Http404): user_detail_view(request, username="******")
def user() -> User: return UserFactory()