def test_archive_creation(self): """Test archive creation for solutions.""" for solution in [self.passed_solution, self.failed_solution]: self.assertFalse(solution.archive) create_archive(solution) self.assertTrue(solution.archive) self.assertTrue(is_zipfile(solution.archive.path)) with ZipFile(solution.archive.path) as zipfile: self.assertIn('Example1.java', zipfile.namelist()) self.assertIn('Example2.java', zipfile.namelist()) with zipfile.open('Example1.java') as stream: self.assertEqual(stream.read(), JAVA_EXAMPLE_1) with zipfile.open('Example2.java') as stream: self.assertEqual(stream.read(), JAVA_EXAMPLE_2) with TemporaryDirectory() as tmpdir: # Test extraction to expose erroneous packaging zipfile.extractall(tmpdir) for _, dirs, files in os.walk(tmpdir): self.assertIn('Example1.java', files) self.assertIn('Example2.java', files) self.assertEqual(2, len(files)) # The inherited folder structure should be flat self.assertFalse(dirs) self.assertIsNone(zipfile.testzip())
def test_archive_download(self): """Test archive download in solution detail view.""" create_archive(self.solution) response = self.client.get(reverse( 'solutions:archive_download', kwargs={'solution_id': self.solution.id}), follow=True) self.assertEqual(response.status_code, 200)
def test_archive_post_delete(self): """ Validate that solution archives are deleted correctly after the corresponding solution was deleted. """ create_archive(self.solution) archive_path = self.solution.archive.path self.assertTrue(os.path.isfile(archive_path)) self.solution.delete() self.assertFalse(os.path.isfile(archive_path))
def test_archive_download(self): """Test archive download in solution detail view.""" self.assertTrue(self.client.login(username="******", password="******")) create_archive(self.solution) # Download archive response = self.client.get(reverse( "solutions:archive_download", kwargs={"solution_id": self.solution.id}), follow=True) self.assertEqual(response.status_code, 200)
def test_archive_access(self): """Verify that a user cannot download other users' solution archives.""" self.assertTrue(self.client.login(username="******", password="******")) create_archive(self.solution) # Because the solution was created by bob, alice # should not be able to access his archive response = self.client.get(reverse( "solutions:archive_download", kwargs={"solution_id": self.solution.id}), follow=True) self.assertEqual(response.status_code, 404)
def test_archive_download_access(self): """Test the access privileges to the archive download.""" create_archive(self.solution) for user, expected_status_code in [ (self.bob, 200), (self.arnold, 200), (self.alice, 404), ]: self.client.force_login(user) response = self.client.get( reverse('solutions:archive_download', kwargs={'solution_id': self.solution.id})) self.assertEqual(response.status_code, expected_status_code)
def test_archive_status(self): """Test the archive status endpoint.""" self.assertTrue(self.client.login(username='******', password='******')) response = self.client.get( reverse('solutions:archive_status', kwargs={'solution_id': self.solution.id})) self.assertEqual(response.status_code, 200) self.assertContains(response, 'unavailable') create_archive(self.solution) response = self.client.get( reverse('solutions:archive_status', kwargs={'solution_id': self.solution.id})) self.assertEqual(response.status_code, 200) self.assertContains(response, 'available')