Add ICU message format support
[chromium-blink-merge.git] / ios / web / web_state / js / credential_util_unittest.mm
blobdc42abdb3bda851dd676798838992ea8ff33b4fa
1 // Copyright 2015 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.
5 #include "ios/web/web_state/js/credential_util.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "ios/web/public/web_state/credential.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/gtest_mac.h"
14 #include "testing/platform_test.h"
15 #include "url/gurl.h"
17 namespace web {
18 namespace {
20 // "type" value for a DictionaryValue representation of PasswordCredential.
21 const char* kTestCredentialTypePassword = "PasswordCredential";
23 // "type" value for a DictionaryValue representation of FederatedCredential.
24 const char* kTestCredentialTypeFederated = "FederatedCredential";
26 // "id" value for a DictionaryValue representation of a credential.
27 const char* kTestCredentialID = "foo";
29 // "name" value for a DictionaryValue representation of a credential.
30 const char* kTestCredentialName = "Foo Bar";
32 // "avatarURL" value for a DictionaryValue representation of a credential.
33 const char* kTestCredentialAvatarURL = "https://foo.com/bar.jpg";
35 // "password" value for a DictionaryValue representation of a credential.
36 const char* kTestCredentialPassword = "baz";
38 // "federationURL" value for a DictionaryValue representation of a credential.
39 const char* kTestCredentialFederationURL = "https://foo.com/";
41 // Returns a Credential with Password type.
42 Credential GetTestPasswordCredential() {
43   Credential credential;
44   credential.type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
45   credential.id = base::ASCIIToUTF16(kTestCredentialID);
46   credential.name = base::ASCIIToUTF16(kTestCredentialName);
47   credential.avatar_url = GURL(kTestCredentialAvatarURL);
48   credential.password = base::ASCIIToUTF16(kTestCredentialPassword);
49   return credential;
52 // Returns a credential with Federated type.
53 Credential GetTestFederatedCredential() {
54   Credential credential;
55   credential.type = CredentialType::CREDENTIAL_TYPE_FEDERATED;
56   credential.id = base::ASCIIToUTF16(kTestCredentialID);
57   credential.name = base::ASCIIToUTF16(kTestCredentialName);
58   credential.avatar_url = GURL(kTestCredentialAvatarURL);
59   credential.federation_url = GURL(kTestCredentialFederationURL);
60   return credential;
63 // Returns a value representing the credential returned by
64 // |GetTestPasswordCredential()|.
65 scoped_ptr<base::DictionaryValue> GetTestPasswordCredentialDictionaryValue() {
66   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
67   value->SetString("type", kTestCredentialTypePassword);
68   value->SetString("id", kTestCredentialID);
69   value->SetString("name", kTestCredentialName);
70   value->SetString("avatarURL", kTestCredentialAvatarURL);
71   value->SetString("password", kTestCredentialPassword);
72   return value.Pass();
75 // Returns a value representing the credential returned by
76 // |GetTestFederatedCredentialDictionaryValue()|.
77 scoped_ptr<base::DictionaryValue> GetTestFederatedCredentialDictionaryValue() {
78   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
79   value->SetString("type", kTestCredentialTypeFederated);
80   value->SetString("id", kTestCredentialID);
81   value->SetString("name", kTestCredentialName);
82   value->SetString("avatarURL", kTestCredentialAvatarURL);
83   value->SetString("federation", kTestCredentialFederationURL);
84   return value.Pass();
87 // Tests that parsing an empty value fails.
88 TEST(CredentialUtilTest, ParsingEmptyValueFails) {
89   base::DictionaryValue value;
90   Credential credential;
91   EXPECT_FALSE(DictionaryValueToCredential(value, &credential));
94 // Tests that parsing a value with a bad type fails.
95 TEST(CredentialUtilTest, ParsingValueWithBadTypeFails) {
96   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
97   value->SetString("type", "FooCredential");
98   Credential credential;
99   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
102 // Tests that parsing a correctly-formed value representing a PasswordCredential
103 // succeeds.
104 TEST(CredentialUtilTest, ParsingPasswordCredentialSucceeds) {
105   Credential credential;
106   EXPECT_TRUE(DictionaryValueToCredential(
107       *GetTestPasswordCredentialDictionaryValue(), &credential));
108   EXPECT_TRUE(CredentialsEqual(GetTestPasswordCredential(), credential));
111 // Tests that parsing a value representing a PasswordCredential but with no ID
112 // specified fails.
113 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoIDFails) {
114   scoped_ptr<base::DictionaryValue> value(
115       GetTestPasswordCredentialDictionaryValue().Pass());
116   value->RemoveWithoutPathExpansion("id", nullptr);
117   Credential credential;
118   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
121 // Tests that parsing a value representing a PasswordCredential with a badly-
122 // formed avatarURL fails.
123 TEST(CredentialUtilTest, ParsingPasswordCredentialWithBadAvatarURLFails) {
124   scoped_ptr<base::DictionaryValue> value(
125       GetTestPasswordCredentialDictionaryValue().Pass());
126   value->SetString("avatarURL", "foo");
127   Credential credential;
128   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
131 // Tests that parsing a value representing a PasswordCredential with no password
132 // specified fails.
133 TEST(CredentialUtilTest, ParsingPasswordCredentialWithNoPasswordFails) {
134   scoped_ptr<base::DictionaryValue> value(
135       GetTestPasswordCredentialDictionaryValue().Pass());
136   value->Remove("password", nullptr);
137   Credential credential;
138   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
141 // Tests that parsing a correctly-formed value representing a
142 // FederatedCredential succeeds.
143 TEST(CredentialUtilTest, ParsingFederatedCredentialSucceeds) {
144   Credential credential;
145   EXPECT_TRUE(DictionaryValueToCredential(
146       *GetTestFederatedCredentialDictionaryValue(), &credential));
147   EXPECT_TRUE(CredentialsEqual(GetTestFederatedCredential(), credential));
150 // Tests that parsing a value representing a FederatedCredential with no ID
151 // fails.
152 TEST(CredentialUtilTest, ParsingFederatedCredentialWithNoIDFails) {
153   scoped_ptr<base::DictionaryValue> value(
154       GetTestFederatedCredentialDictionaryValue().Pass());
155   value->RemoveWithoutPathExpansion("id", nullptr);
156   Credential credential;
157   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
160 // Tests that parsing a value representing a FederatedCredential with a badly-
161 // formed avatarURL fails.
162 TEST(CredentialUtilTest, ParsingFederatedCredentialWithBadAvatarURLFails) {
163   scoped_ptr<base::DictionaryValue> value(
164       GetTestFederatedCredentialDictionaryValue().Pass());
165   value->SetString("avatarURL", "foo");
166   Credential credential;
167   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
170 // Tests that parsing a value representing a FederatedCredential with no
171 // federation URL fails.
172 TEST(CredentialUtilTest, ParsingFederatedValueWithNoFederationURLFails) {
173   scoped_ptr<base::DictionaryValue> value(
174       GetTestFederatedCredentialDictionaryValue().Pass());
175   value->Remove("federation", nullptr);
176   Credential credential;
177   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
180 // Tests that parsing a value representing a FederatedCredential with a badly-
181 // formed federationURL fails.
182 TEST(CredentialUtilTest, ParsingFederatedValueWithBadFederationURLFails) {
183   scoped_ptr<base::DictionaryValue> value(
184       GetTestFederatedCredentialDictionaryValue().Pass());
185   value->SetString("federation", "bar");
186   Credential credential;
187   EXPECT_FALSE(DictionaryValueToCredential(*value, &credential));
190 // Tests that serializing a FederatedCredential to a DictionaryValue results
191 // in the expected structure.
192 TEST(CredentialUtilTest, SerializeFederatedCredential) {
193   base::DictionaryValue value;
194   Credential credential(GetTestFederatedCredential());
195   CredentialToDictionaryValue(credential, &value);
196   EXPECT_TRUE(GetTestFederatedCredentialDictionaryValue()->Equals(&value));
199 // Tests that serializing a PasswordCredential to a DictionaryValue results in
200 // the
201 // expected structure.
202 TEST(CredentialUtilTest, SerializePasswordCredential) {
203   base::DictionaryValue value;
204   Credential credential(GetTestPasswordCredential());
205   CredentialToDictionaryValue(credential, &value);
206   EXPECT_TRUE(GetTestPasswordCredentialDictionaryValue()->Equals(&value));
209 TEST(CredentialUtilTest, SerializeEmptyCredential) {
210   base::DictionaryValue value;
211   Credential credential;
212   CredentialToDictionaryValue(credential, &value);
213   EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value));
216 TEST(CredentialUtilTest, SerializeEmptyCredentialIntoNonEmptyDictionary) {
217   base::DictionaryValue value;
218   value.SetString("foo", "bar");
219   Credential credential;
220   CredentialToDictionaryValue(credential, &value);
221   EXPECT_TRUE(make_scoped_ptr(new base::DictionaryValue)->Equals(&value));
224 }  // namespace
225 }  // namespace web