Skip to content

Add new cmdlets for storage SAS token stored policy. #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@
<Compile Include="Service\MockStorageQueueManagement.cs" />
<Compile Include="Service\MockStorageTableManagement.cs" />
<Compile Include="StorageTestBase.cs" />
<Compile Include="Table\GetAzureStorageTableStoredAccessPolicyTest.cs" />
<Compile Include="Table\GetAzureStorageTableTest.cs" />
<Compile Include="Table\NewAzureStorageTableSasTest.cs" />
<Compile Include="Table\NewAzureStorageTableStoredAccessPolicyTest.cs" />
<Compile Include="Table\NewAzureStorageTableTest.cs" />
<Compile Include="Table\RemoveAzureStorageTableStoredAccessPolicyTest.cs" />
<Compile Include="Table\RemoveAzureStorageTableTest.cs" />
<Compile Include="Table\SetAzureStorageTableStoredAccessPolicyTest.cs" />
<Compile Include="Table\StorageTableStorageTestBase.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Commands.Common.Storage;
using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
using Microsoft.WindowsAzure.Storage;
Expand Down Expand Up @@ -158,6 +159,16 @@ public QueuePermissions GetPermissions(CloudQueue queue, QueueRequestOptions opt
throw new NotImplementedException();
}

public Task<QueuePermissions> GetPermissionsAsync(CloudQueue queue, QueueRequestOptions requestOptions, OperationContext operationContext)
{
throw new NotImplementedException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to have this given its not implemented?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mock class used in test cases. It implements an interface which has this method in it. But we don't need to use this method in test cases, so just leave it to be not implemented..

}

public void SetPermissions(CloudQueue queue, QueuePermissions queuePermissions, QueueRequestOptions requestOptions, OperationContext operationContext)
{
throw new NotImplementedException();
}

