Ejemplo n.º 1
0
class LogsTestCase(ApiTestCase):

    def setUp(self):
        super(LogsTestCase, self).setUp()

        self.user = AuthUserFactory()

        self.action_set = NodeLog.actions
        self.node = ProjectFactory(is_public=False)
        for i in range(len(self.action_set)):
            self.node.add_log(
                self.action_set[i],
                {},
                Auth(self.node.creator),
                save=True
            )
        self.node.add_contributor(self.user, permissions=[osf_permissions.READ], auth=Auth(self.node.creator), log=False, save=True)
        self.node_log_url = '/{}nodes/{}/logs/'.format(API_BASE, self.node._id)
        self.url = '/{}logs/'.format(API_BASE)

        self.public_node = ProjectFactory(is_public=True)
        for i in range(len(self.action_set)):
            self.public_node.add_log(
                self.action_set[i],
                {},
                Auth(self.public_node.creator),
                save=True
            )

    def tearDown(self):
        NodeLog.remove()
        Node.remove()
Ejemplo n.º 2
0
class LogsTestCase(ApiTestCase):
    def setUp(self):
        super(LogsTestCase, self).setUp()

        self.user = AuthUserFactory()

        self.action_set = NodeLog.actions
        self.node = ProjectFactory(is_public=False)
        for i in range(len(self.action_set)):
            self.node.add_log(self.action_set[i], {},
                              Auth(self.node.creator),
                              save=True)
        self.node.add_contributor(self.user,
                                  permissions=[osf_permissions.READ],
                                  auth=Auth(self.node.creator),
                                  log=False,
                                  save=True)
        self.node_log_url = '/{}nodes/{}/logs/'.format(API_BASE, self.node._id)
        self.url = '/{}logs/'.format(API_BASE)

        self.public_node = ProjectFactory(is_public=True)
        for i in range(len(self.action_set)):
            self.public_node.add_log(self.action_set[i], {},
                                     Auth(self.public_node.creator),
                                     save=True)

    def tearDown(self):
        NodeLog.remove()
        Node.remove()
Ejemplo n.º 3
0
class TestNodeFileLogDetail(ApiTestCase):

    def setUp(self):
        super(TestNodeFileLogDetail, self).setUp()

        self.user_one = AuthUserFactory()
        self.user_two = AuthUserFactory()

        self.node = ProjectFactory(creator=self.user_one)
        self.node.add_contributor(self.user_two)

        self.component = NodeFactory(parent=self.node, creator=self.user_one)

        self.file = api_utils.create_test_file(node=self.component, user=self.user_one)

        self.node.add_log(
            'osf_storage_file_moved',
            auth=Auth(self.user_one),
            params={
                'node': self.node._id,
                'project': self.node.parent_id,
                'path': self.file.materialized_path,
                'source': {
                    'materialized': self.file.materialized_path,
                    'addon': 'osfstorage',
                    'node': {
                        '_id': self.component._id,
                        'url': self.component.url,
                        'title': self.component.title,
                    }
                },
                'destination': {
                    'materialized': self.file.materialized_path,
                    'addon': 'osfstorage',
                    'node': {
                        '_id': self.node._id,
                        'url': self.node.url,
                        'title': self.node.title,
                    }
                }
            },
        )

        self.node.save()

        self.node_logs_url = '/{}nodes/{}/logs/'.format(API_BASE, self.node._id)
        self.component_logs_url = '/{}nodes/{}/logs/'.format(API_BASE, self.component._id)

    def test_title_not_hidden_from_contributor_in_file_move(self):
        res = self.app.get(self.node_logs_url, auth=self.user_two.auth)
        assert_equal(res.status_code, 200)
        assert_equal(res.json['data'][0]['attributes']['params']['destination']['node_title'], self.node.title)

    def test_title_hidden_from_non_contributor_in_file_move(self):
        res = self.app.get(self.node_logs_url, auth=self.user_two.auth)
        assert_equal(res.status_code, 200)
        assert_not_in(self.component.title, res.json['data'])
        assert_equal(res.json['data'][0]['attributes']['params']['source']['node_title'], 'Private Component')
