class DeleteBookTest(TestCase): def setUp(self): super(DeleteBookTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:delete_book', args=[self.book.url_title]) def test_as_not_owner(self): # first login as other user not owner or admin other_user = UserFactory() self.client.login(username=other_user.username, password=PLAIN_USER_PASSWORD) # ---- POST method ----- response = self.client.post(self.dispatcher, dict(title=self.book.title)) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # template must be the delete_error_template, because user doesn't # have enough rights to delete the book self.assertTemplateUsed(response, "reader/book_delete_error.html") def test_as_book_owner(self): # first login as book owner user self.client.login(username=self.user.username, password=PLAIN_USER_PASSWORD) # ---- GET method ---- response = self.client.get(self.dispatcher) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # check if returning the right template self.assertTemplateUsed(response, "reader/book_delete.html") # check if content in response self.assertContains(response, 'Delete Book') # ---- POST method ----- response = self.client.post(self.dispatcher, dict(title=self.book.title)) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # in post method, the template must have changed self.assertTemplateUsed(response, "reader/book_delete_redirect.html") # check if book registry is not in database anymore Book = self.book.__class__ self.assertRaises(Book.DoesNotExist, Book.objects.get, pk=self.book.id)
class DraftChapterTest(TestCase): def setUp(self): super(DraftChapterTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.chapter = ChapterFactory() self.dispatcher = reverse('reader:draft_chapter_page', args=[self.book.url_title]) self.book_toc = BookTocFactory.create_toc(self.book, self.book.version, self.chapter) # list of variables to check in context self.vars_to_check = [ 'content', 'toc_items', 'book_version', 'can_edit' ] def test_context_as_anon(self): response = self.client.get(self.dispatcher) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) def test_can_edit_logged_in(self): # first login as book owner user self.client.login(username=self.user.username, password=PLAIN_USER_PASSWORD) response = self.client.get(self.dispatcher) # as anonymous user you can't edit the book self.assertEquals(response.context['security'].can_edit(), True)
class EditBookInfoTest(TestCase): def setUp(self): super(EditBookInfoTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:edit_info_book', args=[self.book.url_title]) def reload_from_db(self, obj): """ Returns reloaded attributes of a given object from the database """ return obj.__class__.objects.get(pk=obj.id) def test_anon_user(self): response = self.client.get(self.dispatcher) # response status code should be 302, you're not logged in self.assertEquals(response.status_code, status.HTTP_302_FOUND) def test_as_book_owner(self): # first login as book owner user self.client.login( username=self.user.username, password=PLAIN_USER_PASSWORD ) # ---- GET method ---- response = self.client.get(self.dispatcher) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # check if returning the right template self.assertTemplateUsed(response, "reader/book_info_edit.html") # check if book object is in context self.assertTrue('book' in response.context) # check some content in response self.assertContains(response, 'Book description') self.assertContains(response, 'Book image') # ---- POST method ----- new_description = 'lorem ipsum testing' response = self.client.post(self.dispatcher, dict(description=new_description)) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # test if description has been updated correctly updated_book = self.reload_from_db(self.book) self.assertTrue(updated_book.description == new_description) # in post method, the template must have changed self.assertTemplateUsed(response, "reader/book_info_edit_redirect.html")
def setUp(self): super(EditBookInfoTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:edit_info_book', args=[self.book.url_title])
def setUp(self): super(DeleteBookTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:delete_book', args=[self.book.url_title])
def setUp(self): super(BookInfoTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:infopage', args=[self.book.url_title]) # list of variables to check in context self.vars_to_check = [ 'book_collaborators', 'book_admins', 'book_history', 'book_group', 'is_book_admin' ]
def setUp(self): super(LogBookHistoryTestCase, self).setUp() self.book = BookFactory() self.book_version = BookVersionFactory(book=self.book) self.chapter = ChapterFactory(book=self.book) self.user = UserFactory()
class BookInfoTest(TestCase): def setUp(self): super(BookInfoTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:infopage', args=[self.book.url_title]) # list of variables to check in context self.vars_to_check = [ 'book_collaborators', 'book_admins', 'book_history', 'book_group', 'is_book_admin' ] def test_details_as_anonymous(self): response = self.client.get(self.dispatcher) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_details_as_owner_logged_in(self): # first login with book owner user self.client.login( username=self.user.username, password=PLAIN_USER_PASSWORD ) response = self.client.get(self.dispatcher) # check status code self.assertEquals(response.status_code, status.HTTP_200_OK) # check if context has been filled correctly for key in self.vars_to_check: self.assertTrue(key in response.context) # owner is also book admin, so this should be True self.assertEquals(response.context['is_book_admin'], True) # check admin actions available in response self.assertContains(response, 'Edit Book Info') self.assertContains(response, 'Delete Book')
def test_user_security_is_not_bookowner(self): # let's create a book and set it to the role # so we can scope permissions just for that book book = BookFactory(owner=self.superuser) sec = security.get_security_for_book(self.user, book) self.assertFalse(sec.is_book_owner())
def setUp(self): super(DraftChapterTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.chapter = ChapterFactory() self.dispatcher = reverse('reader:draft_chapter_page', args=[self.book.url_title]) self.book_toc = BookTocFactory.create_toc(self.book, self.book.version, self.chapter) # list of variables to check in context self.vars_to_check = [ 'content', 'toc_items', 'book_version', 'can_edit' ]
def setUp(self): super(DeleteBookTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:delete_book', args=[self.book.url_title])
class BookInfoTest(TestCase): def setUp(self): super(BookInfoTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:infopage', args=[self.book.url_title]) # list of variables to check in context self.vars_to_check = [ 'book_collaborators', 'book_admins', 'book_history', 'book_group', 'is_book_admin' ] def test_details_as_anonymous(self): response = self.client.get(self.dispatcher) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_details_as_owner_logged_in(self): # first login with book owner user self.client.login(username=self.user.username, password=PLAIN_USER_PASSWORD) response = self.client.get(self.dispatcher) # check status code self.assertEquals(response.status_code, status.HTTP_200_OK) # check if context has been filled correctly for key in self.vars_to_check: self.assertTrue(key in response.context) # owner is also book admin, so this should be True self.assertEquals(response.context['is_book_admin'], True) # check admin actions available in response self.assertContains(response, 'Edit book info') self.assertContains(response, 'Delete Book')
def setUp(self): super(FrontpageTest, self).setUp() self.dispatcher = reverse('portal:frontpage') self.user = UserFactory() # setup for groups BookGroupFactory(members=(0, 1)) # setup for books BookFactory()
def test_user_security_is_admin(self): # let's create a book and set it to the role # so we can scope permissions just for that book book = BookFactory() self.bookrole.book = book # also put user as member of that role self.bookrole.members.add(self.superuser) self.bookrole.save() sec = security.get_security_for_book(self.superuser, book) self.assertTrue(sec.is_admin())
def test_has_perm_for_book(self): # let's create a book and set it to the role # so we can scope permissions just for that book book = BookFactory() self.bookrole.book = book # also put user as member of that role self.bookrole.members.add(self.user) self.bookrole.save() permission = '%s.%s' % (APP_NAME, CODE_NAME) self.assertTrue(security.has_perm(self.user, permission, book=book), "Member of a book's role should have permissions")
def setUp(self): super(GroupDetailTest, self).setUp() self.user = UserFactory() # setup for books self.book = BookFactory() # setup for groups self.bookGroup = self.book.group # add members self.bookGroup.members.add(1)
class DraftChapterTest(TestCase): def setUp(self): super(DraftChapterTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.chapter = ChapterFactory() self.dispatcher = reverse('reader:draft_chapter_page', args=[self.book.url_title]) self.book_toc = BookTocFactory.create_toc(self.book, self.book.version, self.chapter) # list of variables to check in context self.vars_to_check = [ 'content', 'toc_items', 'book_version', 'can_edit' ] def test_context_as_anon(self): response = self.client.get(self.dispatcher) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) def test_can_edit_logged_in(self): # first login as book owner user self.client.login( username=self.user.username, password=PLAIN_USER_PASSWORD ) response = self.client.get(self.dispatcher) # as anonymous user you can't edit the book self.assertEquals(response.context['security'].can_edit(), True)
def setUp(self): super(DashboardTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory( book=self.book) # TODO: improve this self.book.save() self.user_1 = self.book.owner # need two users to be able to test collaboration within a book self.user_2 = UserFactory() # setup book content chapter_1 = ChapterFactory(book=self.book, version=self.book.version) chapter_2 = ChapterFactory(book=self.book, version=self.book.version) # setup content for user two in same book # call this "Contribution" book_history = BookHistoryFactory(book=self.book, user=self.user_2, chapter=chapter_2) self.dispatcher = reverse('accounts:view_profile', args=[self.user_1.username])
def setUp(self): super(BookInfoTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:infopage', args=[self.book.url_title]) # list of variables to check in context self.vars_to_check = [ 'book_collaborators', 'book_admins', 'book_history', 'book_group', 'is_book_admin' ]
def setUp(self): super(DraftChapterTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.chapter = ChapterFactory() self.dispatcher = reverse('reader:draft_chapter_page', args=[self.book.url_title]) self.book_toc = BookTocFactory.create_toc(self.book, self.book.version, self.chapter) # list of variables to check in context self.vars_to_check = [ 'content', 'toc_items', 'book_version', 'can_edit' ]
def setUp(self): super(DashboardTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) # TODO: improve this self.book.save() self.user_1 = self.book.owner # need two users to be able to test collaboration within a book self.user_2 = UserFactory() # setup book content chapter_1 = ChapterFactory(book=self.book, version=self.book.version) chapter_2 = ChapterFactory(book=self.book, version=self.book.version) # setup content for user two in same book # call this "Contribution" book_history = BookHistoryFactory( book=self.book, user=self.user_2, chapter=chapter_2 ) self.dispatcher = reverse('accounts:view_profile', args=[self.user_1.username])
def books(request): books = [] for title in request.param: books.append( BookFactory(title=title, url_title=booktype_slugify(title))) return books
class DeleteBookTest(TestCase): def setUp(self): super(DeleteBookTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) self.book.save() self.user = self.book.owner self.dispatcher = reverse('reader:delete_book', args=[self.book.url_title]) def test_as_not_owner(self): # first login as other user not owner or admin other_user = UserFactory() self.client.login( username=other_user.username, password=PLAIN_USER_PASSWORD ) # ---- POST method ----- response = self.client.post(self.dispatcher, dict(title=self.book.title)) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # template must be the delete_error_template, because user doesn't # have enough rights to delete the book self.assertTemplateUsed(response, "reader/book_delete_error.html") def test_as_book_owner(self): # first login as book owner user self.client.login( username=self.user.username, password=PLAIN_USER_PASSWORD ) # ---- GET method ---- response = self.client.get(self.dispatcher) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # check if returning the right template self.assertTemplateUsed(response, "reader/book_delete.html") # check if content in response self.assertContains(response, 'Delete Book') # ---- POST method ----- response = self.client.post(self.dispatcher, dict(title=self.book.title)) # response status code should be 200 self.assertEquals(response.status_code, status.HTTP_200_OK) # in post method, the template must have changed self.assertTemplateUsed(response, "reader/book_delete_redirect.html") # check if book registry is not in database anymore Book = self.book.__class__ self.assertRaises(Book.DoesNotExist, Book.objects.get, pk=self.book.id)
class DashboardTest(TestCase): """ Tests Dashboard page as logged in and as anonymous user """ def setUp(self): super(DashboardTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory( book=self.book) # TODO: improve this self.book.save() self.user_1 = self.book.owner # need two users to be able to test collaboration within a book self.user_2 = UserFactory() # setup book content chapter_1 = ChapterFactory(book=self.book, version=self.book.version) chapter_2 = ChapterFactory(book=self.book, version=self.book.version) # setup content for user two in same book # call this "Contribution" book_history = BookHistoryFactory(book=self.book, user=self.user_2, chapter=chapter_2) self.dispatcher = reverse('accounts:view_profile', args=[self.user_1.username]) def _test_base_details(self, response): # context should contain all below variables context_vars = [ 'books', 'books_collaborating', 'licenses', 'groups', 'recent_activity' ] for var in context_vars: self.assertTrue(var in response.context) def test_as_anonymous(self): response = self.client.get(self.dispatcher) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_as_account_owner(self): self.client.login(username=self.user_2.username, password=PLAIN_USER_PASSWORD) own_dispatcher = reverse('accounts:view_profile', args=[self.user_2.username]) response = self.client.get(own_dispatcher) context = response.context # as authenticated user, test basic details self._test_base_details(response) # response should contain next things self.assertContains(response, 'My Dashboard') self.assertContains(response, 'Log out') self.assertContains(response, 'Participating Books') self.assertContains(response, '#createBookModal') self.assertContains(response, '#importBookModal') self.assertContains(response, 'id="user-settings"') # this user is collaborating with other books self.assertTrue(len(context['books_collaborating']) >= 1) self.assertTrue(self.book in context['books_collaborating']) # this user has no groups belonging self.assertTrue(len(context['groups']) == 0) def test_other_user_dashboard(self): self.client.login(username=self.user_2.username, password=PLAIN_USER_PASSWORD) response = self.client.get(self.dispatcher) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN)
class DashboardTest(TestCase): """ Tests Dashboard page as logged in and as anonymous user """ def setUp(self): super(DashboardTest, self).setUp() self.book = BookFactory() self.book.version = BookVersionFactory(book=self.book) # TODO: improve this self.book.save() self.user_1 = self.book.owner # need two users to be able to test collaboration within a book self.user_2 = UserFactory() # setup book content chapter_1 = ChapterFactory(book=self.book, version=self.book.version) chapter_2 = ChapterFactory(book=self.book, version=self.book.version) # setup content for user two in same book # call this "Contribution" book_history = BookHistoryFactory( book=self.book, user=self.user_2, chapter=chapter_2 ) self.dispatcher = reverse('accounts:view_profile', args=[self.user_1.username]) def _test_base_details(self, response): # context should contain all below variables context_vars = [ 'books', 'books_collaborating', 'licenses', 'groups', 'recent_activity' ] for var in context_vars: self.assertTrue(var in response.context) def test_as_anonymous(self): response = self.client.get(self.dispatcher) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN) def test_as_account_owner(self): self.client.login( username=self.user_2.username, password=PLAIN_USER_PASSWORD ) own_dispatcher = reverse('accounts:view_profile', args=[self.user_2.username]) response = self.client.get(own_dispatcher) context = response.context # as authenticated user, test basic details self._test_base_details(response) # response should contain next things self.assertContains(response, 'My Dashboard') self.assertContains(response, 'Log out') self.assertContains(response, 'Participating Books') self.assertContains(response, '#createBookModal') self.assertContains(response, 'id="user-settings"') # this user is collaborating with other books self.assertTrue(len(context['books_collaborating']) >= 1) self.assertTrue(self.book in context['books_collaborating']) # this user has no groups belonging self.assertTrue(len(context['groups']) == 0) def test_other_user_dashboard(self): self.client.login( username=self.user_2.username, password=PLAIN_USER_PASSWORD ) response = self.client.get(self.dispatcher) self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN )