Skip to content

Commit

Permalink
drive: Add request class for Permissions.insert API.
Browse files Browse the repository at this point in the history
BUG=348465

Review URL: https://codereview.chromium.org/185723004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@254738 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kinaba@chromium.org committed Mar 4, 2014
1 parent 55fe4e3 commit 00d2f3e
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 0 deletions.
67 changes: 67 additions & 0 deletions google_apis/drive/drive_api_requests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -834,5 +834,72 @@ DownloadFileRequest::DownloadFileRequest(
DownloadFileRequest::~DownloadFileRequest() {
}

//========================== PermissionsInsertRequest ==========================

PermissionsInsertRequest::PermissionsInsertRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const EntryActionCallback& callback)
: EntryActionRequest(sender, callback),
url_generator_(url_generator),
type_(PERMISSION_TYPE_USER),
role_(PERMISSION_ROLE_READER) {
}

PermissionsInsertRequest::~PermissionsInsertRequest() {
}

GURL PermissionsInsertRequest::GetURL() const {
return url_generator_.GetPermissionsInsertUrl(id_);
}

net::URLFetcher::RequestType
PermissionsInsertRequest::GetRequestType() const {
return net::URLFetcher::POST;
}

bool PermissionsInsertRequest::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
*upload_content_type = kContentTypeApplicationJson;

base::DictionaryValue root;
switch (type_) {
case PERMISSION_TYPE_ANYONE:
root.SetString("type", "anyone");
break;
case PERMISSION_TYPE_DOMAIN:
root.SetString("type", "domain");
break;
case PERMISSION_TYPE_GROUP:
root.SetString("type", "group");
break;
case PERMISSION_TYPE_USER:
root.SetString("type", "user");
break;
}
switch (role_) {
case PERMISSION_ROLE_OWNER:
root.SetString("role", "owner");
break;
case PERMISSION_ROLE_READER:
root.SetString("role", "reader");
break;
case PERMISSION_ROLE_WRITER:
root.SetString("role", "writer");
break;
case PERMISSION_ROLE_COMMENTER:
root.SetString("role", "reader");
{
base::ListValue* list = new base::ListValue;
list->AppendString("commenter");
root.Set("additionalRoles", list);
}
break;
}
root.SetString("value", value_);
base::JSONWriter::Write(&root, upload_content);
return true;
}

} // namespace drive
} // namespace google_apis
48 changes: 48 additions & 0 deletions google_apis/drive/drive_api_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,54 @@ class DownloadFileRequest : public DownloadFileRequestBase {
DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest);
};

//========================== PermissionsInsertRequest ==========================

// Enumeration type for specifying type of permissions.
enum PermissionType {
PERMISSION_TYPE_ANYONE,
PERMISSION_TYPE_DOMAIN,
PERMISSION_TYPE_GROUP,
PERMISSION_TYPE_USER,
};

// Enumeration type for specifying the role of permissions.
enum PermissionRole {
PERMISSION_ROLE_OWNER,
PERMISSION_ROLE_READER,
PERMISSION_ROLE_WRITER,
PERMISSION_ROLE_COMMENTER,
};

// This class performs the request for adding permission on a specified file.
class PermissionsInsertRequest : public EntryActionRequest {
public:
// See https://developers.google.com/drive/v2/reference/permissions/insert.
PermissionsInsertRequest(RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const EntryActionCallback& callback);
virtual ~PermissionsInsertRequest();

void set_id(const std::string& id) { id_ = id; }
void set_type(PermissionType type) { type_ = type; }
void set_role(PermissionRole role) { role_ = role; }
void set_value(const std::string& value) { value_ = value; }

// UrlFetchRequestBase overrides.
virtual GURL GetURL() const OVERRIDE;
virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual bool GetContentData(std::string* upload_content_type,
std::string* upload_content) OVERRIDE;

private:
const DriveApiUrlGenerator url_generator_;
std::string id_;
PermissionType type_;
PermissionRole role_;
std::string value_;

DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest);
};

} // namespace drive
} // namespace google_apis

Expand Down
81 changes: 81 additions & 0 deletions google_apis/drive/drive_api_requests_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
Expand Down Expand Up @@ -37,6 +38,13 @@ const char kTestChildrenResponse[] =
"\"childLink\": \"child_link\",\n"
"}\n";

const char kTestPermissionResponse[] =
"{\n"
"\"kind\": \"drive#permission\",\n"
"\"id\": \"resource_id\",\n"
"\"selfLink\": \"self_link\",\n"
"}\n";

const char kTestUploadExistingFilePath[] = "/upload/existingfile/path";
const char kTestUploadNewFilePath[] = "/upload/newfile/path";
const char kTestDownloadPathPrefix[] = "/download/";
Expand Down Expand Up @@ -1771,4 +1779,77 @@ TEST_F(DriveApiRequestsTest, DownloadFileRequest_GetContentCallback) {
EXPECT_EQ(expected_contents, contents);
}