public AzureStorageContext StorageContext
{
get { throw new NotImplementedException(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Commands.Common.Storage;
using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
using Microsoft.WindowsAzure.Storage;
Expand All @@ -31,6 +32,11 @@ public class MockStorageTableManagement : IStorageTableManagement
/// </summary>
public List<CloudTable> tableList = new List<CloudTable>();

/// <summary>
/// Exists permissions
/// </summary>
public TablePermissions tablePermissions = new TablePermissions();

/// <summary>
/// Table end point
/// </summary>
Expand Down Expand Up @@ -166,9 +172,49 @@ public bool DoesTableExist(CloudTable table, TableRequestOptions requestOptions,
/// <returns>Table permission</returns>
public TablePermissions GetTablePermissions(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
{
throw new NotImplementedException();
return this.tablePermissions;
}

/// <summary>
/// Set table permission
/// </summary>
/// <param name="table">Cloud table object</param>
/// <param name="tablePermissions">table permissions</param>
/// <param name="requestOptions">Table request options</param>
/// <param name="operationContext">Operation context</param>
/// <returns></returns>
public void SetTablePermissions(CloudTable table, TablePermissions tablePermissions, TableRequestOptions requestOptions, OperationContext operationContext)
{
this.tablePermissions = tablePermissions;
}

/// <summary>
/// Return a task that asynchronously set table permissions
/// </summary>
/// <param name="table">target table</param>
/// <param name="tablePermissions">permissions to set</param>
/// <param name="requestOptions">request options</param>
/// <param name="operationContext">context</param>
/// <returns></returns>
public Task SetTablePermissionsAsync(CloudTable table, TablePermissions tablePermissions, TableRequestOptions requestOptions, OperationContext operationContext)
{
return Task.Factory.StartNew(() => this.SetTablePermissions(table, tablePermissions, requestOptions, operationContext));
}

/// <summary>
/// Return a task that asynchronously fetch table permissions
/// </summary>
/// <param name="table">target table</param>
/// <param name="requestOptions">request options</param>
/// <param name="operationContext">context</param>
/// <returns></returns>
public Task<TablePermissions> GetTablePermissionsAsync(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
{
return Task.Factory.StartNew(() => this.GetTablePermissions(table,
requestOptions, operationContext));
}


public AzureStorageContext StorageContext
{
get { throw new NotImplementedException(); }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// ----------------------------------------------------------------------------------
//
// Copyright 2012 Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.WindowsAzure.Commands.Storage.Test.Table
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Commands.Storage.Common;
using Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;

[TestClass]
public class GetAzureStorageTableStoredAccessPolicyTest : StorageTableStorageTestBase
{
public GetAzureStorageTableStoredAccessPolicyCommand command = null;

[TestInitialize]
public void InitCommand()
{
command = new GetAzureStorageTableStoredAccessPolicyCommand(tableMock)
{
CommandRuntime = MockCmdRunTime
};
CurrentTableCmd = command;
}

[TestCleanup]
public void CleanCommand()
{
command = null;
}

[TestMethod]
public void GetStoredAccessPolicyNotExistsTest()
{
AddTestStoredAccessPolicy();
string policyName = "Policy" + Guid.NewGuid();
string tableName = "sampleTable";

MockCmdRunTime.ResetPipelines();
command.Table = tableName;
command.Policy = policyName;
RunAsyncCommand(() => command.ExecuteCmdlet());

Assert.AreEqual(0, MockCmdRunTime.OutputPipeline.Count);

MockCmdRunTime.ResetPipelines();
clearTest();
}

[TestMethod]
public void GetStoredAccessPolicySuccessTest()
{
AddTestStoredAccessPolicy();
string policyName = TestPolicy1;
string tableName = "sampleTable";

MockCmdRunTime.ResetPipelines();
command.Table = tableName;
command.Policy = policyName;
RunAsyncCommand(() => command.ExecuteCmdlet());

Assert.AreEqual(1, MockCmdRunTime.OutputPipeline.Count);

MockCmdRunTime.ResetPipelines();
clearTest();
}

[TestMethod]
public void GetAllStoredAccessPolicySuccessTest()
{
AddTestStoredAccessPolicy();
string tableName = "sampleTable";

MockCmdRunTime.ResetPipelines();
command.Table = tableName;
RunAsyncCommand(() => command.ExecuteCmdlet());

Assert.AreEqual(2, MockCmdRunTime.OutputPipeline.Count);

MockCmdRunTime.ResetPipelines();
clearTest();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// ----------------------------------------------------------------------------------
//
// Copyright 2012 Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.WindowsAzure.Commands.Storage.Test.Table
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Commands.Storage.Common;
using Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Globalization;

[TestClass]
public class NewAzureStorageTableStoredAccessPolicyTest : StorageTableStorageTestBase
{
public NewAzureStorageTableStoredAccessPolicyCommand command = null;

[TestInitialize]
public void InitCommand()
{
command = new NewAzureStorageTableStoredAccessPolicyCommand(tableMock)
{
CommandRuntime = MockCmdRunTime
};
}

[TestCleanup]
public void CleanCommand()
{
command = null;
}

[TestMethod]
public void CreateAzureTableStoredAccessPolicyWithInvalidNameTest()
{
clearTest();
//policy name lenght longer than 64
string policyName = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
string tableName = "sampleTable";
AssertThrows<ArgumentException>(() => command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, null, null, null),
String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, policyName));
clearTest();
}


[TestMethod]
public void CreateAzureTableStoredAccessPolicySuccessTest()
{
clearTest();
string policyName = "Policy" + Guid.NewGuid();
string tableName = "sampleTable";

string permission = "rd";
DateTime startTime = DateTime.Today;
DateTime expiryTime = DateTime.Today.AddDays(1);
command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, startTime, expiryTime, permission);

SharedAccessTablePermissions expectedPermissions = SharedAccessTablePermissions.None;
expectedPermissions |= SharedAccessTablePermissions.Query;
expectedPermissions |= SharedAccessTablePermissions.Delete;
SharedAccessTablePolicy resultPolicy = tableMock.tablePermissions.SharedAccessPolicies[policyName];
Assert.IsNotNull(resultPolicy);
Assert.AreEqual<SharedAccessTablePermissions>(expectedPermissions, resultPolicy.Permissions);
Assert.AreEqual<DateTimeOffset?>(startTime.ToUniversalTime(), resultPolicy.SharedAccessStartTime);
Assert.AreEqual<DateTimeOffset?>(expiryTime.ToUniversalTime(), resultPolicy.SharedAccessExpiryTime);
clearTest();
}


[TestMethod]
public void CreateStoredAccessPolicyAlreadyExistsTest()
{
clearTest();
string policyName = "Policy" + Guid.NewGuid();
string tableName = "sampleTable";

command.Table = tableName;
command.Policy = policyName;
command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, null, null, null);
AssertThrows<ResourceAlreadyExistException>(() => command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, null, null, null),
String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, policyName));
clearTest();
}

[TestMethod]
public void CreateStoredAccessPolicyInvalidExpirtTime()
{
clearTest();
string policyName = "Policy" + Guid.NewGuid();
string tableName = "sampleTable";

DateTime? startTime = DateTime.Today;
DateTime? expiryTime = DateTime.Today.AddDays(-1);
DateTimeOffset? expectedSharedAccessStartTime = startTime.Value.ToUniversalTime();
DateTimeOffset? expectedSharedAccessExpiryTime = expiryTime.Value.ToUniversalTime();
AssertThrows<ArgumentException>(() => command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, startTime, expiryTime, null),
String.Format(CultureInfo.CurrentCulture, Resources.ExpiryTimeGreatThanStartTime, expectedSharedAccessExpiryTime, expectedSharedAccessStartTime));
clearTest();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ----------------------------------------------------------------------------------
//
// Copyright 2012 Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.WindowsAzure.Commands.Storage.Test.Table
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Commands.Storage.Common;
using Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

[TestClass]
public class RemoveAzureStorageTableStoredAccessPolicyTest : StorageTableStorageTestBase
{
public RemoveAzureStorageTableStoredAccessPolicyCommand command = null;

[TestInitialize]
public void InitCommand()
{
command = new RemoveAzureStorageTableStoredAccessPolicyCommand(tableMock)
{
CommandRuntime = MockCmdRunTime
};
}

[TestCleanup]
public void CleanCommand()
{
command = null;
}

[TestMethod]
public void RemoveStoredAccessPolicyNotExistsTest()
{
AddTestStoredAccessPolicy();
string policyName = "Policy" + Guid.NewGuid();
string tableName = "sampleTable";

AssertThrows<ResourceNotFoundException>(() => command.RemoveAzureTableStoredAccessPolicy(tableMock, tableName, policyName),
String.Format(CultureInfo.CurrentCulture, Resources.PolicyNotFound, policyName));
clearTest();
}

[TestMethod]
public void RemoveStoredAccessPolicySuccessTest()
{
AddTestStoredAccessPolicy();
string policyName = TestPolicy1;
string tableName = "sampleTable";

command.RemoveAzureTableStoredAccessPolicy(tableMock, tableName, policyName);
Assert.IsTrue(!tableMock.tablePermissions.SharedAccessPolicies.Keys.Contains(policyName));

clearTest();
}
}
}
Loading