protected override void Initialize(System.Web.Routing.RequestContext requestContext) { _user = (requestContext.HttpContext.Session["User"] != null ? (UserModel)requestContext.HttpContext.Session["User"] : CreateDefaultUserModel() ); ViewData["User"] = _user; base.Initialize(requestContext); }
public void SubscriptionUpdateReceived(UserModel userModel, string update) { XmlDocument doc = new XmlDocument(); doc.LoadXml(update); XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable); nsMgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); var extractor = new XPathValueExtractorService(doc, nsMgr, _loggingService); string feedUpdated = extractor.TryGetValue("//atom:feed/atom:updated/text()"); string entryPublished = extractor.TryGetValue("//atom:feed/atom:entry/atom:published/text()"); string entryId = extractor.TryGetValue("//atom:feed/atom:entry/atom:id/text()"); string entryTitle = extractor.TryGetValue("//atom:feed/atom:entry/atom:title/text()"); string entryContent = extractor.TryGetValue("//atom:feed/atom:entry/atom:content/text()"); entryContent = Regex.Replace(entryContent, @"((<[\s\/]*script\b[^>]*>)([^>]*)(<\/script>))", "", RegexOptions.IgnoreCase | RegexOptions.Multiline); if (entryContent.Length > 25000) { entryContent = entryContent.Substring(0, Config.TrimEntryContentLength); _loggingService.Warn("Trimming message content for \"" + entryTitle + "\" due to large content size"); } string entryAuthors = extractor.TryGetList("//atom:feed/atom:entry/atom:author/atom:name/text()"); string entryLinkAlt = extractor.TryGetValue("//atom:feed/atom:entry/atom:link[@rel='alternate']/@href"); string entryLinkReplies = extractor.TryGetValue("//atom:feed/atom:entry/atom:link[@rel='replies']/@href"); var publishMessage = new PublishMessage(userModel.PushTopic); publishMessage.Values.Add("feedUpdated", feedUpdated); publishMessage.Values.Add("entryPublished", entryPublished); publishMessage.Values.Add("entryId", entryId); publishMessage.Values.Add("entryTitle", entryTitle); publishMessage.Values.Add("entryContent", entryContent); publishMessage.Values.Add("entryAuthors", entryAuthors); publishMessage.Values.Add("entryLinkAlternate", entryLinkAlt); publishMessage.Values.Add("entryLinkReplies", entryLinkReplies); _queueWriter.Write(publishMessage); }
public UserModel GetUser(string username) { lock (_dbAccessLock) { UserModel model = null; User user = _entities.UsersSet.First(u => u.PubSubHubUser == username); if (user != null) { model = new UserModel() { Username = user.PubSubHubUser, PushTopic = user.KwwikaTopic, MaxHubSubscriptions = user.MaxHubSubscriptions }; } return model; } }
public void Login_attempts_a_Subscribe_and_NotAuthorisedResponse_leads_to_ErrorDescription_being_set_Test() { ActionResult actual = null; SubscriptionModel model = new SubscriptionModel(); UserModel userModel = new UserModel() { Username = "******", Password = "******" }; With.Mocks(_mocks).Expecting(delegate { _subscriptionPersistenceService.Stub(x => x.GetUser(userModel.Username)) .Return(userModel); _subscriptionService.Expect(x => x.Subscribe( Arg<IHubConfiguration>.Matches( config => config.HubUsername == userModel.Username && config.HubPassword == userModel.Password), Arg<SubscriptionModel>.Is.Anything)) .Return(new SubscriptionServiceResult() { Type = SubscriptionResponseResultType.NotAuthorised }); }).Verify(delegate { actual = _controller.Login(userModel); }); Assert.IsNotNull(actual); Assert.IsNull(_controller.ViewData["ErrorDescription"]); }
public void Login_checks_user_has_been_added_to_the_database_and_Error_is_set_when_user_does_not_exist_Test() { ActionResult actual = null; SubscriptionModel model = new SubscriptionModel(); UserModel userModel = new UserModel() { Username = "******", Password = "******" }; With.Mocks(_mocks).Expecting(delegate { _subscriptionPersistenceService.Expect(x => x.GetUser(userModel.Username)) .Return(null); }).Verify(delegate { actual = _controller.Login(userModel); }); Assert.IsNotNull(_controller.ViewData.ModelState["_FORM"]); }
public void HubUpdate_for_feed_update_Test() { ActionResult actual = null; int detailsId = 1; string updateContents = "some document contents"; SetupResult.For(_request.InputStream).Return(new MemoryStream(new UTF8Encoding().GetBytes(updateContents))); SubscriptionModel model = new SubscriptionModel() { Verified = true, PubSubHubUser = "******" }; UserModel userModel = new UserModel(); With.Mocks(_mocks).Expecting(delegate { _subscriptionPersistenceService.Expect(x => x.GetSubscriptionById(detailsId)).Return(model); _subscriptionPersistenceService.Expect(x => x.GetSubscriptionCountById(detailsId)).Return(1); _subscriptionPersistenceService.Expect(x => x.GetUser(model.PubSubHubUser)).Return(userModel); _subscriptionListener.Expect(x => x.SubscriptionUpdateReceived(userModel, updateContents)); _subscriptionPersistenceService.Expect(x => x.SaveChanges(model)); }).Verify(delegate { actual = _controller.HubUpdate(detailsId); }); Assert.IsNotNull(actual); Assert.AreEqual((int)HttpStatusCode.OK, _response.StatusCode); }