public void Table_CreateResourceToken_ReturnsCorrectHostPath()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();

            // Act.
            ResourceToken token = broker.CreateResourceToken(ConnectionString, new ResourceParameters { Name = "table", Permissions = ResourcePermissions.Read, Expiration = DateTime.Now + TimeSpan.FromDays(1) });

            // Assert.
            Assert.IsNotNull(token);

            SASParts parts = new SASParts(token.Uri);
            Assert.AreEqual("https://test.table.core.windows.net/table", parts.HostName);
        }
        public void Table_CreateResourceToken_WithExpirationDate_ReturnsCorrectExpirationDate()
        {
            // Setup
            DateTime expiration = new DateTime(2199, 3, 12, 1, 2, 3, DateTimeKind.Utc);
            AzureTableBroker broker = new AzureTableBroker();

            // Act.
            ResourceToken token = broker.CreateResourceToken(ConnectionString, new ResourceParameters { Name = "table", Permissions = ResourcePermissions.Read, Expiration = expiration });

            // Assert.
            Assert.IsNotNull(token);

            SASParts parts = new SASParts(token.Uri);
            Assert.AreEqual("2199-03-12T01%3A02%3A03Z", parts.Value("se"));
        }
        public void Table_CreateResourceToken_WithNoExpirationDate_ReturnsNoExpirationDate()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();

            // Act.
            ResourceToken token = broker.CreateResourceToken(ConnectionString, new ResourceParameters { Name = "table", Permissions = ResourcePermissions.Read });

            // Assert.
            Assert.IsNotNull(token);

            SASParts parts = new SASParts(token.Uri);
            Assert.AreEqual(null, parts.Value("se"));
        }
        public void Table_ExtractConnectionString_WithQueueConnectionString_ThrowsException()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();
            Dictionary<string, string> appSettings = new Dictionary<string, string>();
            appSettings.Add("ResourceBrokerQueueConnectionString", ConnectionString);

            // Act
            this.ExpectException<InvalidOperationException>(() => broker.ExtractConnectionString(appSettings));
        }
        public void Table_ExtractConnectionString_AppSettingsTableConnectionString_ReturnsTableConnectionString()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();
            Dictionary<string, string> appSettings = new Dictionary<string, string>();
            appSettings.Add("ResourceBrokerTableConnectionString", ConnectionString);

            // Act
            string c = broker.ExtractConnectionString(appSettings);

            // Assert
            Assert.AreEqual(c, ConnectionString);
        }
 public void Table_ExtractConnectionString_AppSettingsTableConnectionStringIsNull_ThrowsArgumentException()
 {
     AzureTableBroker broker = new AzureTableBroker();
     Dictionary<string, string> appSettings = new Dictionary<string, string>();
     appSettings.Add("ResourceBrokerTableConnectionString", null);
     this.ExpectException<InvalidOperationException>(() => broker.ExtractConnectionString(appSettings));
 }
        public void Table_CreateResourceToken_WithProcessPermissions_IgnoresProcessPermissions()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();

            // Act
            ResourceToken token = broker.CreateResourceToken(ConnectionString, new ResourceParameters { Name = "table", Permissions = ResourcePermissions.Read | ResourcePermissions.Process, Expiration = DateTime.Now + TimeSpan.FromDays(1) });

            // Assert.
            Assert.IsNotNull(token);
            SASParts parts = new SASParts(token.Uri);
            Assert.AreEqual("r", parts.Value("sp"));
        }
        public void Table_CreateResourceToken_WithUpdateDeletePermissions_ReturnsCorrectAccess()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();

            // Act
            ResourceToken token = broker.CreateResourceToken(ConnectionString, new ResourceParameters { Name = "table", Permissions = ResourcePermissions.Update | ResourcePermissions.Delete, Expiration = DateTime.Now + TimeSpan.FromDays(1) });

            // Assert.
            Assert.IsNotNull(token);
            SASParts parts = new SASParts(token.Uri);
            Assert.AreEqual("ud", parts.Value("sp"));
        }
        public void Table_CreateResourceToken_ReturnsExpectedStartDate()
        {
            // Setup
            AzureTableBroker broker = new AzureTableBroker();

            // Act.
            ResourceToken token = broker.CreateResourceToken(ConnectionString, new ResourceParameters { Name = "table", Permissions = ResourcePermissions.Read });

            // Assert.
            Assert.IsNotNull(token);

            // Calculate the expected start time give or take 4 seconds.
            DateTime startRangeBegin = DateTime.UtcNow - TimeSpan.FromMinutes(5) - TimeSpan.FromSeconds(2);
            DateTime startRangeEnd = startRangeBegin + TimeSpan.FromSeconds(2);

            // Now convert these into strings using the SAS format.
            string startRangeBeginString = this.DateTimeToSASDateString(startRangeBegin);
            string startRangeEndString = this.DateTimeToSASDateString(startRangeEnd);

            // Get the actual begin time from the SAS token.
            SASParts parts = new SASParts(token.Uri);
            string beginning = parts.Value("st");

            // Make sure it is within the range.
            Assert.IsTrue(string.CompareOrdinal(beginning, startRangeBeginString) >= 0 && string.CompareOrdinal(beginning, startRangeEndString) <= 0);
        }