TEST_F(DriveApiRequestsTest, PermissionsInsertRequest) {
expected_content_type_ = "application/json";
expected_content_ = kTestPermissionResponse;

GDataErrorCode error = GDATA_OTHER_ERROR;

// Add comment permission to the user "user@example.com".
{
base::RunLoop run_loop;
drive::PermissionsInsertRequest* request =
new drive::PermissionsInsertRequest(
request_sender_.get(),
*url_generator_,
test_util::CreateQuitCallback(
&run_loop,
test_util::CreateCopyResultCallback(&error)));
request->set_id("resource_id");
request->set_role(drive::PERMISSION_ROLE_COMMENTER);
request->set_type(drive::PERMISSION_TYPE_USER);
request->set_value("user@example.com");
request_sender_->StartRequestWithRetry(request);
run_loop.Run();
}

EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
EXPECT_EQ("/drive/v2/files/resource_id/permissions",
http_request_.relative_url);
EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);

scoped_ptr<base::Value> expected(base::JSONReader::Read(
"{\"additionalRoles\":[\"commenter\"], \"role\":\"reader\", "
"\"type\":\"user\",\"value\":\"user@example.com\"}"));
ASSERT_TRUE(expected);

scoped_ptr<base::Value> result(base::JSONReader::Read(http_request_.content));
EXPECT_TRUE(http_request_.has_content);
EXPECT_TRUE(base::Value::Equals(expected.get(), result.get()));

// Add "can edit" permission to users in "example.com".
error = GDATA_OTHER_ERROR;
{
base::RunLoop run_loop;
drive::PermissionsInsertRequest* request =
new drive::PermissionsInsertRequest(
request_sender_.get(),
*url_generator_,
test_util::CreateQuitCallback(
&run_loop,
test_util::CreateCopyResultCallback(&error)));
request->set_id("resource_id2");
request->set_role(drive::PERMISSION_ROLE_WRITER);
request->set_type(drive::PERMISSION_TYPE_DOMAIN);
request->set_value("example.com");
request_sender_->StartRequestWithRetry(request);
run_loop.Run();
}

EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
EXPECT_EQ("/drive/v2/files/resource_id2/permissions",
http_request_.relative_url);
EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);

expected.reset(base::JSONReader::Read(
"{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}"));
ASSERT_TRUE(expected);

result.reset(base::JSONReader::Read(http_request_.content));
EXPECT_TRUE(http_request_.has_content);
EXPECT_TRUE(base::Value::Equals(expected.get(), result.get()));
}

} // namespace google_apis
8 changes: 8 additions & 0 deletions google_apis/drive/drive_api_url_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const char kDriveV2FileTrashUrlFormat[] = "/drive/v2/files/%s/trash";
const char kDriveV2InitiateUploadNewFileUrl[] = "/upload/drive/v2/files";
const char kDriveV2InitiateUploadExistingFileUrlPrefix[] =
"/upload/drive/v2/files/";
const char kDriveV2PermissionsUrlFormat[] = "/drive/v2/files/%s/permissions";

// apps.delete and file.authorize API is exposed through a special endpoint
// v2internal that is accessible only by the official API key for Chrome.
Expand Down Expand Up @@ -216,4 +217,11 @@ GURL DriveApiUrlGenerator::GenerateDownloadFileUrl(
return base_download_url_.Resolve(net::EscapePath(resource_id));
}

GURL DriveApiUrlGenerator::GetPermissionsInsertUrl(
const std::string& resource_id) const {
return base_url_.Resolve(
base::StringPrintf(kDriveV2PermissionsUrlFormat,
net::EscapePath(resource_id).c_str()));
}

} // namespace google_apis
3 changes: 3 additions & 0 deletions google_apis/drive/drive_api_url_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class DriveApiUrlGenerator {
// Generates a URL for downloading a file.
GURL GenerateDownloadFileUrl(const std::string& resource_id) const;

// Generates a URL for adding permissions.
GURL GetPermissionsInsertUrl(const std::string& resource_id) const;

private:
const GURL base_url_;
const GURL base_download_url_;
Expand Down
7 changes: 7 additions & 0 deletions google_apis/drive/drive_api_url_generator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,11 @@ TEST_F(DriveApiUrlGeneratorTest, GenerateDownloadFileUrl) {
test_url_generator_.GenerateDownloadFileUrl("resourceId").spec());
}

TEST_F(DriveApiUrlGeneratorTest, GeneratePermissionsInsertUrl) {
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/0ADK06pfg/permissions",
url_generator_.GetPermissionsInsertUrl("0ADK06pfg").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/file%3Aabc/permissions",
test_url_generator_.GetPermissionsInsertUrl("file:abc").spec());
}

} // namespace google_apis

0 comments on commit 00d2f3e

Please sign in to comment.