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 "sync/test/accounts_client/test_accounts_client.cc" 10 #include "sync/test/accounts_client/test_accounts_client.h" 11 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "url/gurl.h" 14 15 using std::string; 16 using std::vector; 17 using testing::_; 18 using testing::DoAll; 19 using testing::Return; 20 using testing::SetArgPointee; 21 22 namespace { 23 static const string kServer = "https://test-account-service"; 24 static const string kUsername = "foobar (at) baz.com"; 25 static const string kAccountSpace = "test_account_space"; 26 static const string kSessionId = "1234-ABCD"; 27 static const string kExpirationTime = "12:00"; 28 } // namespace 29 30 static 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 39 class 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 48 TEST(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 57 TEST(TestAccountsClientTest, ClaimAccountSuccess) { 58 vector<string> usernames; 59 usernames.push_back("foo0 (at) gmail.com"); 60 usernames.push_back("foo1 (at) gmail.com"); 61 usernames.push_back("foo2 (at) gmail.com"); 62 NoNetworkTestAccountsClient client(kServer, kAccountSpace, usernames); 63 64 base::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 83 TEST(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 92 TEST(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