def test_file_create_already_exists_in_folder(self): test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) test_file_one = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello, World') self.assertIsNotNone(test_file_one) test_file_two = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello, World') self.assertIsNone(test_file_two)
def test_file_within_folder(self): test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) test_file = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello, World') test_file_two = FileRepo.create_file(self.user.id, test_folder['id'], 'test2.txt', 'Hello, World Two') res = FileRepo.get_files_within_folder(self.user.id, test_folder['id']) self.assertEqual(len(res), 2) self.assertIn(test_file, res) self.assertIn(test_file_two, res)
def test_file_history_multiple_versions(self): # Login self.client.login(username=self.username, password=self.password) user_id = self.client.session['_auth_user_id'] # Create a folder new_folder = FolderRepo.create_folder(user_id, 'TestFolder') # Place a new file in the above folder new_file = FileRepo.create_file(user_id, new_folder['id'], 'TestFile.txt', 'Hello World') # Publish a new version of the file we just created new_file_ver = FileRepo.publish_new_version(user_id, new_file['id'], "Bye World") # Navigate to the file history page fh_page = self.client.get('/app/file/{}/history/'.format( new_file['id'])) # Validate file history page contents self.assertEqual(fh_page.status_code, 200) self.assertIn(b'History', fh_page.content) self.assertIn( bytes(new_file['creation_time'].strftime("%I:%M%p on %B %d, %Y"), 'utf-8'), fh_page.content) self.assertIn( bytes( new_file_ver['creation_time'].strftime("%I:%M%p on %B %d, %Y"), 'utf-8'), fh_page.content) self.assertIn(b'[Current Version]', fh_page.content) # Logout self.client.logout()
def test_file_grab_previous_version(self): # GIVEN # ====== # Create a new folder test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) # Create a new file test_file = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello World') # WHEN # ==== # Publish multiple new versions of the file f1 = FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World1') f2 = FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World2') f3 = FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World3') # THEN # ==== # Make sure specific versions can be grabbed db_file = FileRepo.get_file_by_version(self.user.id, test_file['id'], f1['version']) self.assertEqual(db_file['content_text'], f1['content_text']) self.assertEqual(db_file['version'], f1['version']) db_file = FileRepo.get_file_by_version(self.user.id, test_file['id'], f2['version']) self.assertEqual(db_file['content_text'], f2['content_text']) self.assertEqual(db_file['version'], f2['version']) db_file = FileRepo.get_file_by_version(self.user.id, test_file['id'], f3['version']) self.assertEqual(db_file['content_text'], f3['content_text']) self.assertEqual(db_file['version'], f3['version'])
def test_file_publish_multiple_new_versions(self): # GIVEN # ====== # Create a new folder test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) # Create a new file test_file = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello World') # WHEN # ==== # Publish multiple new versions of the file FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World1') FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World2') FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World3') # THEN # ==== # Make sure the updated file looks good db_file = FileRepo.get_file(self.user.id, test_file['id']) self.assertEqual(db_file['id'], test_file['id']) self.assertEqual(db_file['name'], test_file['name']) self.assertEqual(db_file['content_text'], 'Bye World3') self.assertEqual(db_file['version'], 3)
def test_file_publish_new_version(self): # GIVEN # ====== # Create a new folder test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) # Create a new file test_file = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello World') # WHEN # ==== # Publish a new version of the file updated_file = FileRepo.publish_new_version(self.user.id, test_file['id'], 'Bye World') # THEN # ==== # Make sure the updated file looks good self.assertEqual(updated_file['id'], test_file['id']) self.assertEqual(updated_file['name'], test_file['name']) self.assertEqual(updated_file['content_text'], 'Bye World') self.assertGreater(updated_file['version'], test_file['version']) # Pull the file and make sure it look database contents look good db_file = FileRepo.get_file(self.user.id, test_file['id']) # File should be the same as updated_file self.assertDictEqual(db_file, updated_file)
def test_delete_file_after_create(self): test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) test_file = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello, World') self.assertIsNotNone(test_file) res = FileRepo.delete_file(self.user.id, test_file['id']) self.assertEqual(res, True)
def test_file_create_already_exists_different_folder(self): test_folder_one = FolderRepo.create_folder(self.user.id, 'folder1') test_folder_two = FolderRepo.create_folder(self.user.id, 'folder2') self.assertIsNotNone(test_folder_one) self.assertIsNotNone(test_folder_two) test_file_one = FileRepo.create_file(self.user.id, test_folder_one['id'], 'test.txt', 'Hello, World') self.assertIsNotNone(test_file_one) test_file_two = FileRepo.create_file(self.user.id, test_folder_two['id'], 'test.txt', 'Hello, World') self.assertIsNotNone(test_file_two) self.assertEqual(test_file_two['name'], 'test.txt') self.assertEqual(test_file_two['deleted'], False) self.assertIn('creation_time', test_file_two) self.assertEqual(test_file_two['belong_id'], test_folder_two['id']) self.assertEqual(test_file_two['content_text'], 'Hello, World')
def test_file_exists(self): self.client.login(username=self.username, password=self.password) user_id = self.client.session['_auth_user_id'] folder = FolderRepo.create_folder(user_id, 'RootFolder') file = FileRepo.create_file(user_id, folder['id'], 'myFile.txt', 'Hello, World') res = self.client.get('/app/file/{}/'.format(file['id'])) self.assertEqual(res.status_code, 200) self.assertIn(bytes(file['name'], 'utf-8'), res.content) self.assertIn(bytes(file['content_text'], 'utf-8'), res.content) self.client.logout()
def test_file_create_success(self): test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) test_file = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello, World') self.assertIsNotNone(test_file) self.assertEqual(test_file['name'], 'test.txt') self.assertEqual(test_file['deleted'], False) self.assertIn('creation_time', test_file) self.assertEqual(test_file['belong_id'], test_folder['id']) self.assertEqual(test_file['content_text'], 'Hello, World') self.assertEqual(test_file['version'], 0)
def test_folder_has_files_in_it(self): self.client.login(username='******', password='******') user_id = self.client.session['_auth_user_id'] f1 = FolderRepo.create_folder(user_id, 'ParentFolder') f2 = FolderRepo.create_folder(user_id, 'ChildFolder') tf = FileRepo.create_file(user_id, f2['id'], 'test.txt', 'Hello, World') res = self.client.get('/app/folder/{}/'.format(f2['id'])) self.assertEqual(res.status_code, 200) self.assertIn(b'<h2>ChildFolder</h2>', res.content) self.assertIn( bytes( '<a href="/app/file/{}/">{}</a>'.format(tf['id'], tf['name']), 'utf-8'), res.content)
def test_file_get_after_create(self): test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) test_file_create = FileRepo.create_file(self.user.id, test_folder['id'], 'test.txt', 'Hello, World') self.assertIsNotNone(test_file_create) # Correct user test_file_get_good = FileRepo.get_file(self.user.id, test_file_create['id']) # Wrong user test_file_get_bad = FileRepo.get_file(self.user_two.id, test_file_create['id']) self.assertDictEqual(test_file_get_good, test_file_create) self.assertIsNone(test_file_get_bad)
def file_create(request): current_user = request.user if not current_user: return render('login') if request.method == 'POST': name = request.POST.get('name') text = request.POST.get('text') parent_id = request.POST.get('parent_id', None) new_file = FileRepo.create_file(current_user.id, parent_id, name, text) if not new_file: response = render(request, 'app/file_create.html', {'error': 'Unable to create new folder'}) response.status_code = 400 else: response = redirect('folder', parent_id) return response parent_id = request.GET.get('parent_id', None) return render(request, 'app/file_create.html', {'parent_id': parent_id})
def test_file_create_different_user_folder(self): test_folder = FolderRepo.create_folder(self.user.id, 'folder1') self.assertIsNotNone(test_folder) test_file = FileRepo.create_file(self.user_two.id, test_folder['id'], 'test.txt', 'Hello, World') self.assertIsNone(test_file)
def test_file_create_no_folder(self): nonexistent_folder_id = 322 test_file = FileRepo.create_file(self.user.id, nonexistent_folder_id, 'test.txt', 'Hello, World') self.assertIsNone(test_file)