예제 #1
0
    def test_edit_notebook(self):
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format())
        self.notebook.save()
        c = Client()
        c.login(username=self.user.username, password='******')

        response = c.post('/edit-notebook/{}/'.format(self.notebook.id), {
            'description': 'edited',
            'tags': 'notfoo, notbar',
            'data_sources': 'new_data_source',
        },
                          follow=True)
        self.assertEqual(response.status_code, 200)
        updated_nb = SharedNotebook.objects.get(pk=self.notebook.pk)
        self.assertEqual(updated_nb.description, 'edited')
예제 #2
0
 def setUp(self):
     settings.DEBUG = True
     settings.JUPYTERHUB_BASE_URL = 'http://example.com/'
     self.factory = RequestFactory()
     self.oh_member = OpenHumansMember.create(
                         oh_id=1234,
                         oh_username='******',
                         access_token='foo',
                         refresh_token='bar',
                         expires_in=36000)
     self.oh_member.save()
     self.notebook = SharedNotebook(
         oh_member=self.oh_member,
         notebook_name='test_notebook.ipynb',
         notebook_content=open(
             'main/tests/fixtures/test_notebook.ipynb').read(),
         description='test_description',
         tags='["foo", "bar"]',
         data_sources='["source1", "source2"]',
         views=123,
         updated_at=arrow.now().format(),
         created_at=arrow.now().format()
     )
     self.notebook.save()
     self.oh_member_two = OpenHumansMember.create(
                         oh_id=2345,
                         oh_username='******',
                         access_token='foo',
                         refresh_token='bar',
                         expires_in=36000)
     self.oh_member_two.save()
예제 #3
0
 def setUp(self):
     settings.DEBUG = True
     settings.OPENHUMANS_CLIENT_ID = "6yNYmUlXN1wLwQFQR0lnUohR1KMeVt"
     settings.OPENHUMANS_CLIENT_SECRET = "Y2xpZW50aWQ6Y2xpZW50c2VjcmV0"
     settings.OPENHUMANS_APP_BASE_URL = "http://127.0.0.1:5000"
     self.factory = RequestFactory()
     self.oh_member = OpenHumansMember.create(oh_id=1234,
                                              oh_username='******',
                                              access_token='foobar',
                                              refresh_token='bar',
                                              expires_in=36000)
     self.oh_member.save()
     self.user = self.oh_member.user
     self.user.set_password('foobar')
     self.user.save()
     self.notebook = SharedNotebook(
         oh_member=self.oh_member,
         notebook_name='test_notebook.ipynb',
         notebook_content=open(
             'main/tests/fixtures/test_notebook.ipynb').read(),
         description='test_description',
         tags='["foo", "bar"]',
         data_sources='["source1", "source2"]',
         views=123,
         updated_at=arrow.now().format(),
         created_at=arrow.now().format())
     self.notebook.save()
예제 #4
0
    def setUp(self):
        settings.DEBUG = True

        self.oh_member = OpenHumansMember.create(
                            oh_id=1234,
                            oh_username='******',
                            access_token='foo',
                            refresh_token='bar',
                            expires_in=36000)
        self.oh_member.save()
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format()
        )
        self.notebook.save()
        self.oh_member_two = OpenHumansMember.create(
                            oh_id=2345,
                            oh_username='******',
                            access_token='foo',
                            refresh_token='bar',
                            expires_in=36000)
        self.oh_member_two.save()
        self.oh_member_data = {
            'created': '2018-01-19T21:55:40.049169Z',
            'next': None,
            'project_member_id': '1234',
            'message_permission': True,
            'sources_shared': ['direct-sharing-71'],
            'username': '******',
            'data': [{
                'id': 12,
                'source': 'direct-sharing-71',
                'basename': 'test_notebook.ipynb',
                'created': '2018-06-06T17:09:26.688794Z',
                'download_url': 'http://example.com/test_notebook.ipynb',
                'metadata': {
                    'tags': ['personal data notebook', 'notebook', 'jupyter'],
                    'description': 'A Personal Data Notebook'}
                    }]}
