Exemple #1
0
 def test_migrate_ancillary_data_to_legacy_user_when_multiple_users(self):
     factory.make_FileStorage(owner=None)
     # Create two users, both with API credentials, an SSH key and a node.
     user1 = factory.make_User()
     consumer1, token1 = user1.userprofile.create_authorisation_token()
     key1 = factory.make_SSHKey(user1, get_ssh_key_string(1))
     node1 = factory.make_Node(owner=user1)
     user2 = factory.make_User()
     consumer2, token2 = user2.userprofile.create_authorisation_token()
     key2 = factory.make_SSHKey(user2, get_ssh_key_string(2))
     node2 = factory.make_Node(owner=user2)
     migrate()
     # The SSH keys have been copied to the legacy user.
     legacy_user = get_legacy_user()
     legacy_users_ssh_keys = get_ssh_keys(legacy_user)
     self.assertSetEqual({key1.key, key2.key}, set(legacy_users_ssh_keys))
     # The API credentials have been moved to the legacy user.
     legacy_users_nodes = Node.objects.filter(owner=legacy_user)
     self.assertSetEqual({node1, node2}, set(legacy_users_nodes))
     self.assertEqual(
         (legacy_user, legacy_user, legacy_user, legacy_user),
         (
             reload_object(consumer1).user,
             reload_object(token1).user,
             reload_object(consumer2).user,
             reload_object(token2).user,
         ),
     )
Exemple #2
0
 def test_list_files_does_not_include_file_content(self):
     factory.make_FileStorage(filename="filename",
                              content=b"test content",
                              owner=self.user)
     response = self.make_API_GET_request()
     parsed_results = json_load_bytes(response.content)
     self.assertNotIn('content', parsed_results[0])
Exemple #3
0
 def test_storage_can_be_retrieved(self):
     filename = factory.make_string()
     content = self.make_data()
     factory.make_FileStorage(filename=filename, content=content)
     storage = FileStorage.objects.get(filename=filename)
     self.assertEqual((filename, content),
                      (storage.filename, storage.content))
Exemple #4
0
    def test_get_fetches_the_most_recent_file(self):
        filename = factory.make_name('file')
        factory.make_FileStorage(filename=filename, owner=self.user)
        storage = factory.make_FileStorage(filename=filename, owner=self.user)
        response = self.make_API_GET_request("get", filename)

        self.assertEqual(http.client.OK, response.status_code)
        self.assertEqual(storage.content, response.content)
Exemple #5
0
    def test_get_file_checks_owner(self):
        filename = factory.make_name('file')
        factory.make_FileStorage(filename=filename,
                                 content=b"give me rope",
                                 owner=factory.make_User())
        response = self.make_API_GET_request("get", filename)

        self.assertEqual(http.client.NOT_FOUND, response.status_code)
Exemple #6
0
 def test_get_unowned_files(self):
     user = factory.make_User()
     files = [
         factory.make_FileStorage(owner=None),
         factory.make_FileStorage(owner=user),
         factory.make_FileStorage(owner=None),
     ]
     self.assertSetEqual({files[0], files[2]}, set(get_unowned_files()))
Exemple #7
0
    def test_get_file_succeeds(self):
        filename = factory.make_name('file')
        factory.make_FileStorage(filename=filename,
                                 content=b"give me rope",
                                 owner=self.user)
        response = self.make_API_GET_request("get", filename)

        self.assertEqual(http.client.OK, response.status_code)
        self.assertEqual(b"give me rope", response.content)
Exemple #8
0
 def test_migrate_all_nodes_to_new_legacy_user_when_multiple_users(self):
     factory.make_FileStorage(owner=None)
     user1 = factory.make_User()
     node1 = factory.make_Node(owner=user1)
     user2 = factory.make_User()
     node2 = factory.make_Node(owner=user2)
     migrate()
     self.assertNotIn(reload_object(node1).owner, {user1, user2, None})
     self.assertNotIn(reload_object(node2).owner, {user1, user2, None})
Exemple #9
0
 def test_delete_file_deletes_file(self):
     filename = factory.make_name('file')
     factory.make_FileStorage(filename=filename,
                              content=b"test content",
                              owner=self.user)
     response = self.client.delete(reverse('file_handler', args=[filename]))
     self.assertEqual(http.client.NO_CONTENT, response.status_code)
     files = FileStorage.objects.filter(filename=filename)
     self.assertEqual([], list(files))