Ejemplo n.º 4
0
    def _create_nodes_and_add_logs(self, first_activity_date, second_activity_date=None):
        node_one = ProjectFactory(creator=self.user_1, date_created=first_activity_date)
        node_one.add_log(
            'log_added', params={'project': node_one._id}, auth=self.auth_1, log_date=first_activity_date, save=True
        )

        if second_activity_date:
            node_two = ProjectFactory(creator=self.user_1, date_created=second_activity_date)
            node_two.add_log(
                'log_added', params={'project': node_two._id}, auth=self.auth_1, log_date=second_activity_date, save=True
            )
Ejemplo n.º 5
0
class TestUtilsTests(OsfTestCase):
    def setUp(self):
        super(TestUtilsTests, self).setUp()

        self.node = ProjectFactory()
        self.user = self.node.creator
        self.auth = Auth(self.user)

    def test_assert_logs(self):
        def add_log(self):
            self.node.add_log(NodeLog.UPDATED_FIELDS, {}, auth=self.auth)

        wrapped = test_utils.assert_logs(NodeLog.UPDATED_FIELDS,
                                         'node')(add_log)
        wrapped(self)

    def test_assert_logs_fail(self):
        def dont_add_log(self):
            pass

        wrapped = test_utils.assert_logs(NodeLog.UPDATED_FIELDS,
                                         'node')(dont_add_log)
        assert_raises(AssertionError, lambda: wrapped(self))

    def test_assert_logs_stacked(self):
        def add_log(self):
            self.node.add_log(NodeLog.UPDATED_FIELDS, {}, auth=self.auth)

        def add_two_logs(self):
            add_log(self)
            self.node.add_log(NodeLog.CONTRIB_ADDED, {}, auth=self.auth)

        wrapped = test_utils.assert_logs(NodeLog.UPDATED_FIELDS, 'node',
                                         -2)(test_utils.assert_logs(
                                             NodeLog.CONTRIB_ADDED,
                                             'node')(add_two_logs))
        wrapped(self)

    def test_assert_not_logs_pass(self):
        def dont_add_log(self):
            pass

        wrapped = test_utils.assert_not_logs(NodeLog.UPDATED_FIELDS,
                                             'node')(dont_add_log)
        wrapped(self)

    def test_assert_not_logs_fail(self):
        def add_log(self):
            self.node.add_log(NodeLog.UPDATED_FIELDS, {}, auth=self.auth)

        wrapped = test_utils.assert_not_logs(NodeLog.UPDATED_FIELDS,
                                             'node')(add_log)
        assert_raises(AssertionError, lambda: wrapped(self))
Ejemplo n.º 6
0
    def _create_nodes_and_add_logs(self,
                                   first_activity_date,
                                   second_activity_date=None):
        node_one = ProjectFactory(creator=self.user_1,
                                  date_created=first_activity_date)
        node_one.add_log('log_added',
                         params={'project': node_one._id},
                         auth=self.auth_1,
                         log_date=first_activity_date,
                         save=True)

        if second_activity_date:
            node_two = ProjectFactory(creator=self.user_1,
                                      date_created=second_activity_date)
            node_two.add_log('log_added',
                             params={'project': node_two._id},
                             auth=self.auth_1,
                             log_date=second_activity_date,
                             save=True)
