Skip to content

Commit

Permalink
changing write accesses and cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
femalves committed Dec 6, 2023
1 parent d942fb7 commit d1b2677
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 168 deletions.
26 changes: 12 additions & 14 deletions biblib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,18 @@ def create_unique(cls, session, content, bibcode, library):
"""
Creates a new note in the database
"""
try:
if bibcode not in library.bibcode.keys():
raise BibcodeNotFoundError('Bibcode {0} not found in the library {1}'.format(bibcode, library.id))

existing_note = session.query(Notes).filter_by(bibcode=bibcode, library_id=library.id).first()

if existing_note:
raise DuplicateNoteError('Duplicate note for bibcode {0} and library {1}'.format(bibcode, library.id))

note = Notes(content=content, bibcode=bibcode, library_id=library.id)
return note
except Exception:
session.rollback()
raise

if bibcode not in library.bibcode.keys():
raise BibcodeNotFoundError('Bibcode {0} not found in the library {1}'.format(bibcode, library.id))

existing_note = session.query(Notes).filter_by(bibcode=bibcode, library_id=library.id).first()

if existing_note:
raise DuplicateNoteError('Duplicate note for bibcode {0} and library {1}'.format(bibcode, library.id))

note = Notes(content=content, bibcode=bibcode, library_id=library.id)
return note


class Library(Base):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def helper_bb_classic_user_epic(self, harbour_view):
headers=user_mary.headers
)

self.assertTrue(len(response.json['my_libraries']), 1)
self.assertEqual(response.json['my_libraries'][0]['name'], stub_library_1.name)
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(
Expand Down Expand Up @@ -140,8 +140,8 @@ def helper_bb_classic_user_epic(self, harbour_view):
url,
headers=user_mary.headers
)
self.assertTrue(len(response.json['my_libraries']), 1)
self.assertEqual(response.json['my_libraries'][0]['name'], stub_library_1.name)
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(
Expand Down
8 changes: 4 additions & 4 deletions biblib/tests/functional_tests/test_classic_user_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ def test_classic_user_epic(self):
url,
headers=user_gpa.headers
)
self.assertEqual(response.json['my_libraries'][0]['name'], stub_library_1.name)
self.assertEqual(response.json['my_libraries'][1]['name'], stub_library_2.name)
library_id_1 = response.json['my_libraries'][0]['id']
library_id_2 = response.json['my_libraries'][1]['id']
self.assertEqual(response.json['libraries'][0]['name'], stub_library_1.name)
self.assertEqual(response.json['libraries'][1]['name'], stub_library_2.name)
library_id_1 = response.json['libraries'][0]['id']
library_id_2 = response.json['libraries'][1]['id']

# Gpa clicks the library page and checks that the content is as expected
for library_id, stub_library in [[library_id_1, stub_library_1], [library_id_2, stub_library_2]]:
Expand Down
2 changes: 1 addition & 1 deletion biblib/tests/functional_tests/test_deletion_abuse_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ def test_deletion_abuse_epic(self):
headers=stub_owner.headers
)
self.assertTrue(response.json['libraries_count'] == 0)
self.assertTrue(len(response.json['my_libraries']) == 0)
self.assertTrue(len(response.json['libraries']) == 0)
if __name__ == '__main__':
unittest.main(verbosity=2)
6 changes: 3 additions & 3 deletions biblib/tests/functional_tests/test_deletion_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def test_deletion_epic(self):
url,
headers=stub_user.headers
)
self.assertTrue(len(response.json['my_libraries']) == 2)
library_id_1 = response.json['my_libraries'][0]['id']
library_id_2 = response.json['my_libraries'][1]['id']
self.assertTrue(len(response.json['libraries']) == 2)
library_id_1 = response.json['libraries'][0]['id']
library_id_2 = response.json['libraries'][1]['id']

# Deletes the second library
url = url_for('documentview', library=library_id_2)
Expand Down
2 changes: 1 addition & 1 deletion biblib/tests/functional_tests/test_job_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_job_epic(self):
url,
headers=user_mary.headers
)
library_id = response.json['my_libraries'][0]['id']
library_id = response.json['libraries'][0]['id']

