1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include <vector>
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/json/json_writer.h"
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/values.h"
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "sync/test/accounts_client/test_accounts_client.cc"
10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "sync/test/accounts_client/test_accounts_client.h"
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using std::string;
16a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using std::vector;
17a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using testing::_;
18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using testing::DoAll;
19a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using testing::Return;
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)using testing::SetArgPointee;
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
22a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace {
23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)static const string kServer = "https://test-account-service";
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)static const string kUsername = "foobar@baz.com";
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)static const string kAccountSpace = "test_account_space";
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)static const string kSessionId = "1234-ABCD";
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)static const string kExpirationTime = "12:00";
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} // namespace
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)static AccountSession CreateValidAccountSession() {
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AccountSession session;
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  session.username = kUsername;
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  session.account_space = kAccountSpace;
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  session.session_id = kSessionId;
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  session.expiration_time = kExpirationTime;
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return session;
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)class NoNetworkTestAccountsClient : public TestAccountsClient {
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) public:
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  NoNetworkTestAccountsClient(const string& server,
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                              const string& account_space,
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                              vector<string> usernames)
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      : TestAccountsClient(server, account_space, usernames) {}
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  MOCK_METHOD2(SendRequest, bool(const GURL&, string*));
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)};
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)TEST(TestAccountsClientTest, ClaimAccountError) {
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  vector<string> usernames;
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames);
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_CALL(client, SendRequest(_, _))
52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      .WillOnce(Return(false));
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AccountSession session;
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_FALSE(client.ClaimAccount(&session));
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)TEST(TestAccountsClientTest, ClaimAccountSuccess) {
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  vector<string> usernames;
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  usernames.push_back("foo0@gmail.com");
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  usernames.push_back("foo1@gmail.com");
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  usernames.push_back("foo2@gmail.com");
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames);
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::DictionaryValue success_dict;
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  success_dict.Set("username", new base::StringValue(kUsername));
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  success_dict.Set("account_space", new base::StringValue(kAccountSpace));
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  success_dict.Set("session_id", new base::StringValue(kSessionId));
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  success_dict.Set("expiration_time", new base::StringValue(kExpirationTime));
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  string success_response;
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  base::JSONWriter::Write(&success_dict, &success_response);
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_CALL(client, SendRequest(_, _))
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      .WillOnce(DoAll(SetArgPointee<1>(success_response), Return(true)));
74a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AccountSession session;
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_TRUE(client.ClaimAccount(&session));
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_EQ(kUsername, session.username);
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_EQ(kAccountSpace, session.account_space);
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_EQ(kSessionId, session.session_id);
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_EQ(kExpirationTime, session.expiration_time);
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)TEST(TestAccountsClientTest, ReleaseAccountEmptySession) {
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  vector<string> usernames;
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames);
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AccountSession session;
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // No expectation for SendRequest is made because no network call should be
88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // performed in this scenario.
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  client.ReleaseAccount(session);
90a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)TEST(TestAccountsClientTest, ReleaseAccountSuccess) {
93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  vector<string> usernames;
94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames);
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXPECT_CALL(client, SendRequest(_, _))
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      .WillOnce(Return(true));
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  AccountSession session = CreateValidAccountSession();
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  client.ReleaseAccount(session);
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
100