예제 #5
0
 def test_notebook_index(self):
     self.second_notebook = SharedNotebook(
         oh_member=self.oh_member,
         notebook_name='second_test.ipynb',
         notebook_content=open(
             'main/tests/fixtures/test_notebook.ipynb').read(),
         description='test_description',
         tags='["foo2", "bar2"]',
         data_sources='["source3", "source4"]',
         views=123,
         updated_at=arrow.now().format(),
         created_at=arrow.now().format())
     self.second_notebook.save()
     c = Client()
     response = c.get('/notebooks/')
     self.assertContains(response, 'source2', 4, status_code=200)
     response_filtered = c.get('/notebooks/?source=source2')
     self.assertContains(response_filtered, 'source2', status_code=200)
     self.assertContains(response_filtered, 'source3', 2, status_code=200)
예제 #6
0
 def test_add_comment(self):
     c = Client()
     c.login(username=self.user.username, password='******')
     self.assertEqual(len(NotebookComment.objects.all()), 0)
     self.notebook = SharedNotebook(
         oh_member=self.oh_member,
         notebook_name='test_notebook.ipynb',
         notebook_content=open(
             'main/tests/fixtures/test_notebook.ipynb').read(),
         description='test_description',
         tags='["foo", "bar"]',
         data_sources='["source1", "source2"]',
         views=123,
         updated_at=arrow.now().format(),
         created_at=arrow.now().format())
     self.notebook.save()
     response = c.post('/add-comment/{}/'.format(self.notebook.id),
                       {'comment_text': 'stupid comment'},
                       follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(NotebookComment.objects.all()), 1)
예제 #7
0
 def test_signal_nb_delete(self):
     self.notebook_two = SharedNotebook(
         pk=2,
         oh_member=self.oh_member_two,
         notebook_name='test_notebook.ipynb',
         notebook_content=open(
             'main/tests/fixtures/test_notebook.ipynb').read(),
         description='test_description',
         tags='["foo", "bar"]',
         data_sources='["source1", "source2"]',
         views=123,
         updated_at=arrow.now().format(),
         created_at=arrow.now().format(),
         master_notebook=self.notebook
     )
     self.notebook_two.save()
     self.assertEqual(self.notebook_two.master_notebook, self.notebook)
     self.notebook_three = SharedNotebook(
         pk=3,
         oh_member=self.oh_member_two,
         notebook_name='test_notebook.ipynb',
         notebook_content=open(
             'main/tests/fixtures/test_notebook.ipynb').read(),
         description='test_description',
         tags='["foo", "bar"]',
         data_sources='["source1", "source2"]',
         views=123,
         updated_at=arrow.now().format(),
         created_at=arrow.now().format(),
         master_notebook=self.notebook
     )
     self.notebook_three.save()
     self.assertEqual(self.notebook_three.master_notebook, self.notebook)
     self.notebook.delete()
     my_handler('SharedNotebook', self.notebook)
     nb_two = SharedNotebook.objects.get(pk=2)
     nb_three = SharedNotebook.objects.get(pk=3)
     self.assertEqual(
         nb_three.master_notebook,
         nb_two)
