def setUp(self): self.user = UserFactory() self.public_node = ProjectFactory(creator=self.user, is_public=True) self.private_node = ProjectFactory(creator=self.user, is_public=False) self.request = RequestFactory().post('/fake_path') self.view = views.SpamUserDeleteView self.view = setup_log_view(self.view, self.request, guid=self.user._id)
def test_confirm_preprint_as_ham(self): view = views.PreprintConfirmHamView() view = setup_log_view(view, self.request, guid=self.preprint._id) view.delete(self.request) self.preprint.refresh_from_db() assert self.preprint.spam_status == 4
def test_remove_contributor(self, mock_remove_contributor): user_id = self.user_2._id node_id = self.node._id view = setup_log_view(self.view(), self.request, node_id=node_id, user_id=user_id) view.delete(self.request) mock_remove_contributor.assert_called_with(self.user_2, None, log=False)
def setUp(self): super(TestNodeDeleteView, self).setUp() self.node = ProjectFactory() self.request = RequestFactory().post('/fake_path') self.view = NodeDeleteView() self.view = setup_log_view(self.view, self.request, guid=self.node._id)
def test_no_log(self): view = setup_log_view(self.view, self.request, node_id=self.node._id, user_id=self.user_2._id) view.delete(self.request) nt.assert_not_equal(self.node.logs[-1].action, NodeLog.CONTRIB_REMOVED)
def test_confirm_node_as_ham(self): view = NodeConfirmHamView() view = setup_log_view(view, self.request, guid=self.node._id) view.post(self.request) self.node.refresh_from_db() nt.assert_true(self.node.spam_status == 4)
def test_confirm_registration_as_ham(self): view = NodeConfirmHamView() view = setup_log_view(view, self.request, guid=self.registration._id) view.delete(self.request) self.registration.refresh_from_db() nt.assert_true(self.registration.spam_status == 4)
def test_remove_contributor(self, mock_remove_contributor): user_id = self.user_2._id node_id = self.node._id view = setup_log_view(self.view(), self.request, guid=node_id, user_id=user_id) view.delete(self.request) mock_remove_contributor.assert_called_with(self.user_2, None, log=False)
def test_reindex_preprint_elastic(self, mock_update_search, preprint, req): count = AdminLogEntry.objects.count() view = views.PreprintReindexElastic() view = setup_log_view(view, req, guid=preprint._id) view.delete(req) assert mock_update_search.called assert AdminLogEntry.objects.count() == count + 1
def test_no_log(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2.id) view.post(self.request) assert self.preprint.logs.latest( ).action != PreprintLog.CONTRIB_REMOVED
def test_get_object(self): view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user._id) preprint, user = view.get_object() assert isinstance(preprint, Preprint) assert isinstance(user, OSFUser)
def test_integration_remove_contributor(self): assert self.user_2 in self.preprint.contributors view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user_2._id) count = AdminLogEntry.objects.count() view.delete(self.request) assert self.user_2 not in self.preprint.contributors assert AdminLogEntry.objects.count() == count + 1
def test_reindex_node_elastic(self, mock_update_search): count = AdminLogEntry.objects.count() view = NodeReindexElastic() view = setup_log_view(view, self.request, guid=self.node._id) view.delete(self.request) nt.assert_true(mock_update_search.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_reindex_node_elastic(self): count = AdminLogEntry.objects.count() view = NodeReindexElastic() view = setup_log_view(view, self.request, guid=self.node._id) view.delete(self.request) nt.assert_true(self.mock_reindex_elastic.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_integration_remove_contributor(self): nt.assert_in(self.user_2, self.node.contributors) view = setup_log_view(self.view(), self.request, guid=self.node._id, user_id=self.user_2._id) count = AdminLogEntry.objects.count() view.delete(self.request) nt.assert_not_in(self.user_2, self.node.contributors) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def setUp(self): self.user = UserFactory() self.request = RequestFactory().post('/fake_path') self.view = views.HamUserRestoreView self.view = setup_log_view(self.view, self.request, guid=self.user._id) self.spam_confirmed, created = Tag.objects.get_or_create(name='spam_confirmed') self.ham_confirmed, created = Tag.objects.get_or_create(name='ham_confirmed')
def test_no_log(self): view = setup_log_view(self.view(), self.request, guid=self.node._id, user_id=self.user_2.id) view.post(self.request) nt.assert_not_equal(self.node.logs.latest().action, NodeLog.CONTRIB_REMOVED)
def test_reindex_registration_elastic(self, mock_update_node): count = AdminLogEntry.objects.count() view = NodeReindexElastic() view = setup_log_view(view, self.request, guid=self.registration._id) view.post(self.request) nt.assert_true(mock_update_node.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_reindex_registration_elastic(self, mock_update_node): count = AdminLogEntry.objects.count() view = NodeReindexElastic() view = setup_log_view(view, self.request, guid=self.registration._id) view.delete(self.request) nt.assert_true(mock_update_node.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_get_object(self): view = setup_log_view(self.view(), self.request, guid=self.node._id, user_id=self.user._id) node, user = view.get_object() nt.assert_is_instance(node, Node) nt.assert_is_instance(user, OSFUser)
def test_reindex_user_elastic(self, mock_reindex_elastic): count = AdminLogEntry.objects.count() view = views.UserReindexElastic() view = setup_log_view(view, self.request, guid=self.user._id) view.delete(self.request) nt.assert_true(mock_reindex_elastic.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_integration_remove_contributor(self): nt.assert_in(self.user_2, self.node.contributors) view = setup_log_view(self.view(), self.request, node_id=self.node._id, user_id=self.user_2._id) count = AdminLogEntry.objects.count() view.delete(self.request) nt.assert_not_in(self.user_2, self.node.contributors) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def setUp(self): super(TestRemove2Factor, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().post('/fake_path') self.view = views.User2FactorDeleteView self.setup_view = setup_log_view(self.view(), self.request, guid=self.user._id) self.url = reverse('users:remove2factor', kwargs={'guid': self.user._id})
def setUp(self): super(TestNodeDeleteView, self).setUp() self.node = ProjectFactory() self.request = RequestFactory().post('/fake_path') self.plain_view = NodeDeleteView self.view = setup_log_view(self.plain_view(), self.request, guid=self.node._id) self.url = reverse('nodes:remove', kwargs={'guid': self.node._id})
def test_reindex_registration_share(self): count = AdminLogEntry.objects.count() view = NodeReindexShare() view = setup_log_view(view, self.request, guid=self.registration._id) view.delete(self.request) nt.assert_false(self.mock_reindex_node.called) nt.assert_true(self.mock_reindex_registration.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_remove_contributor(self): user_id = self.user_2.id node_id = self.node._id view = setup_log_view(self.view(), self.request, guid=node_id, user_id=user_id) view.post(self.request) assert not self.node.contributors.filter(id=user_id)
def test_confirm_preprint_as_ham(self, mock_akismet): request = RequestFactory().post('/fake_path') user = AuthUserFactory() preprint = PreprintFactory(creator=user) view = views.PreprintConfirmHamView() view = setup_log_view(view, request, guid=preprint._id) view.post(request) preprint.refresh_from_db() assert preprint.spam_status == 4
def test_reindex_registration_share(self, mock_update_share, mock_format_registration, mock_format_node): count = AdminLogEntry.objects.count() view = NodeReindexShare() view = setup_log_view(view, self.request, guid=self.registration._id) view.delete(self.request) nt.assert_true(mock_update_share.called) nt.assert_false(mock_format_node.called) nt.assert_true(mock_format_registration.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_confirm_registration_as_ham(self): view = NodeConfirmHamView() view = setup_log_view(view, self.request, guid=self.registration._id) resp = view.post(self.request) nt.assert_true(resp.status_code == 302) self.registration.refresh_from_db() nt.assert_false(self.registration.is_public) nt.assert_true(self.registration.spam_status == 4)
def test_post_id(self): request = RequestFactory().post('/fake_path', data={ 'id': self.group._id, 'name': '' }) view = setup_log_view(self.view, request) redirect = view.post(request) assert redirect.url == '/osf_groups/{}/'.format(self.group._id)
def test_post_name(self): request = RequestFactory().post('/fake_path', data={ 'id': '', 'name': 'Brian' }) view = setup_log_view(self.view, request) redirect = view.post(request) assert redirect.url == '/osf_groups/?name=Brian'
def setUp(self): super(TestPreprintDeleteView, self).setUp() self.user = AuthUserFactory() self.preprint = PreprintFactory(creator=self.user) self.request = RequestFactory().post('/fake_path') self.plain_view = views.PreprintDeleteView self.view = setup_log_view(self.plain_view(), self.request, guid=self.preprint._id) self.url = reverse('preprints:remove', kwargs={'guid': self.preprint._id})
def test_get_default_queryset(self): view = setup_log_view(self.view, self.request) queryset = view.get_queryset() nt.assert_equal(len(queryset), 3) nt.assert_in(self.group, queryset) nt.assert_in(self.group2, queryset) nt.assert_in(self.group3, queryset)
def test_get_queryset_by_name(self): request = RequestFactory().post('/fake_path/?name=Brian') view = setup_log_view(self.view, request) queryset = view.get_queryset() nt.assert_equal(len(queryset), 2) nt.assert_in(self.group, queryset) nt.assert_in(self.group2, queryset)
def test_do_not_remove_last_admin(self): assert len(list(self.preprint.get_admin_contributors(self.preprint.contributors))) == 1 view = setup_log_view(self.view(), self.request, guid=self.preprint._id, user_id=self.user._id) count = AdminLogEntry.objects.count() view.delete(self.request) self.preprint.reload() # Reloads instance to show that nothing was removed assert len(list(self.preprint.contributors)) == 2 assert len(list(self.preprint.get_admin_contributors(self.preprint.contributors))) == 1 assert AdminLogEntry.objects.count() == count
def test_integration_remove_contributor(self): patch_messages(self.request) nt.assert_in(self.user_2, self.node.contributors) view = setup_log_view(self.view(), self.request, guid=self.node._id, user_id=self.user_2.id) count = AdminLogEntry.objects.count() view.post(self.request) nt.assert_not_in(self.user_2, self.node.contributors) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_reindex_preprint_share(self, mock_reindex_preprint, preprint, req): preprint.provider.access_token = 'totally real access token I bought from a guy wearing a trenchcoat in the summer' preprint.provider.save() count = AdminLogEntry.objects.count() view = views.PreprintReindexShare() view = setup_log_view(view, req, guid=preprint._id) view.delete(req) assert mock_reindex_preprint.called assert AdminLogEntry.objects.count() == count + 1
def test_reindex_preprint_share(self, mock_reindex_preprint): self.preprint.provider.access_token = 'totally real access token I bought from a guy wearing a trenchcoat in the summer' self.preprint.provider.save() count = AdminLogEntry.objects.count() view = views.PreprintReindexShare() view = setup_log_view(view, self.request, guid=self.preprint._id) view.delete(self.request) nt.assert_true(mock_reindex_preprint.called) nt.assert_equal(AdminLogEntry.objects.count(), count + 1)
def test_remove_stuck_registration_with_an_addon(self): # Prevents circular import that prevents admin app from starting up from django.contrib.messages.storage.fallback import FallbackStorage self.registration.add_addon('github', auth=Auth(self.user)) view = RemoveStuckRegistrationsView() view = setup_log_view(view, self.request, guid=self.registration._id) setattr(self.request, 'session', 'session') messages = FallbackStorage(self.request) setattr(self.request, '_messages', messages) view.post(self.request) self.registration.refresh_from_db() nt.assert_true(self.registration.is_deleted)
def test_get_queryset(self): self.node.set_title('New Title', auth=self.auth, save=True) view = AdminNodeLogView() view = setup_log_view(view, self.request, guid=self.node._id) logs = view.get_queryset() log_entry = logs.first() nt.assert_true(log_entry.action == 'edit_title') nt.assert_true(log_entry.params['title_new'] == u'New Title')
def test_get_context_data(self): self.node.set_title('New Title', auth=self.auth, save=True) view = AdminNodeLogView() view = setup_log_view(view, self.request, guid=self.node._id) logs = view.get_context_data()['logs'] log_entry = logs[0][0] log_params = logs[0][1] nt.assert_true(log_entry.action == NodeLog.EDITED_TITLE) nt.assert_true((u'title_new', u'New Title') in log_params) nt.assert_true((u'node', self.node._id) in log_params)
def test_restart_stuck_registration(self): view = RestartStuckRegistrationsView() view = setup_log_view(view, self.request, guid=self.registration._id) nt.assert_equal(self.registration.archive_job.status, u'INITIATED') from django.contrib.messages.storage.fallback import FallbackStorage # django.contrib.messages has a bug which effects unittests # more info here -> https://code.djangoproject.com/ticket/17971 setattr(self.request, 'session', 'session') messages = FallbackStorage(self.request) setattr(self.request, '_messages', messages) view.post(self.request) nt.assert_equal(self.registration.archive_job.status, u'SUCCESS')
def test_merge_user(self, mock_merge_user): user = UserFactory() user_merged = UserFactory() view = views.UserMergeAccounts() view = setup_log_view(view, self.request, guid=user._id) invalid_form = MergeUserForm(data={'user_guid_to_be_merged': 'Not a valid Guid'}) valid_form = MergeUserForm(data={'user_guid_to_be_merged': user_merged._id}) nt.assert_false(invalid_form.is_valid()) nt.assert_true(valid_form.is_valid()) view.form_valid(valid_form) nt.assert_true(mock_merge_user.called_with())
def test_remove_stuck_registration(self): # Prevents circular import that prevents admin app from starting up from django.contrib.messages.storage.fallback import FallbackStorage view = RemoveStuckRegistrationsView() view = setup_log_view(view, self.request, guid=self.registration._id) # django.contrib.messages has a bug which effects unittests # more info here -> https://code.djangoproject.com/ticket/17971 setattr(self.request, 'session', 'session') messages = FallbackStorage(self.request) setattr(self.request, '_messages', messages) view.post(self.request) self.registration.refresh_from_db() nt.assert_true(self.registration.is_deleted)
def test_do_not_remove_last_admin(self): nt.assert_equal( len(list(self.node.get_admin_contributors(self.node.contributors))), 1 ) view = setup_log_view(self.view(), self.request, guid=self.node._id, user_id=self.user._id) count = AdminLogEntry.objects.count() view.delete(self.request) self.node.reload() # Reloads instance to show that nothing was removed nt.assert_equal(len(list(self.node.contributors)), 2) nt.assert_equal( len(list(self.node.get_admin_contributors(self.node.contributors))), 1 ) nt.assert_equal(AdminLogEntry.objects.count(), count)
def test_get_logs_for_children(self): """ The "create component" action is actually logged as a create_project action for its child with a log parameter 'node' having its guid as a value. We have to ensure that all the components a parent has created appear in its admin app logs, so we can't just do node.logs.all(), that will leave out component creation. """ component = ProjectFactory(creator=self.user, parent=self.node) component.save() view = AdminNodeLogView() view = setup_log_view(view, self.request, guid=self.node._id) logs = view.get_context_data()['logs'] log_entry = logs[0][0] log_params = logs[0][1] nt.assert_true(log_entry.action == NodeLog.PROJECT_CREATED) nt.assert_true(log_entry.node._id == component._id) nt.assert_true(('node', component._id) in log_params)
def test_get_object(self): view = RemoveStuckRegistrationsView() view = setup_log_view(view, self.request, guid=self.registration._id) nt.assert_true(self.registration, view.get_object())
def test_get_object(self): view = AdminNodeLogView() view = setup_log_view(view, self.request, guid=self.node._id) nt.assert_true(self.node, view.get_object())
def test_no_log(self): view = setup_log_view(self.view(), self.request, guid=self.node._id, user_id=self.user_2._id) view.delete(self.request) nt.assert_not_equal(self.node.logs.latest().action, NodeLog.CONTRIB_REMOVED)