Ejemplo n.º 1
0
        public void added_apikeys_should_be_retreived_as_comma_separated_string()
        {
            string key1 = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfg";
            string key2 = "mnbvcxzasdfghjklpoiuytrewq1234567890zxcv";

            Notification notification = new Notification();

            notification.AddApiKey(key1);
            notification.AddApiKey(key2);

            Assert.Equal(key1 + "," + key2, notification.Keys);
        }
        public void should_build_dictionary_correctly_from_notification()
        {
            Notification notif = new Notification()
            {
                Application = "app",
                Description = "descr",
                Event       = "evt",
                Priority    = NotificationPriority.Emergency,
                Url         = "http://www.nnihlen.com/blog"
            };

            notif.AddApiKey("asdf");

            RequestBuilderHelper        helper = new RequestBuilderHelper();
            Dictionary <string, string> dict   = helper.BuildDictionaryForNotificataion(notif);

            Assert.True(dict.ContainsKey("application"));
            Assert.Equal("app", dict["application"]);

            Assert.True(dict.ContainsKey("description"));
            Assert.Equal("descr", dict["description"]);

            Assert.True(dict.ContainsKey("event"));
            Assert.Equal("evt", dict["event"]);

            Assert.True(dict.ContainsKey("priority"));
            Assert.Equal("2", dict["priority"]);

            Assert.True(dict.ContainsKey("url"));
            Assert.Equal("http://www.nnihlen.com/blog", dict["url"]);

            Assert.True(dict.ContainsKey("apikey"));
            Assert.Equal("asdf", dict["apikey"]);
        }
Ejemplo n.º 3
0
        public void no_application_should_not_pass_validation_and_throw_ArgumentException()
        {
            var fakeHttpInterface = new FakeHttpInterface();
            var prowlClient       = new ProwlClient(fakeHttpInterface);

            var n = new Notification
            {
                Description = "foo",
                Event       = "event",
                Priority    = NotificationPriority.Normal,
                Url         = "http://www.nnihle.com/blog"
            };

            n.AddApiKey("aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd");

            Assert.Throws(typeof(ArgumentException), delegate { prowlClient.SendNotification(n); });
        }
Ejemplo n.º 4
0
        public void Application_with_more_than_256_characters_should_throw_ArgumentException()
        {
            var fakeHttpInterface = new FakeHttpInterface();
            var prowlClient       = new ProwlClient(fakeHttpInterface);

            var appStr = new string('n', 257);
            var n      = new Notification
            {
                Application = appStr,
                Description = "foo",
                Event       = "event",
                Priority    = NotificationPriority.Normal,
                Url         = "http://www.nnihle.com/blog"
            };

            n.AddApiKey("aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd");

            Assert.Throws(typeof(ArgumentException), delegate { prowlClient.SendNotification(n); });
        }
Ejemplo n.º 5
0
        public void SendVerification_on_httpInterface_should_be_called_when_sending_notification()
        {
            var fakeHttpInterface = new FakeHttpInterface();
            var prowlClient       = new ProwlClient(fakeHttpInterface);

            var n = new Notification
            {
                Application = "app",
                Description = "foo",
                Event       = "event",
                Priority    = NotificationPriority.Normal
            };

            n.AddApiKey("aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd");

            prowlClient.SendNotification(n);

            Assert.True(fakeHttpInterface.SendNotificationsCalled);
        }
Ejemplo n.º 6
0
        public void validation_should_pass_with_correct_notifictaion_object()
        {
            var fakeHttpInterface = new FakeHttpInterface();
            var prowlClient       = new ProwlClient(fakeHttpInterface);

            var n = new Notification
            {
                Application = "app",
                Description = "foo",
                Event       = "event",
                Priority    = NotificationPriority.Normal,
                Url         = "http://www.nnihle.com/blog"
            };

            n.AddApiKey("aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd");

            prowlClient.SendNotification(n);

            Assert.True(fakeHttpInterface.SendNotificationsCalled);
        }
Ejemplo n.º 7
0
        public void Url_with_more_than_512_characters_should_not_pass_validation_and_throw_ArgumentException()
        {
            var fakeHttpInterface = new FakeHttpInterface();
            var prowlClient       = new ProwlClient(fakeHttpInterface);

            var url = new string('n', 513);

            var n = new Notification
            {
                Application = "Application",
                Description = "foo",
                Event       = "evnet",
                Priority    = NotificationPriority.Normal,
                Url         = url
            };

            n.AddApiKey("aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd");

            Assert.Throws(typeof(ArgumentException), delegate { prowlClient.SendNotification(n); });
        }