# Then she submits the document (in this case a bibcode) to add to the
# library
Expand Down
8 changes: 4 additions & 4 deletions biblib/tests/functional_tests/test_mistake_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_mistake_epic(self):
headers=user_mary.headers
)
self.assertEqual(name,
response.json['my_libraries'][0]['name'])
response.json['libraries'][0]['name'])

# Mary decides to make the description also more relevant
description = 'something relevant'
Expand All @@ -122,7 +122,7 @@ def test_mistake_epic(self):
headers=user_mary.headers
)
self.assertEqual(description,
response.json['my_libraries'][0]['description'])
response.json['libraries'][0]['description'])

# Mary dislikes both her changes and makes both the changes at once
name = 'Disliked the other one'
Expand All @@ -148,9 +148,9 @@ def test_mistake_epic(self):
headers=user_mary.headers
)
self.assertEqual(name,
response.json['my_libraries'][0]['name'])
response.json['libraries'][0]['name'])
self.assertEqual(description,
response.json['my_libraries'][0]['description'])
response.json['libraries'][0]['description'])


if __name__ == '__main__':
Expand Down
20 changes: 10 additions & 10 deletions biblib/tests/functional_tests/test_returned_data_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def test_returned_data_user_view_epic(self):
url,
headers=user_dave.headers
)
self.assertTrue(len(response.json['my_libraries']) == 1)
library = response.json['my_libraries'][0]
self.assertTrue(len(response.json['libraries']) == 1)
library = response.json['libraries'][0]
self.assertTrue(library['num_documents'] == 0)
self.assertTrue(library['num_users'] == 1)
self.assertTrue(library['permission'] == 'owner')
Expand Down Expand Up @@ -92,9 +92,9 @@ def test_returned_data_user_view_epic(self):
url,
headers=user_dave.headers
)
self.assertTrue(len(response.json['my_libraries'])==1)
self.assertTrue(len(response.json['libraries'])==1)
self.assertEqual(response.status_code, 200)
library = response.json['my_libraries'][0]
library = response.json['libraries'][0]
self.assertTrue(library['num_documents'] == number_of_documents)

# Dave adds mary so that she can see the library and add content
Expand All @@ -115,7 +115,7 @@ def test_returned_data_user_view_epic(self):
url,
headers=user_mary.headers
)
library = response.json['my_libraries'][0]
library = response.json['libraries'][0]

self.assertEqual(response.status_code, 200)
self.assertTrue(library['num_users'] == 2)
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_returned_data_user_view_epic(self):
self.assertEqual(response.status_code, 200)
self.assertTrue(response.json['libraries_count'] == 1)
self.assertTrue(
response.json['my_libraries'][0]['num_documents']
response.json['libraries'][0]['num_documents']
== (number_of_documents+number_of_documents_second)
)

Expand Down Expand Up @@ -179,13 +179,13 @@ def test_returned_data_user_view_epic(self):

