Пример #1
0
    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)
Пример #2
0
    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()
Пример #3
0
def file_edit(request, file_id):
    context = {}
    current_user = request.user

    # TODO: Do this consistently across all of the views
    if not current_user:
        return render('login')

    # Save the file changes and get the updated file's information
    if request.method == "POST":
        new_content = request.POST.get('content')
        file_info = FileRepo.publish_new_version(
            current_user.id, file_id, new_content)
        err_cond = "File publish failed"
    # Get the file the user requested for edit
    else:
        file_info = FileRepo.get_file(current_user.id, file_id)
        err_cond = "File does not exist"

    # No file information to present...deal with the error
    if not file_info:
        context['error'] = err_cond
    # File contents for user to proceed with editing
    else:
        context['id'] = file_info['id']
        context['name'] = file_info['name']
        context['content'] = file_info['content_text']

    return render(request, 'app/file_edit.html', context)
Пример #4
0
 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)
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
    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'])
Пример #10
0
    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')
Пример #11
0
def file(request, file_id):
    context = {}
    current_user = request.user
    if current_user:
        file_info = FileRepo.get_file(current_user.id, file_id)
        if not file_info:
            return redirect('index')
        context['id'] = file_info['id']
        context['name'] = file_info['name']
        context['content'] = file_info['content_text']
    return render(request, 'app/file.html', context)
Пример #12
0
    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)
Пример #13
0
    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()
Пример #14
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)
Пример #15
0
def file_ver(request, file_id, version_num):
    context = {}

    current_user = request.user
    if not current_user:
        return render('login')

    file_info = FileRepo.get_file_by_version(
        current_user.id, file_id, version_num)
    if not file_info:
        return redirect('index')

    context['id'] = file_info['id']
    context['name'] = file_info['name']
    context['content'] = file_info['content_text']
    return render(request, 'app/file.html', context)
Пример #16
0
def folder(request, folder_id):
    context = {}
    current_user = request.user
    if current_user:
        folder_info = FolderRepo.get_folder(current_user.id, folder_id)
        child_folders_info = FolderRepo.get_all_folder_for_user_with_parent(
            current_user.id, folder_id)
        files_info = FileRepo.get_files_within_folder(
            current_user.id, folder_id)
        if not folder_info:
            return redirect('index')
        context['id'] = folder_info['id']
        context['name'] = folder_info['name']
        context['parent_id'] = folder_info['parent_id']
        context['files'] = files_info
        context['folders'] = child_folders_info
    return render(request, 'app/folder.html', context)
Пример #17
0
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})
Пример #18
0
 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)
Пример #19
0
 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)