예제 #8
0
class SharedNotebookTest(TestCase):
    def setUp(self):
        settings.DEBUG = True

        self.oh_member = OpenHumansMember.create(
                            oh_id=1234,
                            oh_username='******',
                            access_token='foo',
                            refresh_token='bar',
                            expires_in=36000)
        self.oh_member.save()
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format()
        )
        self.notebook.save()
        self.oh_member_two = OpenHumansMember.create(
                            oh_id=2345,
                            oh_username='******',
                            access_token='foo',
                            refresh_token='bar',
                            expires_in=36000)
        self.oh_member_two.save()
        self.oh_member_data = {
            'created': '2018-01-19T21:55:40.049169Z',
            'next': None,
            'project_member_id': '1234',
            'message_permission': True,
            'sources_shared': ['direct-sharing-71'],
            'username': '******',
            'data': [{
                'id': 12,
                'source': 'direct-sharing-71',
                'basename': 'test_notebook.ipynb',
                'created': '2018-06-06T17:09:26.688794Z',
                'download_url': 'http://example.com/test_notebook.ipynb',
                'metadata': {
                    'tags': ['personal data notebook', 'notebook', 'jupyter'],
                    'description': 'A Personal Data Notebook'}
                    }]}

    @vcr.use_cassette('main/tests/fixtures/suggested_sources.yaml')
    def test_notebook_present(self):
        suggested_sources = suggest_data_sources(
                                self.notebook.notebook_content)
        self.assertEqual(
            len(suggested_sources.split(',')),
            2)

    def test_identify_master_notebook(self):
        mnb = identify_master_notebook(
                'test_notebook.ipynb',
                self.oh_member_two)
        self.assertEqual(mnb, self.notebook)
        no_mnb = identify_master_notebook(
                'edited_test_notebook.ipynb',
                self.oh_member_two)
        self.assertEqual(no_mnb, None)

    def test_get_notebook_files(self):
        nb_files = get_notebook_files(self.oh_member_data)
        self.assertEqual(len(nb_files), 1)

    def test_get_notebook_oh(self):
        nbd = get_notebook_oh(
            oh_member_data=self.oh_member_data,
            notebook_id='12')
        self.assertEqual(
            nbd,
            ('test_notebook.ipynb', 'http://example.com/test_notebook.ipynb'))

    def test_metadata_functions(self):
        self.assertEqual(self.notebook.get_tags(),
                         'foo,bar')
        self.assertEqual(self.notebook.get_data_sources(),
                         'source1,source2')
예제 #9
0
class HelpersTest(TestCase):
    def setUp(self):
        settings.DEBUG = True
        settings.JUPYTERHUB_BASE_URL = 'http://example.com/'
        self.factory = RequestFactory()
        self.oh_member = OpenHumansMember.create(
                            oh_id=1234,
                            oh_username='******',
                            access_token='foo',
                            refresh_token='bar',
                            expires_in=36000)
        self.oh_member.save()
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format()
        )
        self.notebook.save()
        self.oh_member_two = OpenHumansMember.create(
                            oh_id=2345,
                            oh_username='******',
                            access_token='foo',
                            refresh_token='bar',
                            expires_in=36000)
        self.oh_member_two.save()

    def test_notebook_link(self):
        request = self.factory.get('/')
        self.assertEqual(
            ('http://example.com//notebook-import?notebook_location'
                '=http://testserver/export-notebook/1/&notebook_name'
                '=test_notebook.ipynb'),
            helpers.create_notebook_link(self.notebook, request))

    def test_notebook_search(self):
        sources = helpers.find_notebook_by_keywords('foo', 'data_sources')
        tags = helpers.find_notebook_by_keywords('foo', 'tags')
        user = helpers.find_notebook_by_keywords('test', 'username')
        self.assertEqual(len(sources), 0)
        self.assertEqual(len(tags), 1)
        self.assertEqual(len(user), 1)

    def test_paginator(self):
        queryset = SharedNotebook.objects.all().order_by('pk')
        pages = [1, 2, 'NaN']
        for page in pages:
            paginator = helpers.paginate_items(queryset, page)
            self.assertEqual(paginator.number, 1)

    def test_signal_nb_delete(self):
        self.notebook_two = SharedNotebook(
            pk=2,
            oh_member=self.oh_member_two,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format(),
            master_notebook=self.notebook
        )
        self.notebook_two.save()
        self.assertEqual(self.notebook_two.master_notebook, self.notebook)
        self.notebook_three = SharedNotebook(
            pk=3,
            oh_member=self.oh_member_two,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format(),
            master_notebook=self.notebook
        )
        self.notebook_three.save()
        self.assertEqual(self.notebook_three.master_notebook, self.notebook)
        self.notebook.delete()
        my_handler('SharedNotebook', self.notebook)
        nb_two = SharedNotebook.objects.get(pk=2)
        nb_three = SharedNotebook.objects.get(pk=3)
        self.assertEqual(
            nb_three.master_notebook,
            nb_two)

    def test_get_all_data_sources_numeric(self):
        sources_numeric = helpers.get_all_data_sources_numeric()
        self.assertIn(('source2', 1), sources_numeric)
        self.assertIn(('source1', 1), sources_numeric)

    def test_get_all_data_sources(self):
        sources = helpers.get_all_data_sources()
        self.assertIn('source2', sources)
        self.assertIn('source1', sources)
