Ejemplo n.º 1
0
    def test_job_fast_epic(self):
        """
        Carries out the epic 'Fast Job', where a user wants to add their articles to
        their private libraries so that they can send it on to a prospective
        employer

        :return: no return
        """

        # Mary creates a private library and
        #   1. Gives it a name.
        #   2. Gives it a description.
        #   3. Makes it public to view.

        # Stub data
        user_mary = UserShop()
        user_random = UserShop()
        stub_library = LibraryShop(want_bibcode=True, public=True)

        self.assertIs(list, type(stub_library.get_bibcodes()))
        self.assertIs(list, type(stub_library.user_view_post_data['bibcode']))

        # Make the library and make it public to be viewed by employers
        url = url_for('userview')
        response = self.client.post(
            url,
            data=stub_library.user_view_post_data_json,
            headers=user_mary.headers
        )
        library_id = response.json['id']
        self.assertEqual(response.status_code, 200, response)
        self.assertTrue('bibcode' in response.json)
        self.assertTrue(response.json['name'] == stub_library.name)

        # She then asks a friend to check the link, and it works fine.
        url = url_for('libraryview', library=library_id)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library.bibcode) as BQ, \
                MockEndPoint([user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_random.headers
            )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json['documents']),
                         len(stub_library.bibcode))

        # Accidentally tries to add the same bibcodes, but it does not work as
        # expected
        url = url_for('documentview', library=library_id)
        response = self.client.post(
            url,
            data=stub_library.document_view_post_data_json('add'),
            headers=user_mary.headers
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['number_added'], 0)
Ejemplo n.º 2
0
    def test_job_fast_epic(self):
        """
        Carries out the epic 'Fast Job', where a user wants to add their articles to
        their private libraries so that they can send it on to a prospective
        employer

        :return: no return
        """

        # Mary creates a private library and
        #   1. Gives it a name.
        #   2. Gives it a description.
        #   3. Makes it public to view.

        # Stub data
        user_mary = UserShop()
        user_random = UserShop()
        stub_library = LibraryShop(want_bibcode=True, public=True)

        self.assertIs(list, type(stub_library.get_bibcodes()))
        self.assertIs(list, type(stub_library.user_view_post_data['bibcode']))

        # Make the library and make it public to be viewed by employers
        url = url_for('userview')
        response = self.client.post(url,
                                    data=stub_library.user_view_post_data_json,
                                    headers=user_mary.headers)
        library_id = response.json['id']
        self.assertEqual(response.status_code, 200, response)
        self.assertTrue('bibcode' in response.json)
        self.assertTrue(response.json['name'] == stub_library.name)

        # She then asks a friend to check the link, and it works fine.
        url = url_for('libraryview', library=library_id)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library.bibcode) as BQ, \
                MockEndPoint([user_mary]) as EP:
            response = self.client.get(url, headers=user_random.headers)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json['documents']),
                         len(stub_library.bibcode))

        # Accidentally tries to add the same bibcodes, but it does not work as
        # expected
        url = url_for('documentview', library=library_id)
        response = self.client.post(
            url,
            data=stub_library.document_view_post_data_json('add'),
            headers=user_mary.headers)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['number_added'], 0)
    def helper_bb_classic_user_epic(self, harbour_view):
        """
        Carries out the epic 'Bumblebee and Classic User', where a user that
        comes to the new interface makes some libraries, and has some permission
        to access other libraries from other users.
        The user then imports some libraries from ADS Classic, where some have
        similar names with that of the ones they previously made. It is assumed
        they have already setup their ADS credentials
        """
        # Stub data
        user_gpa = UserShop()
        user_mary = UserShop()
        stub_library_1 = LibraryShop(want_bibcode=True, public=True)
        stub_library_2 = LibraryShop(want_bibcode=True, public=True)

        # Gpa navigates the search pages and adds some bibcodes to some a few
        # libraries.
        url = url_for('userview')
        response = self.client.post(
            url,
            data=stub_library_1.user_view_post_data_json,
            headers=user_gpa.headers)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['bibcode'],
                         stub_library_1.get_bibcodes())

        # A friend adds them to one of their libraries with a similar name
        # # Make library
        url = url_for('userview')
        response = self.client.post(
            url,
            data=stub_library_1.user_view_post_data_json,
            headers=user_mary.headers)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['bibcode'],
                         stub_library_1.get_bibcodes())
        library_id_mary = response.json['id']

        # # Permission to read
        url = url_for('permissionview', library=library_id_mary)
        with MockEmailService(user_gpa):
            response = self.client.post(
                url,
                data=user_gpa.permission_view_post_data_json({
                    'read': True,
                    'write': False,
                    'admin': False,
                    'owner': False
                }),
                headers=user_mary.headers)
        self.assertEqual(response.status_code, 200)

        # Gpa imports all libraries from ADS Classic
        stub_library_2.bibcode = stub_library_1.bibcode.copy()
        stub_library_2.bibcode['new bibcode'] = {}

        url = url_for(harbour_view)
        with MockClassicService(status=200, libraries=[stub_library_2]):
            response = self.client.get(url, headers=user_gpa.headers)
        self.assertEqual(response.status_code, 200)

        # Gpa checks that the libraries were imported, and didn't affect the
        # friends libraries
        library_id_gpa = response.json[0]['library_id']

        url = url_for('libraryview', library=library_id_gpa)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_2.get_bibcodes()) as BQ, \
                MockEndPoint([user_gpa]) as EP:
            response = self.client.get(url, headers=user_gpa.headers)
        self.assertIn('new bibcode', response.json['documents'])

        # Check Mary's library
        url = url_for('userview')
        with MockEmailService(user_mary, end_type='uid'):
            response = self.client.get(url, headers=user_mary.headers)
        self.assertTrue(len(response.json['libraries']), 1)
        self.assertEqual(response.json['libraries'][0]['name'],
                         stub_library_1.name)

        url = url_for('libraryview', library=library_id_mary)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_1.get_bibcodes()) as BQ, \
                MockEndPoint([user_mary]) as EP:
            response = self.client.get(url, headers=user_mary.headers)
        self.assertNotIn('new bibcode', response.json['documents'])

        # Gpa then re-imports again by accident, but this is fine as this
        # should be an indempotent process
        url = url_for(harbour_view)
        with MockClassicService(status=200, libraries=[stub_library_2]):
            response = self.client.get(url, headers=user_gpa.headers)
        self.assertEqual(response.status_code, 200)
        library_id_gpa = response.json[0]['library_id']

        url = url_for('libraryview', library=library_id_gpa)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_2.get_bibcodes()) as BQ, \
                MockEndPoint([user_gpa]) as EP:
            response = self.client.get(url, headers=user_gpa.headers)
        self.assertIn('new bibcode', response.json['documents'])

        # Check Mary's library
        url = url_for('userview')
        with MockEmailService(user_mary, end_type='uid'):
            response = self.client.get(url, headers=user_mary.headers)
        self.assertTrue(len(response.json['libraries']), 1)
        self.assertEqual(response.json['libraries'][0]['name'],
                         stub_library_1.name)

        url = url_for('libraryview', library=library_id_mary)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_1.get_bibcodes()) as BQ, \
                MockEndPoint([user_mary]) as EP:
            response = self.client.get(url, headers=user_mary.headers)
        self.assertNotIn('new bibcode', response.json['documents'])
    def test_returned_data_library_view_epic(self):
        """
        Carries out the epic 'Returned Data', for the LibraryView GET end point
        that should return content similar to the UserView GET end point. This
        ensures the responses are as expected.

        :return: no return
        """

        # Stub data
        user_dave = UserShop()
        user_mary = UserShop()

        stub_library = LibraryShop()

        # Librarian Dave makes a library (no bibcodes)
        url = url_for('userview')
        response = self.client.post(url,
                                    data=stub_library.user_view_post_data_json,
                                    headers=user_dave.headers)
        self.assertEqual(response.status_code, 200, response)
        library_id_dave = response.json['id']

        # Dave looks at the library from the user view page and checks some
        # of the parameters displayed to him.
        with MockSolrBigqueryService(canonical_bibcode=stub_library.bibcode) \
                as BQ, MockEndPoint([user_dave]) as EP:
            url = url_for('libraryview', library=library_id_dave)
            response = self.client.get(url, headers=user_dave.headers)

        for key in ['documents', 'solr', 'metadata']:
            self.assertIn(key, response.json)

        documents = response.json['documents']
        solr = response.json['solr']
        metadata = response.json['metadata']
        self.assertTrue(metadata['num_documents'] == 0)
        self.assertTrue(metadata['num_users'] == 1)
        self.assertTrue(metadata['permission'] == 'owner')
        self.assertEqual(metadata['public'], False)
        self.assertEqual(metadata['owner'], user_dave.email.split('@')[0])
        date_created = datetime.strptime(metadata['date_created'],
                                         '%Y-%m-%dT%H:%M:%S.%f')
        date_last_modified = datetime.strptime(metadata['date_last_modified'],
                                               '%Y-%m-%dT%H:%M:%S.%f')
        self.assertAlmostEqual(date_created,
                               date_last_modified,
                               delta=timedelta(seconds=1))

        # Dave adds content to his library
        number_of_documents = 20
        for i in range(number_of_documents):

            # Stub data
            library = LibraryShop()

            # Add document
            url = url_for('documentview', library=library_id_dave)
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_dave.headers)
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)

            documents.append(library.get_bibcodes()[0])

        # Dave looks in the library overview and sees that his library size
        # has increased
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ, \
                MockEndPoint([user_dave]) as EP:
            response = self.client.get(url, headers=user_dave.headers)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(
            response.json['metadata']['num_documents'] == number_of_documents)

        # Dave adds mary so that she can see the library and add content
        url = url_for('permissionview', library=library_id_dave)
        with MockEmailService(user_mary):
            response = self.client.post(
                url,
                data=user_mary.permission_view_post_data_json('admin', True),
                headers=user_dave.headers)
        self.assertEqual(response.status_code, 200)

        # Mary sees that the number of users of the library has increased by 1
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ,\
                MockEndPoint([user_mary, user_dave]) as EP:
            response = self.client.get(url, headers=user_mary.headers)
        self.assertEqual(response.status_code, 200)
        library = response.json['metadata']
        self.assertTrue(library['num_users'] == 2)
        self.assertTrue(library['permission'] == 'admin')

        # Mary adds content to the library
        number_of_documents_second = 1
        for i in range(number_of_documents_second):

            # Stub data
            library = LibraryShop()

            # Add document
            url = url_for('documentview', library=library_id_dave)
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_mary.headers)
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)
            documents.append(library.get_bibcodes()[0])

        # Dave sees that the number of bibcodes has increased and that the
        # last modified date has changed, but the created date has not
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ, \
                MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(url, headers=user_dave.headers)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(response.json['metadata']['num_documents'] == (
            number_of_documents + number_of_documents_second))

        # This is to artificial alter the update time
        time.sleep(1)

        # Dave makes the library public.
        url = url_for('documentview', library=library_id_dave)
        response = self.client.put(
            url,
            data=library.document_view_put_data_json(public=True),
            headers=user_dave.headers)
        self.assertEqual(response.status_code, 200)

        # Dave sees that the lock sign from his library page has dissapeared
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ,\
                MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(url, headers=user_dave.headers)

        libraries = response.json['metadata']
        self.assertTrue(libraries['num_documents'] == number_of_documents + 1)
        self.assertTrue(libraries['public'])
        date_created_2 = datetime.strptime(libraries['date_created'],
                                           '%Y-%m-%dT%H:%M:%S.%f')
        date_last_modified_2 = \
            datetime.strptime(libraries['date_last_modified'],
                              '%Y-%m-%dT%H:%M:%S.%f')
        self.assertEqual(date_created, date_created_2)
        self.assertNotAlmostEqual(date_created_2,
                                  date_last_modified_2,
                                  delta=timedelta(seconds=1))
