예제 #1
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)
예제 #2
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()
예제 #3
0
class TestAUser(OsfTestCase):
    def setUp(self):
        super(TestAUser, self).setUp()
        self.user = UserFactory()
        self.user.set_password('science')
        # Add an API key for quicker authentication
        api_key = ApiKeyFactory()
        self.user.api_keys.append(api_key)
        self.user.save()
        self.auth = ('test', api_key._primary_key)

    def _login(self, username, password):
        '''Log in a user via at the login page.'''
        res = self.app.get('/account/').maybe_follow()
        # Fills out login info
        form = res.forms['signinForm']  # Get the form from its ID
        form['username'] = username
        form['password'] = password
        # submits
        res = form.submit().maybe_follow()
        return res

    def test_can_see_profile_url(self):
        res = self.app.get(self.user.url).maybe_follow()
        assert_in(self.user.url, res)

    def test_can_see_homepage(self):
        # Goes to homepage
        res = self.app.get('/').maybe_follow()  # Redirects
        assert_equal(res.status_code, 200)

    def test_can_log_in(self):
        # Log in and out
        self._login(self.user.username, 'science')
        self.app.get('/logout/')
        # Goes to home page
        res = self.app.get('/').maybe_follow()
        # Clicks sign in button
        res = res.click('Create an Account or Sign-In').maybe_follow()
        # Fills out login info
        form = res.forms['signinForm']  # Get the form from its ID
        form['username'] = self.user.username
        form['password'] = '******'
        # submits
        res = form.submit().maybe_follow()
        # Sees dashboard with projects and watched projects
        assert_in('Projects', res)
        assert_in('Watchlist', res)

    def test_sees_flash_message_on_bad_login(self):
        # Goes to log in page
        res = self.app.get('/account/').maybe_follow()
        # Fills the form with incorrect password
        form = res.forms['signinForm']
        form['username'] = self.user.username
        form['password'] = '******'
        # Submits
        res = form.submit()
        # Sees a flash message
        assert_in('Log-in failed', res)

    def test_is_redirected_to_dashboard_already_logged_in_at_login_page(self):
        res = self._login(self.user.username, 'science')
        res = self.app.get('/login/').follow()
        assert_equal(res.request.path, '/dashboard/')

    def test_sees_projects_in_her_dashboard(self):
        # the user already has a project
        project = ProjectFactory(creator=self.user)
        project.add_contributor(self.user)
        project.save()
        # Goes to homepage, already logged in
        res = self._login(self.user.username, 'science')
        res = self.app.get('/').maybe_follow()
        # Clicks Dashboard link in navbar
        res = res.click('Dashboard', index=0)
        assert_in('Projects', res)  # Projects heading
        # The project title is listed
        # TODO: (bgeiger) figure out how to make this assertion work with hgrid view
        #assert_in(project.title, res)

    def test_does_not_see_osffiles_in_user_addon_settings(self):
        res = self._login(self.user.username, 'science')
        res = self.app.get('/settings/addons/',
                           auth=self.auth,
                           auto_follow=True)
        assert_not_in('OSF Storage', res)

    def test_sees_osffiles_in_project_addon_settings(self):
        project = ProjectFactory(creator=self.user)
        project.add_contributor(self.user,
                                permissions=['read', 'write', 'admin'],
                                save=True)
        res = self.app.get('/{0}/settings/'.format(project._primary_key),
                           auth=self.auth,
                           auto_follow=True)
        assert_in('OSF Storage', res)

    @unittest.skip("Can't test this, since logs are dynamically loaded")
    def test_sees_log_events_on_watched_projects(self):
        # Another user has a public project
        u2 = UserFactory(username='******', fullname='Bono')
        key = ApiKeyFactory()
        u2.api_keys.append(key)
        u2.save()
        project = ProjectFactory(creator=u2, is_public=True)
        project.add_contributor(u2)
        auth = Auth(user=u2, api_key=key)
        # A file was added to the project
        project.add_file(auth=auth,
                         file_name='test.html',
                         content='123',
                         size=2,
                         content_type='text/html')
        project.save()
        # User watches the project
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()
        # Goes to her dashboard, already logged in
        res = self.app.get('/dashboard/', auth=self.auth, auto_follow=True)
        # Sees logs for the watched project
        assert_in('Watched Projects', res)  # Watched Projects header
        # The log action is in the feed
        assert_in('added file test.html', res)
        assert_in(project.title, res)

    def test_sees_correct_title_home_page(self):
        # User goes to homepage
        res = self.app.get('/', auto_follow=True)
        title = res.html.title.string
        # page title is correct
        assert_equal('OSF | Home', title)

    def test_sees_correct_title_on_dashboard(self):
        # User goes to dashboard
        res = self.app.get('/dashboard/', auth=self.auth, auto_follow=True)
        title = res.html.title.string
        assert_equal('OSF | Dashboard', title)

    def test_can_see_make_public_button_if_admin(self):
        # User is a contributor on a project
        project = ProjectFactory()
        project.add_contributor(self.user,
                                permissions=['read', 'write', 'admin'],
                                save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_in('Make Public', res)

    def test_cant_see_make_public_button_if_not_admin(self):
        # User is a contributor on a project
        project = ProjectFactory()
        project.add_contributor(self.user,
                                permissions=['read', 'write'],
                                save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_not_in('Make Public', res)

    def test_can_see_make_private_button_if_admin(self):
        # User is a contributor on a project
        project = ProjectFactory(is_public=True)
        project.add_contributor(self.user,
                                permissions=['read', 'write', 'admin'],
                                save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_in('Make Private', res)

    def test_cant_see_make_private_button_if_not_admin(self):
        # User is a contributor on a project
        project = ProjectFactory(is_public=True)
        project.add_contributor(self.user,
                                permissions=['read', 'write'],
                                save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_not_in('Make Private', res)

    def test_sees_logs_on_a_project(self):
        project = ProjectFactory(is_public=True)
        # User goes to the project's page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        # Can see log event
        assert_in('created', res)

    def test_no_wiki_content_message(self):
        project = ProjectFactory(creator=self.user)
        # Goes to project's wiki, where there is no content
        res = self.app.get('/{0}/wiki/home/'.format(project._primary_key),
                           auth=self.auth)
        # Sees a message indicating no content
        assert_in('No wiki content', res)

    def test_wiki_page_name_non_ascii(self):
        project = ProjectFactory(creator=self.user)
        non_ascii = to_mongo_key('WöRlÐé')
        self.app.get('/{0}/wiki/{1}/'.format(project._primary_key, non_ascii),
                     auth=self.auth,
                     expect_errors=True)
        project.update_node_wiki(non_ascii, 'new content', Auth(self.user))
        assert_in(non_ascii, project.wiki_pages_current)

    def test_noncontributor_cannot_see_wiki_if_no_content(self):
        user2 = UserFactory()
        # user2 creates a public project and adds no wiki content
        project = ProjectFactory(creator=user2, is_public=True)
        # self navigates to project
        res = self.app.get(project.url).maybe_follow()
        # Should not see wiki widget (since non-contributor and no content)
        assert_not_in('No wiki content', res)

    def test_wiki_does_not_exist(self):
        project = ProjectFactory(creator=self.user)
        res = self.app.get('/{0}/wiki/{1}/'.format(
            project._primary_key,
            'not a real page yet',
        ),
                           auth=self.auth,
                           expect_errors=True)
        assert_in('This wiki page does not currently exist.', res)
        assert_equal(res.status_code, 404)

    def test_sees_own_profile(self):
        res = self.app.get('/profile/', auth=self.auth)
        td1 = res.html.find('td', text=re.compile(r'Public(.*?)Profile'))
        td2 = td1.find_next_sibling('td')
        assert_equal(td2.text, self.user.display_absolute_url)

    def test_sees_another_profile(self):
        user2 = UserFactory()
        res = self.app.get(user2.url, auth=self.auth)
        td1 = res.html.find('td', text=re.compile(r'Public(.*?)Profile'))
        td2 = td1.find_next_sibling('td')
        assert_equal(td2.text, user2.display_absolute_url)

    # Regression test for https://github.com/CenterForOpenScience/osf.io/issues/1320
    @mock.patch('framework.auth.views.mails.send_mail')
    def test_can_reset_password(self, mock_send_mail):
        # A registered user
        user = UserFactory()
        # goes to the login page
        url = web_url_for('auth_login')
        res = self.app.get(url)
        # and fills out forgot password form
        form = res.forms['forgotPassword']
        form['forgot_password-email'] = user.username
        # submits
        res = form.submit()
        # mail was sent
        mock_send_mail.assert_called
        # gets 200 response
        assert_equal(res.status_code, 200)
        # URL is /forgotpassword
        assert_equal(res.request.path, web_url_for('forgot_password'))
예제 #4
0
class TestAUser(OsfTestCase):

    def setUp(self):
        super(TestAUser, self).setUp()
        self.user = UserFactory()
        self.user.set_password('science')
        # Add an API key for quicker authentication
        api_key = ApiKeyFactory()
        self.user.api_keys.append(api_key)
        self.user.save()
        self.auth = ('test', api_key._primary_key)

    def _login(self, username, password):
        '''Log in a user via at the login page.'''
        res = self.app.get('/account/').maybe_follow()
        # Fills out login info
        form = res.forms['signinForm']  # Get the form from its ID
        form['username'] = username
        form['password'] = password
        # submits
        res = form.submit().maybe_follow()
        return res

    def test_can_see_profile_url(self):
        res = self.app.get(self.user.url).maybe_follow()
        assert_in(self.user.url, res)

    def test_can_see_homepage(self):
        # Goes to homepage
        res = self.app.get('/').maybe_follow()  # Redirects
        assert_equal(res.status_code, 200)

    def test_can_log_in(self):
        # Log in and out
        self._login(self.user.username, 'science')
        self.app.get('/logout/')
        # Goes to home page
        res = self.app.get('/').maybe_follow()
        # Clicks sign in button
        res = res.click('Create an Account or Sign-In').maybe_follow()
        # Fills out login info
        form = res.forms['signinForm']  # Get the form from its ID
        form['username'] = self.user.username
        form['password'] = '******'
        # submits
        res = form.submit().maybe_follow()
        # Sees dashboard with projects and watched projects
        assert_in('Projects', res)
        assert_in('Watchlist', res)

    def test_sees_flash_message_on_bad_login(self):
        # Goes to log in page
        res = self.app.get('/account/').maybe_follow()
        # Fills the form with incorrect password
        form  = res.forms['signinForm']
        form['username'] = self.user.username
        form['password'] = '******'
        # Submits
        res = form.submit()
        # Sees a flash message
        assert_in('Log-in failed', res)

    def test_is_redirected_to_dashboard_already_logged_in_at_login_page(self):
        res = self._login(self.user.username, 'science')
        res = self.app.get('/login/').follow()
        assert_equal(res.request.path, '/dashboard/')

    def test_sees_projects_in_her_dashboard(self):
        # the user already has a project
        project = ProjectFactory(creator=self.user)
        project.add_contributor(self.user)
        project.save()
        # Goes to homepage, already logged in
        res = self._login(self.user.username, 'science')
        res = self.app.get('/').maybe_follow()
        # Clicks Dashboard link in navbar
        res = res.click('Dashboard', index=0)
        assert_in('Projects', res)  # Projects heading
        # The project title is listed
        # TODO: (bgeiger) figure out how to make this assertion work with hgrid view
        #assert_in(project.title, res)

    def test_does_not_see_osffiles_in_user_addon_settings(self):
        res = self._login(self.user.username, 'science')
        res = self.app.get('/settings/addons/', auth=self.auth, auto_follow=True)
        assert_not_in('OSF Storage', res)

    def test_sees_osffiles_in_project_addon_settings(self):
        project = ProjectFactory(creator=self.user)
        project.add_contributor(
            self.user,
            permissions=['read', 'write', 'admin'],
            save=True)
        res = self.app.get('/{0}/settings/'.format(project._primary_key), auth=self.auth, auto_follow=True)
        assert_in('OSF Storage', res)

    @unittest.skip("Can't test this, since logs are dynamically loaded")
    def test_sees_log_events_on_watched_projects(self):
        # Another user has a public project
        u2 = UserFactory(username='******', fullname='Bono')
        key = ApiKeyFactory()
        u2.api_keys.append(key)
        u2.save()
        project = ProjectFactory(creator=u2, is_public=True)
        project.add_contributor(u2)
        auth = Auth(user=u2, api_key=key)
        # A file was added to the project
        project.add_file(auth=auth, file_name='test.html',
                        content='123', size=2, content_type='text/html')
        project.save()
        # User watches the project
        watch_config = WatchConfigFactory(node=project)
        self.user.watch(watch_config)
        self.user.save()
        # Goes to her dashboard, already logged in
        res = self.app.get('/dashboard/', auth=self.auth, auto_follow=True)
        # Sees logs for the watched project
        assert_in('Watched Projects', res)  # Watched Projects header
        # The log action is in the feed
        assert_in('added file test.html', res)
        assert_in(project.title, res)

    def test_sees_correct_title_home_page(self):
        # User goes to homepage
        res = self.app.get('/', auto_follow=True)
        title = res.html.title.string
        # page title is correct
        assert_equal('OSF | Home', title)

    def test_sees_correct_title_on_dashboard(self):
        # User goes to dashboard
        res = self.app.get('/dashboard/', auth=self.auth, auto_follow=True)
        title = res.html.title.string
        assert_equal('OSF | Dashboard', title)

    def test_can_see_make_public_button_if_admin(self):
        # User is a contributor on a project
        project = ProjectFactory()
        project.add_contributor(
            self.user,
            permissions=['read', 'write', 'admin'],
            save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_in('Make Public', res)

    def test_cant_see_make_public_button_if_not_admin(self):
        # User is a contributor on a project
        project = ProjectFactory()
        project.add_contributor(
            self.user,
            permissions=['read', 'write'],
            save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_not_in('Make Public', res)

    def test_can_see_make_private_button_if_admin(self):
        # User is a contributor on a project
        project = ProjectFactory(is_public=True)
        project.add_contributor(
            self.user,
            permissions=['read', 'write', 'admin'],
            save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_in('Make Private', res)

    def test_cant_see_make_private_button_if_not_admin(self):
        # User is a contributor on a project
        project = ProjectFactory(is_public=True)
        project.add_contributor(
            self.user,
            permissions=['read', 'write'],
            save=True)
        # User goes to the project page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        assert_not_in('Make Private', res)

    def test_sees_logs_on_a_project(self):
        project = ProjectFactory(is_public=True)
        # User goes to the project's page
        res = self.app.get(project.url, auth=self.auth).maybe_follow()
        # Can see log event
        assert_in('created', res)

    @unittest.skip('"No wiki content" replaced with javascript handling')
    def test_no_wiki_content_message(self):
        project = ProjectFactory(creator=self.user)
        # Goes to project's wiki, where there is no content
        res = self.app.get('/{0}/wiki/home/'.format(project._primary_key), auth=self.auth)
        # Sees a message indicating no content
        assert_in('No wiki content', res)

    def test_wiki_page_name_non_ascii(self):
        project = ProjectFactory(creator=self.user)
        non_ascii = to_mongo_key('WöRlÐé')
        self.app.get('/{0}/wiki/{1}/'.format(
            project._primary_key,
            non_ascii
        ), auth=self.auth, expect_errors=True)
        project.update_node_wiki(non_ascii, 'new content', Auth(self.user))
        assert_in(non_ascii, project.wiki_pages_current)

    def test_noncontributor_cannot_see_wiki_if_no_content(self):
        user2 = UserFactory()
        # user2 creates a public project and adds no wiki content
        project = ProjectFactory(creator=user2, is_public=True)
        # self navigates to project
        res = self.app.get(project.url).maybe_follow()
        # Should not see wiki widget (since non-contributor and no content)
        assert_not_in('No wiki content', res)

    @unittest.skip(reason='¯\_(ツ)_/¯ knockout.')
    def test_wiki_does_not_exist(self):
        project = ProjectFactory(creator=self.user)
        res = self.app.get('/{0}/wiki/{1}/'.format(
            project._primary_key,
            'not a real page yet',
        ), auth=self.auth, expect_errors=True)
        assert_in('No wiki content', res)

    def test_sees_own_profile(self):
        res = self.app.get('/profile/', auth=self.auth)
        td1 = res.html.find('td', text=re.compile(r'Public(.*?)Profile'))
        td2 = td1.find_next_sibling('td')
        assert_equal(td2.text, self.user.display_absolute_url)

    def test_sees_another_profile(self):
        user2 = UserFactory()
        res = self.app.get(user2.url, auth=self.auth)
        td1 = res.html.find('td', text=re.compile(r'Public(.*?)Profile'))
        td2 = td1.find_next_sibling('td')
        assert_equal(td2.text, user2.display_absolute_url)

    # Regression test for https://github.com/CenterForOpenScience/osf.io/issues/1320
    @mock.patch('framework.auth.views.mails.send_mail')
    def test_can_reset_password(self, mock_send_mail):
        # A registered user
        user = UserFactory()
        # goes to the login page
        url = web_url_for('auth_login')
        res = self.app.get(url)
        # and fills out forgot password form
        form = res.forms['forgotPassword']
        form['forgot_password-email'] = user.username
        # submits
        res = form.submit()
        # mail was sent
        mock_send_mail.assert_called
        # gets 200 response
        assert_equal(res.status_code, 200)
        # URL is /forgotpassword
        assert_equal(res.request.path, web_url_for('forgot_password'))
예제 #5
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)