Ejemplo n.º 8
0
        public void Event_with_more_than_1024_characters_should_not_pass_validation_and_throw_ArgumentException()
        {
            var fakeHttpInterface = new FakeHttpInterface();
            var prowlClient       = new ProwlClient(fakeHttpInterface);

            var evt = new string('n', 2000);

            var n = new Notification
            {
                Application = "Application",
                Description = "foo",
                Event       = evt,
                //Toooo long
                Priority = NotificationPriority.Normal,
                Url      = "http://www.nnihle.com/blog"
            };

            n.AddApiKey("aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd");

            Assert.Throws(typeof(ArgumentException), delegate { prowlClient.SendNotification(n); });
        }
Ejemplo n.º 9
0
        public virtual bool SendNotification(string title, string message, string apiKeys, NotificationPriority priority = NotificationPriority.Normal, string url = null)
        {
            try
            {
                var notification = new Notification
                {
                    Application = "NzbDrone",
                    Description = message,
                    Event       = title,
                    Priority    = priority,
                    Url         = url
                };

                foreach (var apiKey in apiKeys.Split(','))
                {
                    notification.AddApiKey(apiKey.Trim());
                }

                var client = new ProwlClient();

                Logger.Trace("Sending Prowl Notification");

                var notificationResult = client.SendNotification(notification);

                if (String.IsNullOrWhiteSpace(notificationResult.ErrorMessage))
                {
                    return(true);
                }
            }

            catch (Exception ex)
            {
                Logger.TraceException(ex.Message, ex);
                Logger.Warn("Invalid API Key(s): {0}", apiKeys);
            }

            return(false);
        }
Ejemplo n.º 10
0
        private void Run(string[] args)
        {
            ArgumentsObject arguments = Configuration.Configure <ArgumentsObject>().CreateAndBind(args);

            if (arguments.Help || args.Length == 0)
            {
                ShowHelp();
                return;
            }

            var prowlClient = new ProwlClient();

            if (arguments.RetrieveToken)
            {
                RetrieveToken retrieveToken = new RetrieveToken();
                retrieveToken.ProviderKey = arguments.ProviderKey;

                RetrieveTokenResult result = prowlClient.RetreiveToken(retrieveToken);
                System.Console.WriteLine("Token retreived\nToken: {0}\nUrl: {1}",
                                         result.Token,
                                         result.Url);

                return;
            }

            if (arguments.NewKey)
            {
                if (string.IsNullOrEmpty(arguments.Token) || string.IsNullOrEmpty(arguments.ProviderKey))
                {
                    System.Console.WriteLine("ProviderKey and Token required for this operation.");
                    return;
                }
                RetrieveApikey retrieveApikey = new RetrieveApikey();
                retrieveApikey.Token       = arguments.Token;
                retrieveApikey.ProviderKey = arguments.ProviderKey;

                RetrieveApikeyResult retrieveApikeyResult = prowlClient.RetrieveApikey(retrieveApikey);
                System.Console.WriteLine("New APIKEY: {0}");
                return;
            }

            if (string.IsNullOrEmpty(arguments.Key))
            {
                System.Console.WriteLine("ApiKey requried");
                return;
            }

            if (arguments.Verify)
            {
                IVerification v = new Verification();
                v.ApiKey      = arguments.Key;
                v.ProviderKey = arguments.ProviderKey;
                System.Console.WriteLine("Sending verification...");
                VerificationResult verificationResult = prowlClient.SendVerification(v);
                System.Console.WriteLine("Verification {3}\n\tVerification returned: {0} \n\tNumber of messages left to send: {1}\n\tReset UNIX timestamp: {2}",
                                         verificationResult.ResultCode,
                                         verificationResult.RemainingMessageCount.ToString(),
                                         verificationResult.TimeStamp,
                                         verificationResult.ResultCode == "200" ? "OK" : "NOT OK");
            }
            else
            {
                if (string.IsNullOrEmpty(arguments.Event))
                {
                    System.Console.WriteLine("Event is required");
                    return;
                }

                if (string.IsNullOrEmpty(arguments.Application))
                {
                    System.Console.WriteLine("Application is required");
                    return;
                }

                var notification = new Notification
                {
                    Application = arguments.Application,
                    Description = arguments.Description,
                    Event       = arguments.Event,
                    Url         = arguments.Url
                };


                switch (arguments.Priority.ToLower())
                {
                case "verylow":
                    notification.Priority = NotificationPriority.VeryLow;
                    break;

                case "moderate":
                    notification.Priority = NotificationPriority.Moderate;
                    break;

                case "high":
                    notification.Priority = NotificationPriority.High;
                    break;

                case "emergency":
                    notification.Priority = NotificationPriority.Emergency;
                    break;

                default:
                    notification.Priority = NotificationPriority.Normal;
                    break;
                }

                foreach (string s in arguments.Key.Split(new[] { ',', ';' }))
                {
                    notification.AddApiKey(s);
                }

                NotificationResult notificationResult = prowlClient.SendNotification(notification);

                System.Console.WriteLine("Remaing number of messages: {0}", notificationResult.RemainingMessageCount.ToString());
            }
        }