self.assertTrue(libraries['libraries_count'] == 1)
self.assertTrue(
libraries['my_libraries'][0]['num_documents'] == number_of_documents+1
libraries['libraries'][0]['num_documents'] == number_of_documents+1
)
self.assertTrue(libraries['my_libraries'][0]['public'])
date_created_2 = datetime.strptime(libraries['my_libraries'][0]['date_created'],
self.assertTrue(libraries['libraries'][0]['public'])
date_created_2 = datetime.strptime(libraries['libraries'][0]['date_created'],
'%Y-%m-%dT%H:%M:%S.%f')
date_last_modified_2 = \
datetime.strptime(libraries['my_libraries'][0]['date_last_modified'],
datetime.strptime(libraries['libraries'][0]['date_last_modified'],
'%Y-%m-%dT%H:%M:%S.%f')
self.assertEqual(date_created, date_created_2)
self.assertNotAlmostEqual(date_created_2,
Expand Down
64 changes: 32 additions & 32 deletions biblib/tests/unit_tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def test_user_can_retrieve_rows_number_of_libraries(self):
absolute_uid=user.absolute_uid, rows=20
)

self.assertEqual(len(libraries['my_libraries']), 20)
self.assertEqual(len(libraries['libraries']), 20)

def test_user_can_retrieve_libraries_from_start(self):
"""
Expand Down Expand Up @@ -418,14 +418,14 @@ def test_user_can_retrieve_libraries_from_start(self):
libraries_full = self.user_view.get_libraries(
service_uid=user.id,
absolute_uid=user.absolute_uid, start=0
)['my_libraries']
)['libraries']

# Get the library created
with MockEmailService(self.stub_user, end_type='uid'):
my_libraries = self.user_view.get_libraries(
service_uid=user.id,
absolute_uid=user.absolute_uid, start=10
)['my_libraries']
)['libraries']

self.assertEqual(len(my_libraries), 90)
self.assertEqual(libraries_full[10:], my_libraries)
Expand Down Expand Up @@ -460,7 +460,7 @@ def test_user_can_retrieve_all_libraries_by_paging(self):
libraries_full = self.user_view.get_libraries(
service_uid=user.id,
absolute_uid=user.absolute_uid, start=0
)['my_libraries']
)['libraries']


# Get the library created
Expand All @@ -473,7 +473,7 @@ def test_user_can_retrieve_all_libraries_by_paging(self):
absolute_uid=user.absolute_uid, start=start*10,
rows=10
)
libraries += curr_libraries['my_libraries']
libraries += curr_libraries['libraries']
total_libraries = curr_libraries['libraries_count']

self.assertEqual(total_libraries, 100)
Expand Down Expand Up @@ -508,7 +508,7 @@ def test_user_can_retrieve_library_when_uid_does_not_exist(self):
absolute_uid=user.absolute_uid
)

self.assertEqual(libraries['my_libraries'][0]['owner'], 'Not available')
self.assertEqual(libraries['libraries'][0]['owner'], 'Not available')

def test_user_retrieves_correct_library_content_from_userview(self):
"""
Expand Down Expand Up @@ -564,25 +564,25 @@ def test_user_retrieves_correct_library_content_from_userview(self):

self.assertTrue(libraries['libraries_count'] == number_of_libs)

for library in libraries['my_libraries']:
for library in libraries['libraries']:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
.format(key))

for j in range(number_of_libs):
i = number_of_libs - j - 1
for key in ['name', 'description', 'public']:
self.assertEqual(libraries['my_libraries'][i][key],
self.assertEqual(libraries['libraries'][i][key],
libs[j].user_view_post_data[key])

self.assertEqual(libraries['my_libraries'][i]['num_documents'], 0)
self.assertEqual(libraries['libraries'][i]['num_documents'], 0)

if libraries['my_libraries'][i]['id'] == _lib['id']:
self.assertEqual(libraries['my_libraries'][i]['num_users'], 2)
if libraries['libraries'][i]['id'] == _lib['id']:
self.assertEqual(libraries['libraries'][i]['num_users'], 2)
else:
self.assertEqual(libraries['my_libraries'][i]['num_users'], 1)
self.assertEqual(libraries['libraries'][i]['num_users'], 1)

self.assertEqual(libraries['my_libraries'][i]['permission'], 'owner')
self.assertEqual(libraries['libraries'][i]['permission'], 'owner')

# Get the library created with a different sort order
with MockEmailService(stub_user_1, end_type='uid'):
Expand All @@ -593,24 +593,24 @@ def test_user_retrieves_correct_library_content_from_userview(self):
)

self.assertTrue(libraries['libraries_count'] == number_of_libs)
for library in libraries['my_libraries']:
for library in libraries['libraries']:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
.format(key))