예제 #10
0
class GeneralTest(TestCase):
    def setUp(self):
        settings.DEBUG = True
        self.oh_member = OpenHumansMember.create(oh_id=1234,
                                                 oh_username='******',
                                                 access_token='foobar',
                                                 refresh_token='bar',
                                                 expires_in=36000)
        self.oh_member.save()
        self.user = self.oh_member.user
        self.user.set_password('foobar')
        self.user.save()

    @vcr.use_cassette('main/tests/fixtures/add_notebook.yaml',
                      record_mode='none')
    def test_add_notebook_not_logged_in(self):

        c = Client()
        response = c.post('/add-notebook-gallery/12/', {
            'description': 'foobar',
            'tags': 'test, tags',
            'data_sources': 'data,source'
        },
                          follow=True)
        self.assertEqual(response.status_code, 200)
        notebooks = SharedNotebook.objects.all()
        self.assertEqual(len(notebooks), 0)

    @vcr.use_cassette('main/tests/fixtures/add_notebook.yaml',
                      record_mode='none')
    def test_add_notebook_logged_in(self):

        c = Client()
        c.login(username=self.user.username, password='******')
        response = c.post('/add-notebook-gallery/12/', {
            'description': 'foobar',
            'tags': 'test, tags',
            'data_sources': 'data,source'
        },
                          follow=True)
        self.assertEqual(response.status_code, 200)
        notebooks = SharedNotebook.objects.all()
        self.assertEqual(len(notebooks), 1)
        self.assertEqual(notebooks[0].notebook_name,
                         'twitter-and-fitbit-activity.ipynb')

    def test_add_comment(self):
        c = Client()
        c.login(username=self.user.username, password='******')
        self.assertEqual(len(NotebookComment.objects.all()), 0)
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format())
        self.notebook.save()
        response = c.post('/add-comment/{}/'.format(self.notebook.id),
                          {'comment_text': 'stupid comment'},
                          follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(NotebookComment.objects.all()), 1)

    def test_edit_notebook(self):
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format())
        self.notebook.save()
        c = Client()
        c.login(username=self.user.username, password='******')

        response = c.post('/edit-notebook/{}/'.format(self.notebook.id), {
            'description': 'edited',
            'tags': 'notfoo, notbar',
            'data_sources': 'new_data_source',
        },
                          follow=True)
        self.assertEqual(response.status_code, 200)
        updated_nb = SharedNotebook.objects.get(pk=self.notebook.pk)
        self.assertEqual(updated_nb.description, 'edited')