Ejemplo n.º 5
0
    def test_big_share_editor(self):
        """
        Carries out the epic 'Big Share Editor', where a user creates a library
        and wants one other use to have editing permissions, i.e., add and
        remove bibcodes from the library.

        :return: no return
        """

        # Stub data for users, etc.
        user_dave = UserShop()
        user_mary = UserShop()
        library_dave = LibraryShop()

        # Librarian Dave makes a big library full of content
        url = url_for('userview')
        response = self.client.post(url,
                                    data=library_dave.user_view_post_data_json,
                                    headers=user_dave.headers)
        library_id_dave = response.json['id']
        self.assertEqual(response.status_code, 200, response)

        # Dave adds content to his library
        libraries_added = []
        number_of_documents = 20
        for i in range(number_of_documents):
            # Add document

            library = LibraryShop()

            url = url_for('documentview', library=library_id_dave)
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_dave.headers)
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)

            libraries_added.append(library)

        # Checks they are all in the library
        url = url_for('libraryview', library=library_id_dave)
        canonical_bibcode = [i.get_bibcodes()[0] for i in libraries_added]
        with MockSolrBigqueryService(
                canonical_bibcode=canonical_bibcode) as BQ, \
                MockEndPoint([user_dave]) as EP:
            response = self.client.get(url, headers=user_dave.headers)
        self.assertTrue(len(response.json['documents']) == number_of_documents)

        # Dave is too busy to do any work on the library and so asks his
        # librarian friend Mary to do it. Dave does not realise she cannot
        # add without permissions and Mary gets some error messages
        url = url_for('documentview', library=library_id_dave)
        response = self.client.post(
            url,
            data=library.document_view_post_data_json('add'),
            headers=user_mary.headers)
        self.assertEqual(response.status_code, NO_PERMISSION_ERROR['number'])
        self.assertEqual(response.json['error'], NO_PERMISSION_ERROR['body'])

        # Dave now adds her account to permissions. She already has an ADS
        # account, and so Dave adds her with her e-mail address with read and
        # write permissions (but not admin).
        url = url_for('permissionview', library=library_id_dave)
        with MockEmailService(user_mary):
            response = self.client.post(
                url,
                data=user_mary.permission_view_post_data_json({
                    'read': False,
                    'write': True,
                    'admin': False,
                    'owner': False
                }),
                headers=user_dave.headers)
        self.assertEqual(response.status_code, 200)

        # Mary looks at the library
        canonical_bibcode = [i.get_bibcodes()[0] for i in libraries_added]
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(
                canonical_bibcode=canonical_bibcode) as BQ, \
                MockEndPoint([user_dave, user_dave]) as EP:
            response = self.client.get(url, headers=user_mary.headers)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(len(response.json['documents']) == number_of_documents)

        # Mary removes a few bibcodes and keeps a list of the ones she
        # removed just in case
        url = url_for('documentview', library=library_id_dave)

        libraries_removed = []
        for i in range(number_of_documents // 2):
            # Remove documents
            response = self.client.post(
                url,
                data=libraries_added[i].document_view_post_data_json('remove'),
                headers=user_mary.headers)
            self.assertEqual(response.json['number_removed'],
                             len(libraries_added[i].bibcode))
            self.assertEqual(response.status_code, 200, response)

            libraries_removed.append(libraries_added[i])
            libraries_added.remove(libraries_added[i])

        # She checks that they got removed
        canonical_bibcode = [i.get_bibcodes()[0] for i in libraries_added]
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(
                canonical_bibcode=canonical_bibcode) as BQ, \
                MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(url, headers=user_mary.headers)
        self.assertTrue(
            len(response.json['documents']) == number_of_documents // 2)

        # Dave asks Mary to re-add the ones she removed because they were
        # actually useful
        url = url_for('documentview', library=library_id_dave)
        for library in libraries_removed:
            # Add documents
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_mary.headers)
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)

            libraries_added.append(library)
            canonical_bibcode.extend(library.get_bibcodes())

        # She checks that they got added
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=canonical_bibcode) \
                as BQ, MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(url, headers=user_mary.headers)
        self.assertTrue(len(response.json['documents']) == number_of_documents)

        # Sanity check
        # Dave removes her permissions and Mary tries to modify the library
        # content, but cannot
        url = url_for('permissionview', library=library_id_dave)
        with MockEmailService(user_mary):
            response = self.client.post(
                url,
                data=user_mary.permission_view_post_data_json({
                    'read': False,
                    'write': False,
                    'admin': False,
                    'owner': False
                }),
                headers=user_dave.headers)
        self.assertEqual(response.status_code, 200)

        # Mary tries to add content
        url = url_for('documentview', library=library_id_dave)
        response = self.client.post(
            url,
            data=library.document_view_post_data_json('add'),
            headers=user_mary.headers)
        self.assertEqual(response.status_code, NO_PERMISSION_ERROR['number'])
        self.assertEqual(response.json['error'], NO_PERMISSION_ERROR['body'])
    def helper_bb_classic_user_epic(self, harbour_view):
        """
        Carries out the epic 'Bumblebee and Classic User', where a user that
        comes to the new interface makes some libraries, and has some permission
        to access other libraries from other users.
        The user then imports some libraries from ADS Classic, where some have
        similar names with that of the ones they previously made. It is assumed
        they have already setup their ADS credentials
        """
        # Stub data
        user_gpa = UserShop()
        user_mary = UserShop()
        stub_library_1 = LibraryShop(want_bibcode=True, public=True)
        stub_library_2 = LibraryShop(want_bibcode=True, public=True)

        # Gpa navigates the search pages and adds some bibcodes to some a few
        # libraries.
        url = url_for('userview')
        response = self.client.post(
            url,
            data=stub_library_1.user_view_post_data_json,
            headers=user_gpa.headers
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['bibcode'], stub_library_1.get_bibcodes())

        # A friend adds them to one of their libraries with a similar name
        # # Make library
        url = url_for('userview')
        response = self.client.post(
            url,
            data=stub_library_1.user_view_post_data_json,
            headers=user_mary.headers
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['bibcode'], stub_library_1.get_bibcodes())
        library_id_mary = response.json['id']

        # # Permission to read
        url = url_for('permissionview', library=library_id_mary)
        with MockEmailService(user_gpa):
            response = self.client.post(
                url,
                data=user_gpa.permission_view_post_data_json('read', True),
                headers=user_mary.headers
            )
        self.assertEqual(response.status_code, 200)

        # Gpa imports all libraries from ADS Classic
        stub_library_2.bibcode = stub_library_1.bibcode.copy()
        stub_library_2.bibcode['new bibcode'] = {}

        url = url_for(harbour_view)
        with MockClassicService(status=200, libraries=[stub_library_2]):
            response = self.client.get(url, headers=user_gpa.headers)
        self.assertEqual(response.status_code, 200)

        # Gpa checks that the libraries were imported, and didn't affect the
        # friends libraries
        library_id_gpa = response.json[0]['library_id']

        url = url_for('libraryview', library=library_id_gpa)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_2.get_bibcodes()) as BQ, \
                MockEndPoint([user_gpa]) as EP:
            response = self.client.get(
                url,
                headers=user_gpa.headers
            )
        self.assertIn('new bibcode', response.json['documents'])

        # Check Mary's library
        url = url_for('userview')
        with MockEmailService(user_mary, end_type='uid'):
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertTrue(len(response.json['libraries']), 1)
        self.assertEqual(response.json['libraries'][0]['name'], stub_library_1.name)

        url = url_for('libraryview', library=library_id_mary)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_1.get_bibcodes()) as BQ, \
                MockEndPoint([user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertNotIn('new bibcode', response.json['documents'])

        # Gpa then re-imports again by accident, but this is fine as this
        # should be an indempotent process
        url = url_for(harbour_view)
        with MockClassicService(status=200, libraries=[stub_library_2]):
            response = self.client.get(url, headers=user_gpa.headers)
        self.assertEqual(response.status_code, 200)
        library_id_gpa = response.json[0]['library_id']

        url = url_for('libraryview', library=library_id_gpa)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_2.get_bibcodes()) as BQ, \
                MockEndPoint([user_gpa]) as EP:
            response = self.client.get(
                url,
                headers=user_gpa.headers
            )
        self.assertIn('new bibcode', response.json['documents'])

        # Check Mary's library
        url = url_for('userview')
        with MockEmailService(user_mary, end_type='uid'):
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertTrue(len(response.json['libraries']), 1)
        self.assertEqual(response.json['libraries'][0]['name'], stub_library_1.name)

        url = url_for('libraryview', library=library_id_mary)
        with MockSolrBigqueryService(
                canonical_bibcode=stub_library_1.get_bibcodes()) as BQ, \
                MockEndPoint([user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertNotIn('new bibcode', response.json['documents'])
    def test_returned_data_library_view_epic(self):
        """
        Carries out the epic 'Returned Data', for the LibraryView GET end point
        that should return content similar to the UserView GET end point. This
        ensures the responses are as expected.

        :return: no return
        """

        # Stub data
        user_dave = UserShop()
        user_mary = UserShop()

        stub_library = LibraryShop()

        # Librarian Dave makes a library (no bibcodes)
        url = url_for('userview')
        response = self.client.post(
            url,
            data=stub_library.user_view_post_data_json,
            headers=user_dave.headers
        )
        self.assertEqual(response.status_code, 200, response)
        library_id_dave = response.json['id']

        # Dave looks at the library from the user view page and checks some
        # of the parameters displayed to him.
        with MockSolrBigqueryService(canonical_bibcode=stub_library.bibcode) \
                as BQ, MockEndPoint([user_dave]) as EP:
            url = url_for('libraryview', library=library_id_dave)
            response = self.client.get(
                url,
                headers=user_dave.headers
            )

        for key in ['documents', 'solr', 'metadata']:
            self.assertIn(key, response.json)

        documents = response.json['documents']
        solr = response.json['solr']
        metadata = response.json['metadata']
        self.assertTrue(metadata['num_documents'] == 0)
        self.assertTrue(metadata['num_users'] == 1)
        self.assertTrue(metadata['permission'] == 'owner')
        self.assertEqual(metadata['public'], False)
        self.assertEqual(metadata['owner'], user_dave.email.split('@')[0])
        date_created = datetime.strptime(metadata['date_created'],
                                         '%Y-%m-%dT%H:%M:%S.%f')
        date_last_modified = datetime.strptime(metadata['date_last_modified'],
                                               '%Y-%m-%dT%H:%M:%S.%f')
        self.assertAlmostEqual(date_created,
                               date_last_modified,
                               delta=timedelta(seconds=1))

        # Dave adds content to his library
        number_of_documents = 20
        for i in range(number_of_documents):

            # Stub data
            library = LibraryShop()

            # Add document
            url = url_for('documentview', library=library_id_dave)
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_dave.headers
            )
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)

            documents.append(library.get_bibcodes()[0])

        # Dave looks in the library overview and sees that his library size
        # has increased
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ, \
                MockEndPoint([user_dave]) as EP:
            response = self.client.get(
                url,
                headers=user_dave.headers
            )
        self.assertEqual(response.status_code, 200)
        self.assertTrue(
            response.json['metadata']['num_documents'] == number_of_documents
        )

        # Dave adds mary so that she can see the library and add content
        url = url_for('permissionview', library=library_id_dave)
        with MockEmailService(user_mary):
            response = self.client.post(
                url,
                data=user_mary.permission_view_post_data_json('admin', True),
                headers=user_dave.headers
            )
        self.assertEqual(response.status_code, 200)

        # Mary sees that the number of users of the library has increased by 1
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ,\
                MockEndPoint([user_mary, user_dave]) as EP:
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertEqual(response.status_code, 200)
        library = response.json['metadata']
        self.assertTrue(library['num_users'] == 2)
        self.assertTrue(library['permission'] == 'admin')

        # Mary adds content to the library
        number_of_documents_second = 1
        for i in range(number_of_documents_second):

            # Stub data
            library = LibraryShop()

            # Add document
            url = url_for('documentview', library=library_id_dave)
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_mary.headers
            )
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)
            documents.append(library.get_bibcodes()[0])

        # Dave sees that the number of bibcodes has increased and that the
        # last modified date has changed, but the created date has not
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ, \
                MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_dave.headers
            )
        self.assertEqual(response.status_code, 200)
        self.assertTrue(
            response.json['metadata']['num_documents']
            == (number_of_documents+number_of_documents_second)
        )

        # This is to artificial alter the update time
        time.sleep(1)

        # Dave makes the library public.
        url = url_for('documentview', library=library_id_dave)
        response = self.client.put(
            url,
            data=library.document_view_put_data_json(public=True),
            headers=user_dave.headers
        )
        self.assertEqual(response.status_code, 200)

        # Dave sees that the lock sign from his library page has dissapeared
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=documents) as BQ,\
                MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_dave.headers
            )

        libraries = response.json['metadata']
        self.assertTrue(
            libraries['num_documents'] == number_of_documents+1
        )
        self.assertTrue(libraries['public'])
        date_created_2 = datetime.strptime(libraries['date_created'],
                                           '%Y-%m-%dT%H:%M:%S.%f')
        date_last_modified_2 = \
            datetime.strptime(libraries['date_last_modified'],
                              '%Y-%m-%dT%H:%M:%S.%f')
        self.assertEqual(date_created, date_created_2)
        self.assertNotAlmostEqual(date_created_2,
                                  date_last_modified_2,
                                  delta=timedelta(seconds=1))
    def test_big_share_editor(self):
        """
        Carries out the epic 'Big Share Editor', where a user creates a library
        and wants one other use to have editing permissions, i.e., add and
        remove bibcodes from the library.

        :return: no return
        """

        # Stub data for users, etc.
        user_dave = UserShop()
        user_mary = UserShop()
        library_dave = LibraryShop()

        # Librarian Dave makes a big library full of content
        url = url_for('userview')
        response = self.client.post(
            url,
            data=library_dave.user_view_post_data_json,
            headers=user_dave.headers
        )
        library_id_dave = response.json['id']
        self.assertEqual(response.status_code, 200, response)

        # Dave adds content to his library
        libraries_added = []
        number_of_documents = 20
        for i in range(number_of_documents):
            # Add document

            library = LibraryShop()

            url = url_for('documentview', library=library_id_dave)
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_dave.headers
            )
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)

            libraries_added.append(library)

        # Checks they are all in the library
        url = url_for('libraryview', library=library_id_dave)
        canonical_bibcode = [i.get_bibcodes()[0] for i in libraries_added]
        with MockSolrBigqueryService(
                canonical_bibcode=canonical_bibcode) as BQ, \
                MockEndPoint([user_dave]) as EP:
            response = self.client.get(
                url,
                headers=user_dave.headers
            )
        self.assertTrue(len(response.json['documents']) == number_of_documents)

        # Dave is too busy to do any work on the library and so asks his
        # librarian friend Mary to do it. Dave does not realise she cannot
        # add without permissions and Mary gets some error messages
        url = url_for('documentview', library=library_id_dave)
        response = self.client.post(
            url,
            data=library.document_view_post_data_json('add'),
            headers=user_mary.headers
        )
        self.assertEqual(response.status_code, NO_PERMISSION_ERROR['number'])
        self.assertEqual(response.json['error'], NO_PERMISSION_ERROR['body'])

        # Dave now adds her account to permissions. She already has an ADS
        # account, and so Dave adds her with her e-mail address with read and
        # write permissions (but not admin).
        url = url_for('permissionview', library=library_id_dave)
        with MockEmailService(user_mary):
            response = self.client.post(
                url,
                data=user_mary.permission_view_post_data_json('write', True),
                headers=user_dave.headers
            )
        self.assertEqual(response.status_code, 200)

        # Mary looks at the library
        canonical_bibcode = [i.get_bibcodes()[0] for i in libraries_added]
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(
                canonical_bibcode=canonical_bibcode) as BQ, \
                MockEndPoint([user_dave, user_dave]) as EP:
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertEqual(response.status_code, 200)
        self.assertTrue(len(response.json['documents']) == number_of_documents)

        # Mary removes a few bibcodes and keeps a list of the ones she
        # removed just in case
        url = url_for('documentview', library=library_id_dave)

        libraries_removed = []
        for i in range(number_of_documents/2):
            # Remove documents
            response = self.client.post(
                url,
                data=libraries_added[i].document_view_post_data_json('remove'),
                headers=user_mary.headers
            )
            self.assertEqual(response.json['number_removed'],
                             len(libraries_added[i].bibcode))
            self.assertEqual(response.status_code, 200, response)

            libraries_removed.append(libraries_added[i])
            libraries_added.remove(libraries_added[i])

        # She checks that they got removed
        canonical_bibcode = [i.get_bibcodes()[0] for i in libraries_added]
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(
                canonical_bibcode=canonical_bibcode) as BQ, \
                MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertTrue(
            len(response.json['documents']) == number_of_documents/2
        )

        # Dave asks Mary to re-add the ones she removed because they were
        # actually useful
        url = url_for('documentview', library=library_id_dave)
        for library in libraries_removed:
            # Add documents
            response = self.client.post(
                url,
                data=library.document_view_post_data_json('add'),
                headers=user_mary.headers
            )
            self.assertEqual(response.json['number_added'],
                             len(library.bibcode))
            self.assertEqual(response.status_code, 200, response)

            libraries_added.append(library)
            canonical_bibcode.extend(library.get_bibcodes())

        # She checks that they got added
        url = url_for('libraryview', library=library_id_dave)
        with MockSolrBigqueryService(canonical_bibcode=canonical_bibcode) \
                as BQ, MockEndPoint([user_dave, user_mary]) as EP:
            response = self.client.get(
                url,
                headers=user_mary.headers
            )
        self.assertTrue(
            len(response.json['documents']) == number_of_documents
        )

        # Sanity check
        # Dave removes her permissions and Mary tries to modify the library
        # content, but cannot
        url = url_for('permissionview', library=library_id_dave)
        with MockEmailService(user_mary):
            response = self.client.post(
                url,
                data=user_mary.permission_view_post_data_json('write', False),
                headers=user_dave.headers
            )
        self.assertEqual(response.status_code, 200)

        # Mary tries to add content
        url = url_for('documentview', library=library_id_dave)
        response = self.client.post(
            url,
            data=library.document_view_post_data_json('add'),
            headers=user_mary.headers
        )
        self.assertEqual(response.status_code, NO_PERMISSION_ERROR['number'])
        self.assertEqual(response.json['error'], NO_PERMISSION_ERROR['body'])