1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.ActiveRecordSupport
.Tests
.ScaffoldingTests
18 using Castle
.ActiveRecord
;
19 using Castle
.ActiveRecord
.Framework
;
20 using NUnit
.Framework
;
21 using TestSiteARSupport
.Model
;
23 using Attribute
=WatiN
.Core
.Attribute
;
26 public class UserScaffoldTestCase
: BaseAcceptanceTestCase
28 private Account account1
, account2
, account3
;
31 protected override void CreateTestData()
33 account1
= new Account("AdWords", "email1@gmail.net", "abc123");
34 account2
= new Account("AdSense", "email1@gmail.net", "abc123");
35 account3
= new Account("Orkut", "email1@gmail.net", "abc123");
37 account1
.Password
= account2
.Password
= account3
.Password
= "123abcd";
38 account1
.ConfirmationPassword
= account2
.ConfirmationPassword
= account3
.ConfirmationPassword
= "123abcd";
46 public void CreateUser()
48 ie
.GoTo("http://localhost:88/UserScaffold/new.castle");
50 ie
.Button(new Value("Create")).Click(); // Trying to save will spark the validation
52 Assert
.AreEqual("http://localhost:88/UserScaffold/new.castle", ie
.Url
);
54 Assert
.IsTrue(ie
.Elements
.Exists("advice-required-User_Name"));
55 Assert
.AreEqual("This is a required field.", ie
.Element("advice-required-User_Name").InnerHtml
);
57 ie
.TextField("User_Name").TypeText("John Doe");
58 ie
.SelectList("User_Account_Id").SelectByValue(account1
.Id
.ToString());
60 ie
.Button(new Value("Create")).Click(); // Now it should save
62 Assert
.AreEqual("http://localhost:88/UserScaffold/list.castle", ie
.Url
);
64 ElementCollection elements
= ie
.Elements
.Filter(new Attribute("className", "idRow"));
66 Assert
.IsTrue(elements
.Length
> 0, "Newly added object not present on the list?");
68 int id
= Convert
.ToInt32(elements
[elements
.Length
- 1].InnerHtml
);
70 user
= ActiveRecordMediator
<User
>.FindByPrimaryKey(id
);
72 Assert
.AreEqual("John Doe", user
.Name
);
73 Assert
.IsNotNull(user
.Account
);
74 Assert
.AreEqual(account1
.Id
, user
.Account
.Id
);
78 public void EditUser()
82 ie
.GoTo("http://localhost:88/UserScaffold/edit.castle?id=" + user
.Id
);
84 ie
.TextField("User_Name").TypeText("");
85 ie
.Button(new Value("Save Changes")).Click(); // Trying to save will spark the validation
87 Assert
.AreEqual("http://localhost:88/UserScaffold/edit.castle?id=" + user
.Id
, ie
.Url
);
89 Assert
.IsTrue(ie
.Elements
.Exists("advice-required-User_Name"));
90 Assert
.AreEqual("This is a required field.", ie
.Element("advice-required-User_Name").InnerHtml
);
92 ie
.TextField("User_Name").TypeText("Mary Jane");
93 ie
.SelectList("User_Account_Id").SelectByValue(account2
.Id
.ToString());
95 ie
.Button(new Value("Save Changes")).Click(); // Now it should save
97 Assert
.AreEqual("http://localhost:88/UserScaffold/list.castle", ie
.Url
);
99 ElementCollection elements
= ie
.Elements
.Filter(new Attribute("className", "idRow"));
101 Assert
.IsTrue(elements
.Length
> 0, "Changed object not present on the list?");
103 int id
= Convert
.ToInt32(elements
[elements
.Length
- 1].InnerHtml
);
105 user
= ActiveRecordMediator
<User
>.FindByPrimaryKey(id
);
107 Assert
.AreEqual("Mary Jane", user
.Name
);
108 Assert
.IsNotNull(user
.Account
);
109 Assert
.AreEqual(account2
.Id
, user
.Account
.Id
);
113 public void ConfirmAndDeleteNewlyCreatedUser()
117 Assert
.AreEqual("http://localhost:88/UserScaffold/list.castle", ie
.Url
);
119 ElementCollection elements
= ie
.Elements
.Filter(new Attribute("className", "deletelink"));
123 foreach(Link link
in elements
)
125 if (link
.Url
.EndsWith("confirm.castle?id=" + user
.Id
))
132 Assert
.IsNotNull(delLink
);
136 ie
.Button(new Value("Yes")).Click();
140 ActiveRecordMediator
<User
>.Refresh(user
);
142 Assert
.Fail("Expecting exception as the user was removed");
144 catch(ActiveRecordException
)