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