Exemple #10
0
 def test_files_resource_uri_supports_slashes_in_filenames(self):
     filename = "a/filename/with/slashes/in/it/"
     factory.make_FileStorage(filename=filename,
                              content=b"test content",
                              owner=self.user)
     response = self.make_API_GET_request()
     parsed_results = json_load_bytes(response.content)
     resource_uri = parsed_results[0]['resource_uri']
     expected_uri = reverse('file_handler', args=[filename])
     self.assertEqual(expected_uri, resource_uri)
Exemple #11
0
 def test_get_file_returns_file_object_with_resource_uri(self):
     filename = factory.make_name("file")
     content = sample_binary_data
     factory.make_FileStorage(filename=filename,
                              content=content,
                              owner=self.user)
     response = self.client.get(reverse('file_handler', args=[filename]))
     parsed_result = json_load_bytes(response.content)
     self.assertEqual(reverse('file_handler', args=[filename]),
                      parsed_result['resource_uri'])
Exemple #12
0
 def test_list_files_returns_ordered_list(self):
     filenames = ["myfiles/a", "myfiles/z", "myfiles/b"]
     for filename in filenames:
         factory.make_FileStorage(filename=filename,
                                  content=b"test content",
                                  owner=self.user)
     response = self.make_API_GET_request()
     self.assertEqual(http.client.OK, response.status_code)
     parsed_results = json_load_bytes(response.content)
     filenames = [result['filename'] for result in parsed_results]
     self.assertEqual(sorted(filenames), filenames)
Exemple #13
0
 def test_get_file_returns_file_object_with_content_base64_encoded(self):
     filename = factory.make_name("file")
     content = sample_binary_data
     factory.make_FileStorage(filename=filename,
                              content=content,
                              owner=self.user)
     response = self.client.get(reverse('file_handler', args=[filename]))
     parsed_result = json_load_bytes(response.content)
     self.assertEqual(
         (filename, content),
         (parsed_result['filename'], b64decode(parsed_result['content'])))
Exemple #14
0
    def test_delete_on_files(self):
        filename = factory.make_name("file")
        factory.make_FileStorage(filename=filename,
                                 content=b"test content",
                                 owner=self.user)
        response = self.client.delete(reverse("files_handler"),
                                      query={"filename": filename})

        self.assertEqual(http.client.NO_CONTENT, response.status_code)
        files = FileStorage.objects.filter(filename=filename)
        self.assertEqual([], list(files))
Exemple #15
0
 def test_overwrites_file(self):
     # If a file of the same name has already been stored, the
     # reference to the old data gets overwritten with one to the new
     # data.
     filename = factory.make_name("filename")
     old_storage = factory.make_FileStorage(
         filename=filename, content=self.make_data("old data"))
     new_data = self.make_data("new-data")
     new_storage = factory.make_FileStorage(filename=filename,
                                            content=new_data)
     self.assertEqual(old_storage.filename, new_storage.filename)
     self.assertEqual(new_data,
                      FileStorage.objects.get(filename=filename).content)
Exemple #16
0
 def test_list_files_lists_files_with_prefix(self):
     filenames_with_prefix = ["prefix-file1", "prefix-file2"]
     filenames = filenames_with_prefix + ["otherfile", "otherfile2"]
     for filename in filenames:
         factory.make_FileStorage(filename=filename,
                                  content=b"test content",
                                  owner=self.user)
     response = self.client.get(reverse('files_handler'),
                                {"prefix": "prefix-"})
     self.assertEqual(http.client.OK, response.status_code)
     parsed_results = json_load_bytes(response.content)
     filenames = [result['filename'] for result in parsed_results]
     self.assertItemsEqual(filenames_with_prefix, filenames)
Exemple #17
0
 def test_get_file_returns_owned_file(self):
     # If both an owned file and a non-owned file are present (with the
     # same name), the owned file is returned.
     filename = factory.make_name("file")
     factory.make_FileStorage(filename=filename, owner=None)
     content = sample_binary_data
     storage = factory.make_FileStorage(filename=filename,
                                        content=content,
                                        owner=self.user)
     response = self.client.get(reverse('file_handler', args=[filename]))
     parsed_result = json_load_bytes(response.content)
     self.assertEqual(
         (filename, storage.anon_resource_uri, content),
         (parsed_result['filename'], parsed_result['anon_resource_uri'],
          b64decode(parsed_result['content'])))
