public void GlobalSetup()
        {
            var config  = new XDoc("config");
            var builder = new ContainerBuilder();

            _smtpClientFactory = new SmtpClientFactoryMock();
            builder.Register(c => _smtpClientFactory).As <ISmtpClientFactory>().ServiceScoped();
            _hostInfo     = DreamTestHelper.CreateRandomPortHost(config, builder.Build());
            _emailService = DreamTestHelper.CreateService(
                _hostInfo,
                "sid://mindtouch.com/2009/01/dream/email",
                "email",
                new XDoc("config")
                .Elem("apikey", "servicekey")
                .Elem("smtp-host", DEFAULT_HOST)
                );
            _plug = _emailService.WithInternalKey().AtLocalHost;
        }
Exemple #2
0
 public void GlobalSetup()
 {
     _hostInfo     = DreamTestHelper.CreateRandomPortHost();
     _queueService = DreamTestHelper.CreateService(_hostInfo, "sid://mindtouch.com/2009/12/dream/queue", "queue", new XDoc("config").Elem("folder", Path.GetTempPath()));
     _plug         = _queueService.WithInternalKey().AtLocalHost;
 }
        public void Authtoken_recipients_always_get_what_they_want()
        {
            XUri mockDeki   = new XUri("http://mock/deki");
            int  dekiCalled = 0;

            MockPlug.Register(mockDeki, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                _log.DebugFormat("deki called at {0}", u);
                dekiCalled++;
                r2.Return(DreamMessage.Ok());
            });
            XUri mockAuthorized       = new XUri("http://mock/authorized");
            int  authorizedCalled     = 0;
            XDoc received             = null;
            var  authorizedResetEvent = new ManualResetEvent(false);

            MockPlug.Register(mockAuthorized, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                authorizedCalled++;
                received = r.ToDocument();
                authorizedResetEvent.Set();
                r2.Return(DreamMessage.Ok());
            });
            XUri mockNotAuthorized   = new XUri("http://mock/notauthorized");
            int  notAuthorizedCalled = 0;

            MockPlug.Register(mockNotAuthorized, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                notAuthorizedCalled++;
                r2.Return(DreamMessage.Ok());
            });
            DreamServiceInfo serviceInfo = DreamTestHelper.CreateService(
                _hostInfo,
                "sid://mindtouch.com/dream/2008/10/pubsub",
                "authorization",
                new XDoc("config")
                .Elem("authtoken", "abc")
                .Elem("uri.deki", mockDeki)
                .Start("components")
                .Attr("context", "service")
                .Start("component")
                .Attr("implementation", typeof(DekiDispatcher).AssemblyQualifiedName)
                .Attr("type", typeof(IPubSubDispatcher).AssemblyQualifiedName)
                .End()
                .End()
                );
            XDoc subForAuthorized = new XDoc("subscription-set")
                                    .Elem("uri.owner", mockAuthorized)
                                    .Start("subscription")
                                    .Attr("id", "1")
                                    .Elem("uri.resource", "http://mock/resource/*")
                                    .Elem("channel", "channel:///foo/*")
                                    .Start("recipient").Attr("authtoken", "abc").Elem("uri", mockAuthorized).End()
                                    .End();
            DreamMessage result = serviceInfo.WithInternalKey().AtLocalHost.At("subscribers").PostAsync(subForAuthorized).Wait();

            Assert.IsTrue(result.IsSuccessful);

            XDoc subForNotAuthorized = new XDoc("subscription-set")
                                       .Elem("uri.owner", mockNotAuthorized)
                                       .Start("subscription")
                                       .Attr("id", "1")
                                       .Elem("uri.resource", "http://mock/resource/*")
                                       .Elem("channel", "channel:///foo/*")
                                       .Start("recipient").Elem("uri", mockNotAuthorized).End()
                                       .End();

            result = serviceInfo.WithInternalKey().AtLocalHost.At("subscribers").PostAsync(subForNotAuthorized).Wait();
            Assert.IsTrue(result.IsSuccessful);
            XDoc msg = new XDoc("foop");

            result = serviceInfo.WithInternalKey()
                     .AtLocalHost
                     .At("publish")
                     .WithHeader(DreamHeaders.DREAM_EVENT_CHANNEL, "channel:///foo/bar")
                     .WithHeader(DreamHeaders.DREAM_EVENT_ORIGIN, mockDeki.ToString())
                     .WithHeader(DreamHeaders.DREAM_EVENT_RESOURCE, "http://mock/resource/bar")
                     .PostAsync(msg).Wait();
            Assert.IsTrue(result.IsSuccessful);
            Wait.For(() => authorizedResetEvent.WaitOne(100, true), TimeSpan.FromSeconds(10));
            Assert.AreEqual(0, dekiCalled);
            Assert.AreEqual(0, notAuthorizedCalled);
            Assert.AreEqual(1, authorizedCalled);
            Assert.AreEqual(msg, received);
        }
        public void Uses_deki_to_prune_recipients()
        {
            XUri mockDeki             = new XUri("http://mock/deki");
            int  dekiCalled           = 0;
            int  dekipage42authCalled = 0;
            bool dekiArgsGood         = false;
            int  dekipage43authCalled = 0;

            MockPlug.Register(mockDeki, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                _log.DebugFormat("mockDeki called at: {0}", u);
                dekiCalled++;
                dekiArgsGood     = false;
                List <int> users = new List <int>();
                foreach (XDoc user in r.ToDocument()["user/@id"])
                {
                    users.Add(user.AsInt.Value);
                }
                if (users.Count == 4 && users.Contains(1) && users.Contains(2) && users.Contains(3) && users.Contains(4))
                {
                    dekiArgsGood = true;
                }
                DreamMessage msg = DreamMessage.Ok();
                if (u.WithoutQuery() == mockDeki.At("pages", "42", "allowed"))
                {
                    dekipage42authCalled++;
                    msg = DreamMessage.Ok(new XDoc("users")
                                          .Start("user").Attr("id", 1).End()
                                          .Start("user").Attr("id", 2).End());
                }
                else if (u.WithoutQuery() == mockDeki.At("pages", "43", "allowed"))
                {
                    dekipage43authCalled++;
                    msg = DreamMessage.Ok(new XDoc("users")
                                          .Start("user").Attr("id", 3).End()
                                          .Start("user").Attr("id", 4).End());
                }
                r2.Return(msg);
            });
            XUri           mockRecipient       = new XUri("http://mock/r1");
            int            mockRecipientCalled = 0;
            XDoc           received            = null;
            List <string>  recipients          = new List <string>();
            AutoResetEvent are = new AutoResetEvent(false);

            MockPlug.Register(mockRecipient, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                _log.DebugFormat("mockRecipient called at: {0}", u);
                mockRecipientCalled++;
                received = r.ToDocument();
                recipients.Clear();
                recipients.AddRange(r.Headers.DreamEventRecipients);
                are.Set();
                r2.Return(DreamMessage.Ok());
            });

            DreamServiceInfo serviceInfo = DreamTestHelper.CreateService(
                _hostInfo,
                "sid://mindtouch.com/dream/2008/10/pubsub",
                "whitelist",
                new XDoc("config")
                .Elem("uri.deki", mockDeki)
                .Start("components")
                .Attr("context", "service")
                .Start("component")
                .Attr("implementation", typeof(DekiDispatcher).AssemblyQualifiedName)
                .Attr("type", typeof(IPubSubDispatcher).AssemblyQualifiedName)
                .End()
                .End()
                );
            XDoc sub = new XDoc("subscription-set")
                       .Elem("uri.owner", mockRecipient)
                       .Start("subscription")
                       .Attr("id", "1")
                       .Elem("uri.resource", "http://mock/resource/x")
                       .Elem("channel", "channel:///foo/*")
                       .Elem("uri.proxy", mockRecipient)
                       .Start("recipient").Attr("userid", "1").Elem("uri", "http://recipient/a").End()
                       .Start("recipient").Attr("userid", "2").Elem("uri", "http://recipient/b").End()
                       .Start("recipient").Attr("userid", "3").Elem("uri", "http://recipient/c").End()
                       .Start("recipient").Attr("userid", "4").Elem("uri", "http://recipient/d").End()
                       .End()
                       .Start("subscription")
                       .Attr("id", "2")
                       .Elem("uri.resource", "http://mock/resource/y")
                       .Elem("channel", "channel:///foo/*")
                       .Elem("uri.proxy", mockRecipient)
                       .Start("recipient").Attr("userid", "1").Elem("uri", "http://recipient/a").End()
                       .Start("recipient").Attr("userid", "2").Elem("uri", "http://recipient/b").End()
                       .Start("recipient").Attr("userid", "3").Elem("uri", "http://recipient/c").End()
                       .Start("recipient").Attr("userid", "4").Elem("uri", "http://recipient/d").End()
                       .End();
            DreamMessage result = serviceInfo.WithInternalKey().AtLocalHost.At("subscribers").PostAsync(sub).Wait();

            Assert.IsTrue(result.IsSuccessful);
            XDoc ev = new XDoc("event").Elem("pageid", 42);

            result = serviceInfo.WithInternalKey()
                     .AtLocalHost
                     .At("publish")
                     .WithHeader(DreamHeaders.DREAM_EVENT_CHANNEL, "channel:///foo/bar")
                     .WithHeader(DreamHeaders.DREAM_EVENT_ORIGIN, mockDeki.ToString())
                     .WithHeader(DreamHeaders.DREAM_EVENT_RESOURCE, "http://mock/resource/x")
                     .PostAsync(ev).Wait();
            Assert.IsTrue(result.IsSuccessful);
            Assert.IsTrue(are.WaitOne(500, true));
            Assert.AreEqual(1, dekiCalled);
            Assert.AreEqual(1, dekipage42authCalled);
            Assert.IsTrue(dekiArgsGood);
            Assert.AreEqual(ev, received);
            Assert.AreEqual(2, recipients.Count);
            Assert.Contains("http://recipient/a", recipients);
            Assert.Contains("http://recipient/b", recipients);
            ev     = new XDoc("event").Elem("pageid", 43);
            result = serviceInfo.WithInternalKey()
                     .AtLocalHost
                     .At("publish")
                     .WithHeader(DreamHeaders.DREAM_EVENT_CHANNEL, "channel:///foo/bar")
                     .WithHeader(DreamHeaders.DREAM_EVENT_ORIGIN, mockDeki.ToString())
                     .WithHeader(DreamHeaders.DREAM_EVENT_RESOURCE, "http://mock/resource/y")
                     .PostAsync(ev).Wait();
            Assert.IsTrue(result.IsSuccessful);
            Assert.IsTrue(are.WaitOne(5000, true));
            Assert.AreEqual(2, dekiCalled);
            Assert.AreEqual(1, dekipage42authCalled);
            Assert.IsTrue(dekiArgsGood);
            Assert.AreEqual(ev, received);
            Assert.AreEqual(2, recipients.Count);
            Assert.Contains("http://recipient/c", recipients);
            Assert.Contains("http://recipient/d", recipients);
        }
        public void Service_end_to_end_no_deki()
        {
            // set up mocks for all the support service calls
            string apikey     = "abc";
            XUri   deki       = new XUri("http://mock/deki");
            int    dekiCalled = 0;

            MockPlug.Register(deki, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                _log.DebugFormat("deki: {0}:{1}", v, u);
                dekiCalled++;
                r2.Return(DreamMessage.Ok());
            });

            XUri           varnish           = new XUri("http://mock/varnish");
            AutoResetEvent varnishResetEvent = new AutoResetEvent(false);
            string         varnishHeader     = "";
            int            varnishCalled     = 0;

            MockPlug.Register(varnish, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                _log.DebugFormat("varnish: {0}:{1}", v, u);
                if (v == "PURGE")
                {
                    varnishHeader = r.Headers["X-Purge-Url"];
                    varnishCalled++;
                    varnishResetEvent.Set();
                }
                r2.Return(DreamMessage.Ok());
            });

            XUri           subscribe            = new XUri("http://mock/sub");
            XUri           subscriptionLocation = subscribe.At("testsub");
            AutoResetEvent subscribeResetEvent  = new AutoResetEvent(false);
            XUri           subscribeCalledUri   = null;
            XDoc           subscribePosted      = null;

            MockPlug.Register(subscribe, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                subscribeCalledUri = u;
                if (u == subscribe.At("subscribers"))
                {
                    _log.Debug("creating subscription");
                    DreamMessage msg     = DreamMessage.Ok(new XDoc("x"));
                    msg.Headers.Location = subscriptionLocation;
                    subscribeResetEvent.Set();
                    r2.Return(msg);
                }
                else
                {
                    _log.Debug("updating subscription");
                    subscribePosted = r.ToDocument();
                    subscribeResetEvent.Set();
                    r2.Return(DreamMessage.Ok());
                }
            });

            // set up service
            _log.Debug("set up service");
            DreamServiceInfo serviceInfo = DreamTestHelper.CreateService(
                _hostInfo,
                typeof(VarnishPurgeService),
                "varnish",
                new XDoc("config")
                .Elem("uri.deki", deki)
                .Elem("uri.varnish", varnish)
                .Elem("uri.pubsub", subscribe)
                .Elem("varnish-purge-delay", 1)
                .Elem("apikey", apikey)
                );
            Plug service = serviceInfo.WithInternalKey().AtLocalHost;

            // expect:
            // - storage was queried
            // - subscription was created on subscribe
            Assert.IsTrue(subscribeResetEvent.WaitOne(100, true));
            Assert.AreEqual(subscribe.At("subscribers"), subscribeCalledUri);

            // post page varnish event
            service.At("queue").Post(
                new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/create")
                .Elem("pageid", "1")
                .Elem("path", "x/y/z"));
            Assert.IsTrue(varnishResetEvent.WaitOne(5000, false));
            Assert.AreEqual(0, dekiCalled);
            Assert.AreEqual(1, varnishCalled);
            Assert.AreEqual("^/((x/y/z|index\\.php\\?title=x/y/z)[\\?&]?|@api/deki/pages/1/?).*$", varnishHeader);
            Assert.IsTrue(Wait.For(() => varnishCalled == 1, 10.Seconds()), "varnish wasn't called");
        }