Ejemplo n.º 7
0
class TestUtilsTests(OsfTestCase):

    def setUp(self):
        super(TestUtilsTests, self).setUp()

        self.node = ProjectFactory()
        self.user = self.node.creator
        self.auth = Auth(self.user)

    def test_assert_logs(self):

        def add_log(self):
            self.node.add_log(NodeLog.UPDATED_FIELDS, {}, auth=self.auth)
        wrapped = test_utils.assert_logs(NodeLog.UPDATED_FIELDS, 'node')(add_log)
        wrapped(self)

    def test_assert_logs_fail(self):

        def dont_add_log(self):
            pass
        wrapped = test_utils.assert_logs(NodeLog.UPDATED_FIELDS, 'node')(dont_add_log)
        assert_raises(AssertionError, lambda: wrapped(self))

    def test_assert_logs_stacked(self):

        def add_log(self):
            self.node.add_log(NodeLog.UPDATED_FIELDS, {}, auth=self.auth)

        def add_two_logs(self):
            add_log(self)
            self.node.add_log(NodeLog.CONTRIB_ADDED, {}, auth=self.auth)

        wrapped = test_utils.assert_logs(NodeLog.UPDATED_FIELDS, 'node', -2)(
            test_utils.assert_logs(NodeLog.CONTRIB_ADDED, 'node')(add_two_logs)
        )
        wrapped(self)

    def test_assert_not_logs_pass(self):

        def dont_add_log(self):
            pass
        wrapped = test_utils.assert_not_logs(NodeLog.UPDATED_FIELDS, 'node')(dont_add_log)
        wrapped(self)

    def test_assert_not_logs_fail(self):

        def add_log(self):
            self.node.add_log(NodeLog.UPDATED_FIELDS, {}, auth=self.auth)
        wrapped = test_utils.assert_not_logs(NodeLog.UPDATED_FIELDS, 'node')(add_log)
        assert_raises(AssertionError, lambda: wrapped(self))
Ejemplo n.º 8
0
class TestWatching(OsfTestCase):
    def setUp(self):
        super(TestWatching, self).setUp()
        self.user = UserFactory()
        self.project = ProjectFactory(creator=self.user)
        # add some log objects
        self.consolidate_auth = Auth(user=self.user)
        self.project.save()
        # A log added 100 days ago
        self.project.add_log(
            'tag_added',
            params={'project': self.project._primary_key},
            auth=self.consolidate_auth,
            log_date=dt.datetime.utcnow() - dt.timedelta(days=100),
            save=True,
        )
        # Set the ObjectId to correspond with the log date
        # A log added now
        self.last_log = self.project.add_log(
            'tag_added',
            params={'project': self.project._primary_key},
            auth=self.consolidate_auth,
            log_date=dt.datetime.utcnow(),
            save=True,
        )
        # Clear watched list
        self.user.watched = []
        self.user.save()

    def test_watch_adds_to_watched_list(self):
        n_watched_then = len(self.user.watched)
        # A user watches a WatchConfig
        config = WatchConfigFactory(node=self.project)
        self.user.watch(config)
        n_watched_now = len(self.user.watched)
        assert_equal(n_watched_now, n_watched_then + 1)
        assert_true(self.user.is_watching(self.project))

    def test_unwatch_removes_from_watched_list(self):
        # The user has already watched a project
        self._watch_project(self.project)
        config = WatchConfigFactory(node=self.project)
        n_watched_then = len(self.user.watched)
        self.user.unwatch(config)
        n_watched_now = len(self.user.watched)
        assert_equal(n_watched_now, n_watched_then - 1)
        assert_false(self.user.is_watching(self.project))

    @unittest.skip("Won't work because the old log's id doesn't encode the "
                   "correct log date")
    def test_get_recent_log_ids(self):
        self._watch_project(self.project)
        log_ids = list(self.user.get_recent_log_ids())
        assert_equal(self.last_log._id, log_ids[0])
        # This part won't work
        # TODO(sloria): Rethink.
        assert_equal(len(log_ids), 1)

    def test_get_recent_log_ids_since(self):
        self._watch_project(self.project)
        since = dt.datetime.utcnow().replace(tzinfo=utc) - dt.timedelta(
            days=101)
        log_ids = list(self.user.get_recent_log_ids(since=since))
        assert_equal(len(log_ids), 3)

    def test_get_daily_digest_log_ids(self):
        self._watch_project(self.project)
        day_log_ids = list(self.user.get_daily_digest_log_ids())
        assert_in(self.last_log._id, day_log_ids)

    def _watch_project(self, project):
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()

    def _unwatch_project(self, project):
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()

    def test_paginate_helper(self):
        self._watch_project(self.project)
        logs = list(self.user.get_recent_log_ids())
        size = 10
        page = 0
        total = len(logs)
        paginated_logs, pages = paginate(self.user.get_recent_log_ids(), total,
                                         page, size)
        page_num = math.ceil(total / float(size))
        assert_equal(len(list(paginated_logs)), total)
        assert_equal(page_num, pages)

    def test_paginate_no_negative_page_num(self):
        self._watch_project(self.project)
        logs = list(self.user.get_recent_log_ids())
        size = 10
        page = -1
        total = len(logs)
        with assert_raises(HTTPError):
            paginate(self.user.get_recent_log_ids(), total, page, size)

    def test_paginate_not_go_beyond_limit(self):
        self._watch_project(self.project)
        logs = list(self.user.get_recent_log_ids())
        size = 10
        total = len(logs)
        pages_num = math.ceil(total / float(size))
        page = pages_num
        with assert_raises(HTTPError):
            paginate(self.user.get_recent_log_ids(), total, page, size)