for i in range(number_of_libs):
for key in ['name', 'description', 'public']:
self.assertEqual(libraries['my_libraries'][i][key],
self.assertEqual(libraries['libraries'][i][key],
libs[i].user_view_post_data[key])
self.assertEqual(libraries['my_libraries'][i]['num_documents'], 0)
self.assertEqual(libraries['libraries'][i]['num_documents'], 0)

if libraries['my_libraries'][i]['id'] == _lib['id']:
self.assertEqual(libraries['my_libraries'][i]['num_users'], 2)
if libraries['libraries'][i]['id'] == _lib['id']:
self.assertEqual(libraries['libraries'][i]['num_users'], 2)
else:
self.assertEqual(libraries['my_libraries'][i]['num_users'], 1)
self.assertEqual(libraries['libraries'][i]['num_users'], 1)

self.assertEqual(libraries['my_libraries'][i]['permission'], 'owner')
self.assertEqual(libraries['libraries'][i]['permission'], 'owner')

# Get the library created with a different sort order and sort column
with MockEmailService(stub_user_1, end_type='uid'):
Expand All @@ -622,25 +622,25 @@ def test_user_retrieves_correct_library_content_from_userview(self):
)

self.assertTrue(libraries['libraries_count'] == number_of_libs)
for library in libraries['my_libraries']:
for library in libraries['libraries']:
for key in self.stub_library.user_view_get_response():
self.assertIn(key, library.keys(), 'Missing key: {0}'
.format(key))


for i in range(number_of_libs):
for key in ['name', 'description', 'public']:
self.assertEqual(libraries['my_libraries'][i][key],
self.assertEqual(libraries['libraries'][i][key],
libs[i].user_view_post_data[key])

self.assertEqual(libraries['my_libraries'][i]['num_documents'], 0)
self.assertEqual(libraries['libraries'][i]['num_documents'], 0)

if libraries['my_libraries'][i]['id'] == _lib['id']:
self.assertEqual(libraries['my_libraries'][i]['num_users'], 2)
if libraries['libraries'][i]['id'] == _lib['id']:
self.assertEqual(libraries['libraries'][i]['num_users'], 2)
else:
self.assertEqual(libraries['my_libraries'][i]['num_users'], 1)
self.assertEqual(libraries['libraries'][i]['num_users'], 1)

self.assertEqual(libraries['my_libraries'][i]['permission'], 'owner')
self.assertEqual(libraries['libraries'][i]['permission'], 'owner')

# Get the library created
with MockEmailService(stub_user_2, end_type='uid'):
Expand Down Expand Up @@ -731,7 +731,7 @@ def test_returned_permissions_are_right(self):
absolute_uid=user_other.absolute_uid
)

self.assertEqual(list(permission.keys())[0], libraries['my_libraries'][0]['permission'])
self.assertEqual(list(permission.keys())[0], libraries['libraries'][0]['permission'])

def test_can_only_see_number_of_people_with_admin_or_owner(self):
"""
Expand Down Expand Up @@ -767,7 +767,7 @@ def test_can_only_see_number_of_people_with_admin_or_owner(self):
libraries = self.user_view.get_libraries(
service_uid=user_admin.id,
absolute_uid=user_admin.absolute_uid
)['my_libraries']
)['libraries']

self.assertTrue(libraries[0]['num_users'] > 0)

Expand All @@ -776,7 +776,7 @@ def test_can_only_see_number_of_people_with_admin_or_owner(self):
libraries = self.user_view.get_libraries(
service_uid=user_owner.id,
absolute_uid=user_owner.absolute_uid
)['my_libraries']
)['libraries']

self.assertTrue(libraries[0]['num_users'] > 0)

Expand Down Expand Up @@ -817,7 +817,7 @@ def test_cannot_see_number_of_people_with_lower_than_admin(self):
libraries = self.user_view.get_libraries(
service_uid=user_read.id,
absolute_uid=user_read.absolute_uid
)['my_libraries']
)['libraries']
self.assertTrue(libraries[0]['num_users'] == 0)
# make sure the owner is correct
self.assertIn(libraries[0]['owner'], self.stub_user_3.email)
Expand All @@ -827,7 +827,7 @@ def test_cannot_see_number_of_people_with_lower_than_admin(self):
libraries = self.user_view.get_libraries(
service_uid=user_write.id,
absolute_uid=user_write.absolute_uid
)['my_libraries']
)['libraries']
self.assertTrue(libraries[0]['num_users'] == 0)
self.assertIn(libraries[0]['owner'], self.stub_user_3.email)

Expand Down
Loading

0 comments on commit d1b2677

Please sign in to comment.