Exemple #18
0
 def test_migrate_all_files_to_new_legacy_user_when_multiple_users(self):
     stored = factory.make_FileStorage(owner=None)
     user1 = factory.make_User()
     user2 = factory.make_User()
     migrate()
     stored = reload_object(stored)
     self.assertNotIn(stored.owner, {user1, user2, None})
Exemple #19
0
 def test_delete_filters_by_owner(self):
     storage = factory.make_FileStorage(owner=factory.make_User())
     response = self.client.delete(
         reverse('file_handler', args=[storage.filename]))
     self.assertEqual(http.client.NOT_FOUND, response.status_code)
     files = FileStorage.objects.filter(filename=storage.filename)
     self.assertEqual([storage], list(files))
Exemple #20
0
    def test_get_by_key_works_anonymously(self):
        storage = factory.make_FileStorage()
        response = self.client.get(
            reverse("files_handler"), {"key": storage.key, "op": "get_by_key"}
        )

        self.assertEqual(http.client.OK, response.status_code)
        self.assertEqual(storage.content, response.content)
Exemple #21
0
    def test_get_by_key_works_anonymously(self):
        storage = factory.make_FileStorage()
        response = self.client.get(reverse('files_handler'), {
            'key': storage.key,
            'op': 'get_by_key'
        })

        self.assertEqual(http.client.OK, response.status_code)
        self.assertEqual(storage.content, response.content)
Exemple #22
0
def make_provider_state_file(node=None):
    """Create a 'provider-state' file with a reference (zookeeper-instances)
    to a node.
    """
    if node is None:
        node = factory.make_Node()
    node_link = reverse('node_handler', args=[node.system_id])
    content = 'zookeeper-instances: [%s]\n' % node_link
    content_data = content.encode('ascii')
    return factory.make_FileStorage(
        filename=PROVIDER_STATE_FILENAME, content=content_data, owner=None)
Exemple #23
0
 def test_delete_deletes_related_filestorage_objects(self):
     # Deleting a profile deletes the related filestorage objects.
     profile = factory.make_User().userprofile
     profile_id = profile.id
     filestorage = factory.make_FileStorage(owner=profile.user)
     filestorage_id = filestorage.id
     self.assertTrue(FileStorage.objects.filter(id=filestorage_id).exists())
     self.assertTrue(UserProfile.objects.filter(id=profile_id).exists())
     profile.delete()
     self.assertFalse(
         FileStorage.objects.filter(id=filestorage_id).exists())
     self.assertFalse(UserProfile.objects.filter(id=profile_id).exists())
Exemple #24
0
 def test_returns_None_if_invalid_yaml(self):
     invalid_content = '%'.encode('ascii')
     factory.make_FileStorage(
         filename=PROVIDER_STATE_FILENAME, content=invalid_content)
     self.assertIsNone(get_bootstrap_node_owner())
Exemple #25
0
 def test_anon_cannot_get_file(self):
     storage = factory.make_FileStorage()
     response = self.client.get(
         reverse('file_handler', args=[storage.filename]))
     self.assertEqual(http.client.UNAUTHORIZED, response.status_code)
Exemple #26
0
 def test_anon_cannot_list_files(self):
     factory.make_FileStorage()
     response = self.make_API_GET_request("list")
     # The 'list' operation is not available to anon users.
     self.assertEqual(http.client.BAD_REQUEST, response.status_code)
Exemple #27
0
 def test_anon_resource_uri_allows_anonymous_access(self):
     storage = factory.make_FileStorage()
     response = self.client.get(storage.anon_resource_uri)
     self.assertEqual(http.client.OK, response.status_code)
     self.assertEqual(storage.content, response.content)
Exemple #28
0
 def test_get_does_not_work_anonymously(self):
     storage = factory.make_FileStorage()
     response = self.make_API_GET_request("get", storage.filename)
     self.assertEqual(http.client.BAD_REQUEST, response.status_code)
Exemple #29
0
 def test_key_includes_random_part(self):
     storage1 = factory.make_FileStorage()
     storage2 = factory.make_FileStorage()
     self.assertNotEqual(storage1.key, storage2.key)
Exemple #30
0
 def test_list_files_filters_by_owner(self):
     factory.make_FileStorage(owner=factory.make_User())
     response = self.make_API_GET_request()
     self.assertEqual(http.client.OK, response.status_code)
     parsed_results = json_load_bytes(response.content)
     self.assertEqual([], parsed_results)