15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A complete set of unit tests for OAuth2MintTokenFlow.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/json/json_reader.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/google_service_auth_error.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/oauth2_mint_token_flow.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_status.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using net::TestURLFetcher;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using net::URLFetcher;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using net::URLRequestStatus;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::_;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::StrictMock;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char kValidTokenResponse[] =
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "{"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"token\": \"at1\","
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    "  \"issueAdvice\": \"Auto\","
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    "  \"expiresIn\": \"3600\""
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}";
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char kTokenResponseNoAccessToken[] =
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "{"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"issueAdvice\": \"Auto\""
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}";
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char kValidIssueAdviceResponse[] =
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "{"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"issueAdvice\": \"consent\","
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"consent\": {"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    \"oauthClient\": {"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"name\": \"Test app\","
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"iconUri\": \"\","
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"developerEmail\": \"munjal@chromium.org\""
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    },"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    \"scopes\": ["
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      {"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"description\": \"Manage your calendars\","
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"detail\": \"\nView and manage your calendars\n\""
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      },"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      {"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"description\": \"Manage your documents\","
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"detail\": \"\nView your documents\nUpload new documents\n\""
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      }"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    ]"
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  }"
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}";
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char kIssueAdviceResponseNoDescription[] =
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "{"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"issueAdvice\": \"consent\","
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"consent\": {"
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    \"oauthClient\": {"
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"name\": \"Test app\","
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"iconUri\": \"\","
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"developerEmail\": \"munjal@chromium.org\""
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    },"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    \"scopes\": ["
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      {"
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"description\": \"Manage your calendars\","
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"detail\": \"\nView and manage your calendars\n\""
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      },"
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      {"
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"detail\": \"\nView your documents\nUpload new documents\n\""
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      }"
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    ]"
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  }"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}";
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char kIssueAdviceResponseNoDetail[] =
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "{"
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"issueAdvice\": \"consent\","
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  \"consent\": {"
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    \"oauthClient\": {"
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"name\": \"Test app\","
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"iconUri\": \"\","
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      \"developerEmail\": \"munjal@chromium.org\""
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    },"
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    \"scopes\": ["
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      {"
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"description\": \"Manage your calendars\","
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"detail\": \"\nView and manage your calendars\n\""
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      },"
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      {"
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "        \"description\": \"Manage your documents\""
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "      }"
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "    ]"
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "  }"
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "}";
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::vector<std::string> CreateTestScopes() {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> scopes;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scopes.push_back("http://scope1");
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scopes.push_back("http://scope2");
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return scopes;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static IssueAdviceInfo CreateIssueAdvice() {
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IssueAdviceInfo ia;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IssueAdviceInfoEntry e1;
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  e1.description = base::ASCIIToUTF16("Manage your calendars");
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  e1.details.push_back(base::ASCIIToUTF16("View and manage your calendars"));
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ia.push_back(e1);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IssueAdviceInfoEntry e2;
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  e2.description = base::ASCIIToUTF16("Manage your documents");
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  e2.details.push_back(base::ASCIIToUTF16("View your documents"));
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  e2.details.push_back(base::ASCIIToUTF16("Upload new documents"));
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ia.push_back(e2);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ia;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockDelegate : public OAuth2MintTokenFlow::Delegate {
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockDelegate() {}
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~MockDelegate() {}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD2(OnMintTokenSuccess, void(const std::string& access_token,
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                        int time_to_live));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MOCK_METHOD1(OnIssueAdviceSuccess,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               void (const IssueAdviceInfo& issue_advice));
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MOCK_METHOD1(OnMintTokenFailure,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               void(const GoogleServiceAuthError& error));
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockMintTokenFlow : public OAuth2MintTokenFlow {
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit MockMintTokenFlow(MockDelegate* delegate,
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const OAuth2MintTokenFlow::Parameters& parameters )
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : OAuth2MintTokenFlow(NULL, delegate, parameters) {}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~MockMintTokenFlow() {}
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher*());
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class OAuth2MintTokenFlowTest : public testing::Test {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OAuth2MintTokenFlowTest() {}
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~OAuth2MintTokenFlowTest() { }
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CreateFlow(OAuth2MintTokenFlow::Mode mode) {
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return CreateFlow(&delegate_, mode);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CreateFlow(MockDelegate* delegate,
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  OAuth2MintTokenFlow::Mode mode) {
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string rt = "refresh_token";
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string ext_id = "ext1";
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string client_id = "client1";
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<std::string> scopes(CreateTestScopes());
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_.reset(new MockMintTokenFlow(
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        delegate,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        OAuth2MintTokenFlow::Parameters(rt, ext_id, client_id, scopes, mode)));
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper to parse the given string to DictionaryValue.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static base::DictionaryValue* ParseJson(const std::string& str) {
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::Value> value(base::JSONReader::Read(str));
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(value.get());
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(base::Value::TYPE_DICTIONARY, value->GetType());
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return static_cast<base::DictionaryValue*>(value.release());
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<MockMintTokenFlow> flow_;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StrictMock<MockDelegate> delegate_;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(OAuth2MintTokenFlowTest, CreateApiCallBody) {
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Issue advice mode.
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string body = flow_->CreateApiCallBody();
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string expected_body(
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          "force=false"
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          "&response_type=none"
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          "&scope=http://scope1+http://scope2"
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          "&client_id=client1"
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          "&origin=ext1");
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_body, body);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Record grant mode.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_RECORD_GRANT);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string body = flow_->CreateApiCallBody();
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string expected_body(
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "force=true"
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&response_type=none"
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&scope=http://scope1+http://scope2"
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&client_id=client1"
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&origin=ext1");
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_body, body);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Mint token no force mode.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string body = flow_->CreateApiCallBody();
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string expected_body(
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "force=false"
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&response_type=token"
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&scope=http://scope1+http://scope2"
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&client_id=client1"
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&origin=ext1");
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_body, body);
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Mint token force mode.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string body = flow_->CreateApiCallBody();
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string expected_body(
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "force=true"
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&response_type=token"
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&scope=http://scope1+http://scope2"
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&client_id=client1"
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "&origin=ext1");
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(expected_body, body);
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(OAuth2MintTokenFlowTest, ParseMintTokenResponse) {
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Access token missing.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> json(
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ParseJson(kTokenResponseNoAccessToken));
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string at;
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int ttl;
236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_FALSE(OAuth2MintTokenFlow::ParseMintTokenResponse(json.get(), &at,
237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                                             &ttl));
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(at.empty());
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // All good.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> json(ParseJson(kValidTokenResponse));
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string at;
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    int ttl;
244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_TRUE(OAuth2MintTokenFlow::ParseMintTokenResponse(json.get(), &at,
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                                            &ttl));
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ("at1", at);
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_EQ(3600, ttl);
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(OAuth2MintTokenFlowTest, ParseIssueAdviceResponse) {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Description missing.
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> json(
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ParseJson(kIssueAdviceResponseNoDescription));
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IssueAdviceInfo ia;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(OAuth2MintTokenFlow::ParseIssueAdviceResponse(
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        json.get(), &ia));
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(ia.empty());
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Detail missing.
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> json(
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ParseJson(kIssueAdviceResponseNoDetail));
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IssueAdviceInfo ia;
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(OAuth2MintTokenFlow::ParseIssueAdviceResponse(
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        json.get(), &ia));
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(ia.empty());
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // All good.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<base::DictionaryValue> json(
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ParseJson(kValidIssueAdviceResponse));
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IssueAdviceInfo ia;
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(OAuth2MintTokenFlow::ParseIssueAdviceResponse(
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        json.get(), &ia));
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IssueAdviceInfo ia_expected(CreateIssueAdvice());
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(ia_expected, ia);
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(OAuth2MintTokenFlowTest, ProcessApiCallSuccess) {
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // No body.
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    url_fetcher.SetResponseString(std::string());
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(_));
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Bad json.
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.SetResponseString("foo");
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(_));
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Valid json: no access token.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(_));
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Valid json: good token response.
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.SetResponseString(kValidTokenResponse);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenSuccess("at1", 3600));
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Valid json: no description.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.SetResponseString(kIssueAdviceResponseNoDescription);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(_));
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Valid json: no detail.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.SetResponseString(kIssueAdviceResponseNoDetail);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(_));
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Valid json: good issue advice response.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.SetResponseString(kValidIssueAdviceResponse);
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IssueAdviceInfo ia(CreateIssueAdvice());
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnIssueAdviceSuccess(ia));
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallSuccess(&url_fetcher);
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(OAuth2MintTokenFlowTest, ProcessApiCallFailure) {
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Null delegate should work fine.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.set_status(URLRequestStatus(URLRequestStatus::FAILED, 101));
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(NULL, OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallFailure(&url_fetcher);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Non-null delegate.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TestURLFetcher url_fetcher(1, GURL("http://www.google.com"), NULL);
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_fetcher.set_status(URLRequestStatus(URLRequestStatus::FAILED, 101));
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(_));
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessApiCallFailure(&url_fetcher);
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(OAuth2MintTokenFlowTest, ProcessMintAccessTokenFailure) {
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Null delegate should work fine.
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GoogleServiceAuthError error(
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        GoogleServiceAuthError::FromConnectionError(101));
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(NULL, OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessMintAccessTokenFailure(error);
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // Non-null delegate.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GoogleServiceAuthError error(
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        GoogleServiceAuthError::FromConnectionError(101));
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_CALL(delegate_, OnMintTokenFailure(error));
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    flow_->ProcessMintAccessTokenFailure(error);
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
365