PageRenderTime 70ms CodeModel.GetById 51ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/source/test/NOS.Registration.Tests/AutoRegistrationPluginSpecs.cs

http://github.com/agross/netopenspace
C# | 363 lines | 296 code | 67 blank | 0 comment | 0 complexity | 11a8a9ded6c714e2d04c7f8481136c79 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4
  5using Machine.Specifications;
  6
  7using Rhino.Mocks;
  8using Rhino.Mocks.Constraints;
  9
 10using ScrewTurn.Wiki.PluginFramework;
 11
 12namespace NOS.Registration.Tests
 13{
 14	public class With_auto_registration_plugin
 15	{
 16		protected static IPluginConfiguration Configuration;
 17		protected static IEntryFormatter EntryFormatter;
 18		protected static IHostV30 Host;
 19		protected static ILogger Logger;
 20		protected static INotificationSender NotificationSender;
 21		protected static IPageFormatter PageFormatter;
 22		protected static IPageRepository PageRepository;
 23		protected static AutoRegistrationPlugin Plugin;
 24		protected static IRegistrationRepository RegistrationRepository;
 25		protected static IFileReader FileReader;
 26		protected static ISettings Settings;
 27
 28		Establish context = () =>
 29			{
 30				Host = MockRepository.GenerateStub<IHostV30>();
 31
 32				ISynchronizer synchronizer = MockRepository.GenerateStub<ISynchronizer>();
 33				synchronizer
 34					.Stub(x => x.Lock(Arg<Action>.Is.Anything))
 35					.WhenCalled(invocation => ((Action) invocation.Arguments.First()).Invoke());
 36
 37				RegistrationRepository = MockRepository.GenerateStub<IRegistrationRepository>();
 38				PageRepository = MockRepository.GenerateStub<IPageRepository>();
 39
 40				Logger = MockRepository.GenerateStub<ILogger>();
 41
 42				EntryFormatter = MockRepository.GenerateStub<IEntryFormatter>();
 43				PageFormatter = MockRepository.GenerateStub<IPageFormatter>();
 44
 45				NotificationSender = MockRepository.GenerateStub<INotificationSender>();
 46				FileReader = MockRepository.GenerateStub<IFileReader>();
 47
 48				Configuration = MockRepository.GenerateStub<IPluginConfiguration>();
 49
 50				Settings = MockRepository.GenerateStub<ISettings>();
 51				Settings
 52					.Stub(x => x.SenderEmail)
 53					.Return("sender@example.com");
 54				Settings
 55					.Stub(x => x.ContactEmail)
 56					.Return("admin@example.com");
 57
 58				Plugin = new AutoRegistrationPlugin(synchronizer,
 59				                                    RegistrationRepository,
 60				                                    PageRepository,
 61				                                    PageFormatter,
 62				                                    EntryFormatter,
 63				                                    NotificationSender,
 64				                                    Logger,
 65				                                    Configuration,
 66													FileReader,
 67													Settings);
 68			};
 69	}
 70
 71	[Subject(typeof(AutoRegistrationPlugin))]
 72	public class When_the_auto_registration_is_initialized_with_invalid_configuration_data : With_auto_registration_plugin
 73	{
 74		Establish context = () =>
 75			{
 76				Configuration.Stub(x => x.Parse(null, null))
 77					.IgnoreArguments()
 78					.Return(new List<string> { "error1", "error2" });
 79			};
 80
 81		Because of = () => Plugin.Init(Host, "invalid");
 82
 83		It should_log_all_configuration_errors =
 84			() => Logger.AssertWasCalled(x => x.Error(Arg<string>.Matches(y => y.StartsWith("error")),
 85			                                          Arg<string>.Is.Equal("SYSTEM")),
 86			                             o => o.Repeat.Twice());
 87
 88		It should_log_that_the_plugin_is_disabled =
 89			() => Logger.AssertWasCalled(x => x.Error("The auto registration plugin will be disabled.", "SYSTEM"));
 90
 91		It should_disable_the_plugin =
 92			() => Host.AssertWasNotCalled(x => x.UserAccountActivity += Arg<EventHandler<UserAccountActivityEventArgs>>
 93			                                                            	.Is.Anything);
 94	}
 95
 96	[Subject(typeof(AutoRegistrationPlugin))]
 97	public class When_the_auto_registration_is_initialized_successfully : With_auto_registration_plugin
 98	{
 99		Establish context = () =>
100			{
101				Configuration
102					.Stub(x => x.Parse(null, null))
103					.IgnoreArguments()
104					.Return(new List<string>());
105
106				Configuration.Stub(x => x.MaximumAttendees).Return(15);
107				Configuration.Stub(x => x.HardLimit).Return(42);
108			};
109
110		Because of = () => Plugin.Init(Host, String.Empty);
111
112		It should_enable_the_plugin =
113			() => Host.AssertWasCalled(x => x.UserAccountActivity += Arg<EventHandler<UserAccountActivityEventArgs>>.Is.NotNull);
114
115		It should_log_the_maximum_attendee_count_and_the_hard_limit =
116			() =>
117			Logger.AssertWasCalled(x => x.Info("Waiting list is enabled after 15 attendees with a hard limit of 42.", "SYSTEM"));
118
119		It should_not_respond_to_formatting_requests_in_phase_1 = () => Plugin.PerformPhase1.ShouldBeFalse();
120		It should_not_respond_to_formatting_requests_in_phase_2 = () => Plugin.PerformPhase2.ShouldBeFalse();
121		It should_not_respond_to_formatting_requests_in_phase_3 = () => Plugin.PerformPhase3.ShouldBeFalse();
122	}
123
124	[Subject(typeof(AutoRegistrationPlugin))]
125	public class When_a_user_account_is_activated : With_auto_registration_plugin
126	{
127		static IPagesStorageProviderV30 Provider;
128		static UserAccountActivityEventArgs EventArgs;
129		protected static UserInfo UserInfo;
130		protected static User User;
131		protected static PageInfo PageInfo;
132
133		Establish context = () =>
134			{
135				UserInfo = new UserInfo("user",
136				                        "Jon Doe",
137				                        "email@example.com",
138				                        true,
139				                        DateTime.Now,
140				                        MockRepository.GenerateStub<IUsersStorageProviderV30>());
141				EventArgs = new UserAccountActivityEventArgs(UserInfo, UserAccountActivity.AccountActivated);
142
143				User = new User("user");
144				RegistrationRepository.Stub(x => x.FindByUserName("user")).Return(User);
145
146				Configuration
147					.Stub(x => x.Parse(null, null))
148					.IgnoreArguments()
149					.Return(new List<string>());
150
151				Configuration.Stub(x => x.Comment).Return("comment");
152				Configuration.Stub(x => x.PageName).Return("page");
153				Configuration.Stub(x => x.EntryTemplate).Return(" and entry");
154
155				PageInfo = new PageInfo(Configuration.PageName, null, DateTime.Now);
156				PageRepository.Stub(x => x.FindPage(Configuration.PageName)).Return(PageInfo);
157
158				Host
159					.Stub(x => x.GetPageContent(PageInfo))
160					.Return(new PageContent(PageInfo,
161					                        "title",
162					                        "user that saved the content last",
163					                        DateTime.Now,
164					                        String.Empty,
165					                        "content",
166					                        new[] { "keyword" },
167					                        "description"));
168				Host
169					.Stub(x => x.SendEmail(null, null, null, null, false))
170					.IgnoreArguments()
171					.Return(true);
172
173				Plugin.Init(Host, String.Empty);
174
175				EntryFormatter
176					.Stub(x => x.FormatUserEntry(User, Settings, Configuration.EntryTemplate))
177					.Return(Configuration.EntryTemplate);
178				PageFormatter
179					.Stub(x => x.AddEntry("content", Configuration.EntryTemplate, User, Configuration))
180					.Return("content and entry");
181			};
182
183		Because of = () => Host.Raise(x => x.UserAccountActivity += null, null, EventArgs);
184
185		It should_try_to_find_the_user_in_the_user_list =
186			() => RegistrationRepository.AssertWasCalled(x => x.FindByUserName(User.UserName));
187
188		It should_try_to_find_the_attendee_page =
189			() => PageRepository.AssertWasCalled(x => x.FindPage(Configuration.PageName));
190
191		It should_load_the_attendee_page_contents =
192			() => Host.AssertWasCalled(x => x.GetPageContent(PageInfo));
193
194		It should_format_the_users_entry =
195			() => EntryFormatter.AssertWasCalled(x => x.FormatUserEntry(User, Settings, Configuration.EntryTemplate));
196
197		It should_add_the_users_entry_to_the_attendee_page =
198			() => PageFormatter.AssertWasCalled(x => x.AddEntry("content", Configuration.EntryTemplate, User, Configuration));
199
200		It should_save_the_modified_page_contents =
201			() => PageRepository.AssertWasCalled(x => x.Save(Arg<PageInfo>.Is.Equal(PageInfo),
202			                                                 Arg<string>.Is.Anything,
203			                                                 Arg<string>.Is.Anything,
204			                                                 Arg<string>.Is.Anything,
205			                                                 Arg<string>.Is.Equal("content and entry")));
206
207		It should_save_the_modified_page_contents_as_the_registered_user =
208			() => PageRepository.AssertWasCalled(x => x.Save(Arg<PageInfo>.Is.Equal(PageInfo),
209			                                                 Arg<string>.Is.Anything,
210			                                                 Arg<string>.Is.Equal("user"),
211			                                                 Arg<string>.Is.Anything,
212			                                                 Arg<string>.Is.Anything));
213
214		It should_save_the_modified_page_contents_without_altering_the_page_title =
215			() => PageRepository.AssertWasCalled(x => x.Save(Arg<PageInfo>.Is.Equal(PageInfo),
216			                                                 Arg<string>.Is.Equal("title"),
217			                                                 Arg<string>.Is.Anything,
218			                                                 Arg<string>.Is.Anything,
219			                                                 Arg<string>.Is.Anything));
220
221		It should_save_the_modified_page_contents_giving_a_comment =
222			() => PageRepository.AssertWasCalled(x => x.Save(Arg<PageInfo>.Is.Equal(PageInfo),
223			                                                 Arg<string>.Is.Anything,
224			                                                 Arg<string>.Is.Anything,
225			                                                 Arg<string>.Is.Equal(Configuration.Comment),
226			                                                 Arg<string>.Is.Anything));
227
228		It should_notify_the_user_about_the_activation =
229			() => NotificationSender.AssertWasCalled(x => x.SendMessage(Arg<string>.Is.Equal(UserInfo.Email),
230			                                                            Arg<string>.Is.Equal(Configuration.Comment),
231																		Arg<string>.Is.Anything));
232		
233		It should_notify_the_administrator_about_the_activation =
234			() => NotificationSender.AssertWasCalled(x => x.SendMessage(Arg<string>.Is.Equal(Settings.ContactEmail),
235			                                                            Arg<string>.Is.Equal(Configuration.Comment),
236																		Arg<string>.Is.Anything));
237	}
238
239	[Subject(typeof(AutoRegistrationPlugin))]
240	public class When_a_user_account_is_activated_and_the_user_cannot_be_found : When_a_user_account_is_activated
241	{
242		Establish context = () =>
243			{
244				RegistrationRepository.BackToRecord();
245				RegistrationRepository.Stub(x => x.FindByUserName("user")).Return(null);
246				RegistrationRepository.Replay();
247			};
248
249		It should_not_add_the_users_entry_to_the_attendee_page =
250			() => PageFormatter.AssertWasNotCalled(x => x.AddEntry(null, null, null, null), o => o.IgnoreArguments());
251	}
252
253	[Behaviors]
254	public class FailureNotificationBehavior
255	{
256		protected static INotificationSender NotificationSender;
257		protected static UserInfo UserInfo;
258
259		It should_notify_the_administrator_about_the_failure =
260			() => NotificationSender.AssertWasCalled(x => x.SendMessage(Arg<string>.Is.Equal("admin@example.com"),
261			                                                            Arg<string>.Is.NotNull,
262																		Arg<string>.Is.Anything));
263
264		It should_notify_the_user_about_the_failure =
265			() => NotificationSender.AssertWasCalled(x => x.SendMessage(Arg<string>.Is.Equal(UserInfo.Email),
266			                                                            Arg<string>.Is.NotNull,
267																		Arg<string>.Is.Anything));
268	}
269
270	[Subject(typeof(AutoRegistrationPlugin))]
271	public class When_a_user_account_is_activated_and_the_attendee_page_does_not_exist
272		: When_a_user_account_is_activated
273	{
274		Establish context = () =>
275			{
276				PageRepository.BackToRecord();
277				PageRepository.Stub(x => x.FindPage(Configuration.PageName)).Return(null);
278				PageRepository.Replay();
279			};
280
281		It should_log_that_the_page_does_not_exist =
282			() => Logger.AssertWasCalled(x => x.Error("The attendee page 'page' does not exist.", "SYSTEM"));
283
284		It should_not_add_the_users_entry_to_the_attendee_page =
285			() => PageFormatter.AssertWasNotCalled(x => x.AddEntry(null, null, null, null), o => o.IgnoreArguments());
286
287		Behaves_like<FailureNotificationBehavior> failing_activation;
288	}
289
290	[Subject(typeof(AutoRegistrationPlugin))]
291	public class When_a_user_account_is_activated_and_the_attendee_page_content_could_not_be_loaded
292		: When_a_user_account_is_activated
293	{
294		Establish context = () =>
295			{
296				Host.BackToRecord(BackToRecordOptions.All ^ BackToRecordOptions.EventSubscribers);
297				Host.Stub(x => x.GetPageContent(PageInfo)).Throw(new Exception());
298				Host.Stub(x => x.SendEmail(null, null, null, null, false)).IgnoreArguments().Return(true);
299				Host.Replay();
300			};
301
302		It should_log_that_page_content_loading_failed =
303			() => Logger.AssertWasCalled(x => x.Error(null, null),
304			                             o =>
305			                             o.Constraints(
306			                             	Text.StartsWith("The attendee page's content ('page') could not be loaded:"),
307			                             	Is.Equal("SYSTEM")));
308
309		It should_not_add_the_users_entry_to_the_attendee_page =
310			() => PageFormatter.AssertWasNotCalled(x => x.AddEntry(null, null, null, null), o => o.IgnoreArguments());
311
312		Behaves_like<FailureNotificationBehavior> failing_activation;
313	}
314
315	[Subject(typeof(AutoRegistrationPlugin))]
316	public class When_a_user_account_is_activated_and_users_entry_cannot_be_added
317		: When_a_user_account_is_activated
318	{
319		Establish context = () =>
320			{
321				PageFormatter.BackToRecord();
322				PageFormatter.Stub(x => x.AddEntry(null, null, null, null)).IgnoreArguments().Throw(new Exception());
323				PageFormatter.Replay();
324			};
325
326		It should_log_that_adding_the_entry_failed =
327			() => Logger.AssertWasCalled(x => x.Error(null, null),
328			                             o =>
329			                             o.Constraints(
330			                             	Text.StartsWith("Could not add the user's entry to the attendee list:"),
331			                             	Is.Equal("SYSTEM")));
332
333		It should_swallow_the_error = () => true.ShouldBeTrue();
334
335		Behaves_like<FailureNotificationBehavior> failing_activation;
336	}
337
338	[Subject(typeof(AutoRegistrationPlugin))]
339	public class When_any_other_user_account_activity_takes_place : With_auto_registration_plugin
340	{
341		static UserAccountActivityEventArgs EventArgs;
342		static UserInfo UserInfo;
343
344		Establish context = () =>
345			{
346				UserInfo = new UserInfo("user",
347				                        "Jon Doe",
348				                        "email@example.com",
349				                        true,
350				                        DateTime.Now,
351				                        MockRepository.GenerateStub<IUsersStorageProviderV30>());
352				EventArgs = new UserAccountActivityEventArgs(UserInfo, UserAccountActivity.AccountAdded);
353			};
354
355		Because of = () => Host.Raise(x => x.UserAccountActivity += null, null, EventArgs);
356
357		It should_not_load_users =
358			() => RegistrationRepository.AssertWasNotCalled(x => x.GetAll());
359
360		It should_not_save_users =
361			() => RegistrationRepository.AssertWasNotCalled(x => x.Save(Arg<User>.Is.Anything));
362	}
363}