Ejemplo n.º 9
0
class TestWatching(OsfTestCase):

    def setUp(self):
        super(TestWatching, self).setUp()
        self.user = UserFactory()
        self.project = ProjectFactory(creator=self.user)
        # add some log objects
        api_key = ApiKeyFactory()
        self.user.api_keys.append(api_key)
        self.user.save()
        self.consolidate_auth = Auth(user=self.user, api_key=api_key)
        # Clear project logs
        self.project.logs = []
        self.project.save()
        # A log added 100 days ago
        self.project.add_log(
            'project_created',
            params={'project': self.project._primary_key},
            auth=self.consolidate_auth,
            log_date=dt.datetime.utcnow() - dt.timedelta(days=100),
            save=True,
        )
        # Set the ObjectId to correspond with the log date
        # A log added now
        self.last_log = self.project.add_log(
            'tag_added',
            params={'project': self.project._primary_key},
            auth=self.consolidate_auth, log_date=dt.datetime.utcnow(),
            save=True,
        )
        # Clear watched list
        self.user.watched = []
        self.user.save()

    def test_watch_adds_to_watched_list(self):
        n_watched_then = len(self.user.watched)
        # A user watches a WatchConfig
        config = WatchConfigFactory(node=self.project)
        self.user.watch(config)
        n_watched_now = len(self.user.watched)
        assert_equal(n_watched_now, n_watched_then + 1)
        assert_true(self.user.is_watching(self.project))

    def test_unwatch_removes_from_watched_list(self):
        # The user has already watched a project
        self._watch_project(self.project)
        config = WatchConfigFactory(node=self.project)
        n_watched_then = len(self.user.watched)
        self.user.unwatch(config)
        n_watched_now = len(self.user.watched)
        assert_equal(n_watched_now, n_watched_then - 1)
        assert_false(self.user.is_watching(self.project))

    @unittest.skip("Won't work because the old log's id doesn't encode the "
                   "correct log date")
    def test_get_recent_log_ids(self):
        self._watch_project(self.project)
        log_ids = list(self.user.get_recent_log_ids())
        assert_equal(self.last_log._id, log_ids[0])
        # This part won't work
        # TODO(sloria): Rethink.
        assert_equal(len(log_ids), 1)

    def test_get_recent_log_ids_since(self):
        self._watch_project(self.project)
        since = dt.datetime.utcnow().replace(tzinfo=utc) - dt.timedelta(days=101)
        log_ids = list(self.user.get_recent_log_ids(since=since))
        assert_equal(len(log_ids), 2)

    def test_get_daily_digest_log_ids(self):
        self._watch_project(self.project)
        day_log_ids = list(self.user.get_daily_digest_log_ids())
        assert_in(self.last_log._id, day_log_ids)

    def _watch_project(self, project):
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()

    def _unwatch_project(self, project):
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()
Ejemplo n.º 10
0
class TestWatching(OsfTestCase):

    def setUp(self):
        super(TestWatching, self).setUp()
        self.user = UserFactory()
        self.project = ProjectFactory(creator=self.user)
        # add some log objects
        self.consolidate_auth = Auth(user=self.user)
        self.project.save()
        # A log added 100 days ago
        self.project.add_log(
            'tag_added',
            params={'project': self.project._primary_key},
            auth=self.consolidate_auth,
            log_date=dt.datetime.utcnow() - dt.timedelta(days=100),
            save=True,
        )
        # Set the ObjectId to correspond with the log date
        # A log added now
        self.last_log = self.project.add_log(
            'tag_added',
            params={'project': self.project._primary_key},
            auth=self.consolidate_auth, log_date=dt.datetime.utcnow(),
            save=True,
        )
        # Clear watched list
        self.user.watched = []
        self.user.save()

    def test_watch_adds_to_watched_list(self):
        n_watched_then = len(self.user.watched)
        # A user watches a WatchConfig
        config = WatchConfigFactory(node=self.project)
        self.user.watch(config)
        n_watched_now = len(self.user.watched)
        assert_equal(n_watched_now, n_watched_then + 1)
        assert_true(self.user.is_watching(self.project))

    def test_unwatch_removes_from_watched_list(self):
        # The user has already watched a project
        self._watch_project(self.project)
        config = WatchConfigFactory(node=self.project)
        n_watched_then = len(self.user.watched)
        self.user.unwatch(config)
        n_watched_now = len(self.user.watched)
        assert_equal(n_watched_now, n_watched_then - 1)
        assert_false(self.user.is_watching(self.project))

    @unittest.skip("Won't work because the old log's id doesn't encode the "
                   "correct log date")
    def test_get_recent_log_ids(self):
        self._watch_project(self.project)
        log_ids = list(self.user.get_recent_log_ids())
        assert_equal(self.last_log._id, log_ids[0])
        # This part won't work
        # TODO(sloria): Rethink.
        assert_equal(len(log_ids), 1)

    def test_get_recent_log_ids_since(self):
        self._watch_project(self.project)
        since = dt.datetime.utcnow().replace(tzinfo=utc) - dt.timedelta(days=101)
        log_ids = list(self.user.get_recent_log_ids(since=since))
        assert_equal(len(log_ids), 3)

    def test_get_daily_digest_log_ids(self):
        self._watch_project(self.project)
        day_log_ids = list(self.user.get_daily_digest_log_ids())
        assert_in(self.last_log._id, day_log_ids)

    def _watch_project(self, project):
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()

    def _unwatch_project(self, project):
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()

    def test_paginate_helper(self):
        self._watch_project(self.project)
        logs = list(self.user.get_recent_log_ids())
        size = 10
        page = 0
        total = len(logs)
        paginated_logs, pages = paginate(
            self.user.get_recent_log_ids(), total, page, size)
        page_num = math.ceil(total / float(size))
        assert_equal(len(list(paginated_logs)), total)
        assert_equal(page_num, pages)

    def test_paginate_no_negative_page_num(self):
        self._watch_project(self.project)
        logs = list(self.user.get_recent_log_ids())
        size = 10
        page = -1
        total = len(logs)
        with assert_raises(HTTPError):
            paginate(self.user.get_recent_log_ids(), total, page, size)

    def test_paginate_not_go_beyond_limit(self):
        self._watch_project(self.project)
        logs = list(self.user.get_recent_log_ids())
        size = 10
        total = len(logs)
        pages_num = math.ceil(total / float(size))
        page = pages_num
        with assert_raises(HTTPError):
            paginate(self.user.get_recent_log_ids(), total, page, size)