예제 #11
0
class ViewTest(TestCase):
    def setUp(self):
        settings.DEBUG = True
        settings.OPENHUMANS_CLIENT_ID = "6yNYmUlXN1wLwQFQR0lnUohR1KMeVt"
        settings.OPENHUMANS_CLIENT_SECRET = "Y2xpZW50aWQ6Y2xpZW50c2VjcmV0"
        settings.OPENHUMANS_APP_BASE_URL = "http://127.0.0.1:5000"
        self.factory = RequestFactory()
        self.oh_member = OpenHumansMember.create(oh_id=1234,
                                                 oh_username='******',
                                                 access_token='foobar',
                                                 refresh_token='bar',
                                                 expires_in=36000)
        self.oh_member.save()
        self.user = self.oh_member.user
        self.user.set_password('foobar')
        self.user.save()
        self.notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='test_notebook.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo", "bar"]',
            data_sources='["source1", "source2"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format())
        self.notebook.save()

    def test_notebook_render(self):
        r = self.factory.get('/')
        rendered_notebook = render_notebook(r, self.notebook.id)
        self.assertEqual(rendered_notebook.status_code, 200)
        self.assertIsNotNone(rendered_notebook.content)

    def test_open_notebook(self):
        c = Client()
        self.assertEqual(self.notebook.views, 123)
        c.get('/open-notebook/{}/'.format(self.notebook.id))
        updated_nb = SharedNotebook.objects.get(pk=self.notebook.id)
        self.assertEqual(updated_nb.views, 124)
        c.get('/open-notebook/{}/'.format(self.notebook.id))
        updated_nb = SharedNotebook.objects.get(pk=self.notebook.id)
        self.assertEqual(updated_nb.views, 124)
        c.login(username=self.user.username, password='******')
        c.get('/open-notebook/{}/'.format(self.notebook.id))
        self.assertEqual(updated_nb.views, 124)

    def test_shared(self):
        c = Client()
        response = c.get('/shared/')
        self.assertEqual(response.status_code, 200)
        c.login(username=self.user.username, password='******')
        logged_in_response = c.get('/shared/')
        self.assertEqual(logged_in_response.status_code, 302)

    def test_index(self):
        c = Client()
        response = c.get('/')
        self.assertEqual(response.status_code, 200)
        c.login(username=self.user.username, password='******')
        logged_in_response = c.get('/')
        self.assertEqual(logged_in_response.status_code, 302)

    def test_about(self):
        c = Client()
        response = c.get('/about/')
        self.assertEqual(response.status_code, 200)

    def test_likes(self):
        c = Client()
        response = c.get('/likes')
        self.assertEqual(response.status_code, 301)
        c.login(username=self.user.username, password='******')
        logged_in_response = c.get('/likes/')
        self.assertEqual(logged_in_response.status_code, 200)

    @vcr.use_cassette('main/tests/fixtures/complete.yaml', record_mode='none')
    def test_complete(self):
        c = Client()
        self.assertEqual(1, OpenHumansMember.objects.all().count())
        response = c.get("/complete", {'code': 'mytestcode'}, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'main/index.html')
        self.assertEqual(2, OpenHumansMember.objects.all().count())

    def test_delete_user(self):
        c = Client()
        c.login(username=self.user.username, password='******')
        self.assertEqual(len(OpenHumansMember.objects.all()), 1)
        c.post('/delete-user/')
        self.assertEqual(len(OpenHumansMember.objects.all()), 0)

    def test_delete_notebook(self):
        c = Client()
        c.login(username=self.user.username, password='******')
        self.assertEqual(len(SharedNotebook.objects.all()), 1)
        c.post('/delete-notebook/{}/'.format(self.notebook.pk))
        self.assertEqual(len(SharedNotebook.objects.all()), 0)

    def test_notebook_like(self):
        c = Client()
        c.login(username=self.user.username, password='******')
        self.assertEqual(len(NotebookLike.objects.all()), 0)
        c.post('/like-notebook/{}/'.format(self.notebook.pk))
        self.assertEqual(len(NotebookLike.objects.all()), 1)
        c.post('/like-notebook/{}/'.format(self.notebook.pk))
        self.assertEqual(len(NotebookLike.objects.all()), 0)

    @vcr.use_cassette('main/tests/fixtures/suggested_sources.yaml',
                      record_mode='none')
    def test_search_notebooks(self):
        c = Client()
        post_response = c.post('/search/', {
            'search_term': 'source1',
        })
        self.assertContains(post_response,
                            "test_notebook.ipynb",
                            status_code=200)
        get_response = c.get('/search/', {
            'search_term': 'source1',
            'search_field': 'tags'
        })
        self.assertNotContains(get_response,
                               "test_notebook.ipynb",
                               status_code=200)

    def test_source_index(self):
        c = Client()
        post_response = c.get('/sources/')
        self.assertContains(post_response, "source1", status_code=200)

    def test_notebook_index(self):
        self.second_notebook = SharedNotebook(
            oh_member=self.oh_member,
            notebook_name='second_test.ipynb',
            notebook_content=open(
                'main/tests/fixtures/test_notebook.ipynb').read(),
            description='test_description',
            tags='["foo2", "bar2"]',
            data_sources='["source3", "source4"]',
            views=123,
            updated_at=arrow.now().format(),
            created_at=arrow.now().format())
        self.second_notebook.save()
        c = Client()
        response = c.get('/notebooks/')
        self.assertContains(response, 'source2', 4, status_code=200)
        response_filtered = c.get('/notebooks/?source=source2')
        self.assertContains(response_filtered, 'source2', status_code=200)
        self.assertContains(response_filtered, 'source3', 2, status_code=200)