Ejemplo n.º 11
0
        private void Run(string[] args)
        {
            ArgumentsObject arguments = Configuration.Configure<ArgumentsObject>().CreateAndBind(args);

            if(arguments.Help || args.Length == 0) {
                ShowHelp();
                return;
            }

            var prowlClient = new ProwlClient();

            if (arguments.RetrieveToken)
            {
                RetrieveToken retrieveToken = new RetrieveToken();
                retrieveToken.ProviderKey = arguments.ProviderKey;

                RetrieveTokenResult result = prowlClient.RetreiveToken(retrieveToken);
                System.Console.WriteLine("Token retreived\nToken: {0}\nUrl: {1}",
                    result.Token,
                    result.Url);

                return;
            }

            if (arguments.NewKey) {
                if(string.IsNullOrEmpty(arguments.Token) || string.IsNullOrEmpty(arguments.ProviderKey)) {
                    System.Console.WriteLine("ProviderKey and Token required for this operation.");
                    return;
                }
                RetrieveApikey retrieveApikey = new RetrieveApikey();
                retrieveApikey.Token = arguments.Token;
                retrieveApikey.ProviderKey = arguments.ProviderKey;

                RetrieveApikeyResult retrieveApikeyResult = prowlClient.RetrieveApikey(retrieveApikey);
                System.Console.WriteLine("New APIKEY: {0}");
                return;
            }

            if (string.IsNullOrEmpty(arguments.Key)) {
                System.Console.WriteLine("ApiKey requried");
                return;
            }

            if(arguments.Verify) {
                IVerification v = new Verification();
                v.ApiKey = arguments.Key;
                v.ProviderKey = arguments.ProviderKey;
                System.Console.WriteLine("Sending verification...");
                VerificationResult verificationResult = prowlClient.SendVerification(v);
                System.Console.WriteLine("Verification {3}\n\tVerification returned: {0} \n\tNumber of messages left to send: {1}\n\tReset UNIX timestamp: {2}",
                    verificationResult.ResultCode,
                    verificationResult.RemainingMessageCount.ToString(),
                    verificationResult.TimeStamp,
                    verificationResult.ResultCode == "200" ? "OK" : "NOT OK");
            }
            else {
                if (string.IsNullOrEmpty(arguments.Event))
                {
                    System.Console.WriteLine("Event is required");
                    return;
                }

                if (string.IsNullOrEmpty(arguments.Application))
                {
                    System.Console.WriteLine("Application is required");
                    return;
                }

                var notification = new Notification
                {
                    Application = arguments.Application,
                    Description = arguments.Description,
                    Event = arguments.Event,
                    Url = arguments.Url
                };

                switch (arguments.Priority.ToLower())
                {
                    case "verylow":
                        notification.Priority = NotificationPriority.VeryLow;
                        break;
                    case "moderate":
                        notification.Priority = NotificationPriority.Moderate;
                        break;
                    case "high":
                        notification.Priority = NotificationPriority.High;
                        break;
                    case "emergency":
                        notification.Priority = NotificationPriority.Emergency;
                        break;
                    default:
                        notification.Priority = NotificationPriority.Normal;
                        break;
                }

                foreach (string s in arguments.Key.Split(new[] { ',', ';' }))
                {
                    notification.AddApiKey(s);
                }

                NotificationResult notificationResult= prowlClient.SendNotification(notification);

                System.Console.WriteLine("Remaing number of messages: {0}", notificationResult.RemainingMessageCount.ToString());
            }
        }