Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: third_party/WebKit/Source/modules/credentialmanager/PasswordCredentialTest.cpp

Issue 1828213002: CREDENTIAL: Implement the 'PasswordCredential(HTMLFormElement)' constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: EXPORT2 Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "modules/credentialmanager/PasswordCredential.h"
6
7 #include "bindings/core/v8/ExceptionState.h"
8 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
9 #include "core/dom/ExceptionCode.h"
10 #include "core/frame/FrameView.h"
11 #include "core/html/FormData.h"
12 #include "core/html/HTMLDocument.h"
13 #include "core/html/HTMLFormElement.h"
14 #include "core/html/forms/FormController.h"
15 #include "core/testing/DummyPageHolder.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "wtf/text/StringBuilder.h"
18
19 namespace blink {
20
21 class PasswordCredentialTest : public ::testing::Test {
22 protected:
23 void SetUp() override
24 {
25 m_dummyPageHolder = DummyPageHolder::create();
26 m_document = toHTMLDocument(&m_dummyPageHolder->document());
27 }
28
29 HTMLDocument& document() const { return *m_document; }
30
31 HTMLFormElement* populateForm(const char* html)
32 {
33 StringBuilder b;
34 b.appendLiteral("<!DOCTYPE html><html><body><form id='theForm'>");
35 b.append(html);
36 b.appendLiteral("</form></body></html>");
37 document().documentElement()->setInnerHTML(b.toString(), ASSERT_NO_EXCEP TION);
38 document().view()->updateAllLifecyclePhases();
39 HTMLFormElement* form = toHTMLFormElement(document().getElementById("the Form"));
40 EXPECT_NE(nullptr, form);
41 return form;
42 }
43
44 private:
45 OwnPtr<DummyPageHolder> m_dummyPageHolder;
46 RefPtrWillBePersistent<HTMLDocument> m_document;
47 };
48
49 TEST_F(PasswordCredentialTest, CreateFromForm)
50 {
51 HTMLFormElement* form = populateForm(
52 "<input type='text' name='theId' value='musterman' autocomplete='usernam e'>"
53 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr ent-password'>"
54 "<input type='text' name='theIcon' value='https://example.com/photo' aut ocomplete='photo'>"
55 "<input type='text' name='theExtraField' value='extra'>"
56 "<input type='text' name='theName' value='friendly name' autocomplete='n ame'>");
57 PasswordCredential* credential = PasswordCredential::create(form, ASSERT_NO_ EXCEPTION);
58 ASSERT_NE(nullptr, credential);
59 EXPECT_EQ("theId", credential->idName());
60 EXPECT_EQ("thePassword", credential->passwordName());
61
62 EXPECT_EQ("musterman", credential->id());
63 EXPECT_EQ("sekrit", credential->password());
64 EXPECT_EQ(KURL(ParsedURLString, "https://example.com/photo"), credential->ic onURL());
65 EXPECT_EQ("friendly name", credential->name());
66 EXPECT_EQ("password", credential->type());
67
68 FormDataOrURLSearchParams additionalData;
69 credential->additionalData(additionalData);
70 ASSERT_TRUE(additionalData.isFormData());
71 EXPECT_TRUE(additionalData.getAsFormData()->has("theId"));
72 EXPECT_TRUE(additionalData.getAsFormData()->has("thePassword"));
73 EXPECT_TRUE(additionalData.getAsFormData()->has("theIcon"));
74 EXPECT_TRUE(additionalData.getAsFormData()->has("theName"));
75 EXPECT_TRUE(additionalData.getAsFormData()->has("theExtraField"));
76 }
77
78 TEST_F(PasswordCredentialTest, CreateFromFormNoPassword)
79 {
80 HTMLFormElement* form = populateForm(
81 "<input type='text' name='theId' value='musterman' autocomplete='usernam e'>"
82 "<!-- No password field -->"
83 "<input type='text' name='theIcon' value='https://example.com/photo' aut ocomplete='photo'>"
84 "<input type='text' name='theName' value='friendly name' autocomplete='n ame'>");
85 TrackExceptionState exceptionState;
86 PasswordCredential* credential = PasswordCredential::create(form, exceptionS tate);
87 EXPECT_EQ(nullptr, credential);
88 EXPECT_TRUE(exceptionState.hadException());
89 EXPECT_EQ(V8TypeError, exceptionState.code());
90 EXPECT_EQ("'password' must not be empty.", exceptionState.message());
91 }
92
93 TEST_F(PasswordCredentialTest, CreateFromFormNoId)
94 {
95 HTMLFormElement* form = populateForm(
96 "<!-- No username field. -->"
97 "<input type='text' name='thePassword' value='sekrit' autocomplete='curr ent-password'>"
98 "<input type='text' name='theIcon' value='https://example.com/photo' aut ocomplete='photo'>"
99 "<input type='text' name='theName' value='friendly name' autocomplete='n ame'>");
100 TrackExceptionState exceptionState;
101 PasswordCredential* credential = PasswordCredential::create(form, exceptionS tate);
102 EXPECT_EQ(nullptr, credential);
103 EXPECT_TRUE(exceptionState.hadException());
104 EXPECT_EQ(V8TypeError, exceptionState.code());
105 EXPECT_EQ("'id' must not be empty.", exceptionState.message());
106 }
107
108 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698