def test_with_other_user(self): Ownable, OwnableForm = self.factory() user = UserFactory() other = UserFactory() login_user(user) form = OwnableForm(MultiDict({'owner': str(other.id)})) self.assertEqual(form.owner.data, other) form.validate() self.assertIn('owner', form.errors) self.assertEqual(len(form.errors['owner']), 1)
def test_not_member(self): Ownable, OwnableForm = self.factory() member = Member(user=UserFactory(), role='editor') org = OrganizationFactory(members=[member]) login_user(UserFactory()) form = OwnableForm(MultiDict({'organization': str(org.id)})) self.assertEqual(form.organization.data, org) self.assertFalse(form.validate()) self.assertIn('organization', form.errors) self.assertEqual(len(form.errors['organization']), 1)
def test_organization_over_owner(self): Ownable, OwnableForm = self.factory() user = UserFactory() member = Member(user=user, role='editor') org = OrganizationFactory(members=[member]) login_user(user) form = OwnableForm( MultiDict({ 'owner': str(user.id), 'organization': str(org.id) })) self.assertEqual(form.owner.data, user) self.assertEqual(form.organization.data, org) form.validate() self.assertEqual(form.errors, {}) self.assertIsNone(form.owner.data) self.assertEqual(form.organization.data, org) ownable = Ownable() form.populate_obj(ownable) self.assertIsNone(ownable.owner) self.assertEqual(ownable.organization, org)
def test_my_org_issues(self): user = self.login() member = Member(user=user, role='editor') organization = OrganizationFactory(members=[member]) reuse = ReuseFactory(owner=user) org_reuse = ReuseFactory(organization=organization) dataset = VisibleDatasetFactory(owner=user) org_dataset = VisibleDatasetFactory(organization=organization) sender = UserFactory() issues = [ DatasetIssue.objects.create(subject=s, title='', user=sender) for s in (dataset, org_dataset) ] + [ ReuseIssue.objects.create(subject=s, title='', user=sender) for s in (reuse, org_reuse) ] # Should not be listed DatasetIssue.objects.create(subject=VisibleDatasetFactory(), title='', user=sender) ReuseIssue.objects.create(subject=ReuseFactory(), title='', user=sender) response = self.get(url_for('api.my_org_issues')) self.assert200(response) self.assertEqual(len(response.json), len(issues))
def test_with_initial_owner(self): Ownable, OwnableForm = self.factory() user = UserFactory() ownable = Ownable(owner=user) form = OwnableForm(None, ownable) self.assertEqual(form.owner.data, user) self.assertIsNone(form.organization.data)
def test_initial_value(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) ownable = Ownable(owner=user) form = OwnableForm(None, ownable) self.assertEqual(form.owner.data, user)
def test_with_user_not_logged_found(self): Ownable, OwnableForm = self.factory() user = UserFactory() form = OwnableForm(MultiDict({'owner': str(user.id)})) form.validate() self.assertIn('owner', form.errors) self.assertEqual(len(form.errors['owner']), 1)
def test_list_sources_for_owner(self): owner = UserFactory() sources = HarvestSourceFactory.create_batch(3, owner=owner) HarvestSourceFactory() response = self.get(url_for('api.harvest_sources', owner=str(owner.id))) self.assert200(response) self.assertEqual(len(response.json), len(sources))
def test_with_initial_organization_and_user_logged(self): Ownable, OwnableForm = self.factory() user = UserFactory() org = OrganizationFactory() ownable = Ownable(organization=org) login_user(user) form = OwnableForm(None, ownable) self.assertIsNone(form.owner.data) self.assertEqual(form.organization.data, org)
def test_with_invalid_data(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) form = OwnableForm(MultiDict({'owner': str('wrongwith12c')})) form.validate() self.assertIn('owner', form.errors) self.assertEqual(len(form.errors['owner']), 1)
def test_empty_values(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) form = OwnableForm() self.assertEqual(form.owner.data, user) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user)
def test_create_and_retrieve_for_related(self): org = OrganizationFactory() FakeActivity.objects.create(actor=self.user, related_to=self.fake, organization=org) FakeActivity.objects.create(actor=UserFactory(), related_to=self.fake) activities = Activity.objects(related_to=self.fake) self.assertEqual(len(activities), 2) for activity in activities: self.assertIsInstance(activity, FakeActivity)
def test_list_sources_for_owner(self): owner = UserFactory() self.assertEqual(actions.list_sources(owner), []) sources = HarvestSourceFactory.create_batch(3, owner=owner) HarvestSourceFactory() result = actions.list_sources(owner) self.assertEqual(len(result), len(sources)) for source in sources: self.assertIn(source, result)
def test_add_badge_with_logged_user(self): '''It should track the user that add a badge''' user = UserFactory() fake = Fake.objects.create() login_user(user) result = fake.add_badge(TEST) self.assertEqual(len(fake.badges), 1) badge = fake.badges[0] self.assertEqual(result, badge) self.assertEqual(badge.kind, TEST) self.assertIsNotNone(badge.created) self.assertEqual(badge.created_by, user)
def test_pending_harvester_validations(self): source = HarvestSourceFactory() admin = AdminFactory() user = UserFactory() self.assertEqual(len(validate_harvester_notifications(user)), 0) notifications = validate_harvester_notifications(admin) self.assertEqual(len(notifications), 1) dt, details = notifications[0] self.assertEqualDates(dt, source.created_at) self.assertEqual(details['id'], source.id) self.assertEqual(details['name'], source.name)
def test_with_valid_user_self_json(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) form = OwnableForm.from_json({'owner': str(user.id)}) self.assertEqual(form.owner.data, user) form.validate() self.assertEqual(form.errors, {}) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user)
def test_empty_values_logged(self): Ownable, OwnableForm = self.factory() user = UserFactory() login_user(user) form = OwnableForm() self.assertEqual(form.owner.data, user) self.assertIsNone(form.organization.data) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user) self.assertIsNone(ownable.organization)
def test_with_initial_owner_and_no_data_provided(self): Ownable, OwnableForm = self.factory() user = UserFactory() ownable = Ownable(owner=user) form = OwnableForm(MultiDict({}), ownable) self.assertEqual(form.owner.data, user) login_user(user) form.validate() self.assertEqual(form.errors, {}) form.populate_obj(ownable) self.assertEqual(ownable.owner, user) self.assertIsNone(ownable.organization)
def test_with_initial_and_not_member(self): Ownable, OwnableForm = self.factory() user = UserFactory() org = OrganizationFactory(members=[Member(user=user, role='editor')]) neworg = OrganizationFactory() ownable = Ownable(organization=org) form = OwnableForm(MultiDict({'organization': str(neworg.id)}), ownable) self.assertEqual(form.organization.data, neworg) login_user(user) form.validate() self.assertIn('organization', form.errors) self.assertEqual(len(form.errors['organization']), 1)
def test_with_other_user_admin(self): Ownable, OwnableForm = self.factory() user = UserFactory() admin = AdminFactory() login_user(admin) form = OwnableForm(MultiDict({'owner': str(user.id)})) self.assertEqual(form.owner.data, user) form.validate() self.assertEqual(form.errors, {}) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user)
def test_with_initial_org_and_no_data_provided(self): Ownable, OwnableForm = self.factory() user = UserFactory() org = OrganizationFactory(members=[Member(user=user, role='editor')]) ownable = Ownable(organization=org) form = OwnableForm(MultiDict({}), ownable) self.assertEqual(form.organization.data, org) login_user(user) form.validate() self.assertEqual(form.errors, {}) form.populate_obj(ownable) self.assertIsNone(ownable.owner) self.assertEqual(ownable.organization, org)
def test_set_organization_if_permissions(self): Ownable, OwnableForm = self.factory() user = UserFactory() member = Member(user=user, role='editor') org = OrganizationFactory(members=[member]) ownable = Ownable(owner=user) login_user(user) form = OwnableForm(MultiDict({'organization': str(org.id)}), ownable) self.assertTrue(form.validate()) self.assertEqual(form.errors, {}) form.populate_obj(ownable) self.assertIsNone(ownable.owner) self.assertEqual(ownable.organization, org)
def test_with_organization_object_from_json(self): Ownable, OwnableForm = self.factory() user = UserFactory() member = Member(user=user, role='editor') org = OrganizationFactory(members=[member]) login_user(user) form = OwnableForm.from_json({'organization': {'id': str(org.id)}}) self.assertEqual(form.organization.data, org) form.validate() self.assertEqual(form.errors, {}) ownable = Ownable() form.populate_obj(ownable) self.assertIsNone(ownable.owner) self.assertEqual(ownable.organization, org)
def test_admin_can_set_organization(self): Ownable, OwnableForm = self.factory() user = UserFactory() org = OrganizationFactory() ownable = Ownable(owner=user) login_user(AdminFactory()) form = OwnableForm(MultiDict({'organization': str(org.id)}), ownable) self.assertEqual(form.owner.data, user) self.assertEqual(form.organization.data, org) self.assertTrue(form.validate()) self.assertEqual(form.errors, {}) self.assertIsNone(form.owner.data) self.assertEqual(form.organization.data, org) form.populate_obj(ownable) self.assertIsNone(ownable.owner) self.assertEqual(ownable.organization, org)