Skip to content

Commit 5c78199

Browse files
committed
Merge pull request Azure#22 from EmmaZhu/dev
Add new cmdlets for storage SAS token stored policy.
2 parents 474e063 + e211c63 commit 5c78199

32 files changed

+5271
-371
lines changed

src/ServiceManagement/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@
170170
<Compile Include="Service\MockStorageQueueManagement.cs" />
171171
<Compile Include="Service\MockStorageTableManagement.cs" />
172172
<Compile Include="StorageTestBase.cs" />
173+
<Compile Include="Table\GetAzureStorageTableStoredAccessPolicyTest.cs" />
173174
<Compile Include="Table\GetAzureStorageTableTest.cs" />
174175
<Compile Include="Table\NewAzureStorageTableSasTest.cs" />
176+
<Compile Include="Table\NewAzureStorageTableStoredAccessPolicyTest.cs" />
175177
<Compile Include="Table\NewAzureStorageTableTest.cs" />
178+
<Compile Include="Table\RemoveAzureStorageTableStoredAccessPolicyTest.cs" />
176179
<Compile Include="Table\RemoveAzureStorageTableTest.cs" />
180+
<Compile Include="Table\SetAzureStorageTableStoredAccessPolicyTest.cs" />
177181
<Compile Include="Table\StorageTableStorageTestBase.cs" />
178182
</ItemGroup>
179183
<ItemGroup>

src/ServiceManagement/Storage/Commands.Storage.Test/Service/MockStorageQueueManagement.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Threading.Tasks;
1718
using Microsoft.WindowsAzure.Commands.Common.Storage;
1819
using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
1920
using Microsoft.WindowsAzure.Storage;
@@ -158,6 +159,16 @@ public QueuePermissions GetPermissions(CloudQueue queue, QueueRequestOptions opt
158159
throw new NotImplementedException();
159160
}
160161

162+
public Task<QueuePermissions> GetPermissionsAsync(CloudQueue queue, QueueRequestOptions requestOptions, OperationContext operationContext)
163+
{
164+
throw new NotImplementedException();
165+
}
166+
167+
public void SetPermissions(CloudQueue queue, QueuePermissions queuePermissions, QueueRequestOptions requestOptions, OperationContext operationContext)
168+
{
169+
throw new NotImplementedException();
170+
}
171+
161172
public AzureStorageContext StorageContext
162173
{
163174
get { throw new NotImplementedException(); }

src/ServiceManagement/Storage/Commands.Storage.Test/Service/MockStorageTableManagement.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Threading.Tasks;
1718
using Microsoft.WindowsAzure.Commands.Common.Storage;
1819
using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
1920
using Microsoft.WindowsAzure.Storage;
@@ -31,6 +32,11 @@ public class MockStorageTableManagement : IStorageTableManagement
3132
/// </summary>
3233
public List<CloudTable> tableList = new List<CloudTable>();
3334

35+
/// <summary>
36+
/// Exists permissions
37+
/// </summary>
38+
public TablePermissions tablePermissions = new TablePermissions();
39+
3440
/// <summary>
3541
/// Table end point
3642
/// </summary>
@@ -166,7 +172,46 @@ public bool DoesTableExist(CloudTable table, TableRequestOptions requestOptions,
166172
/// <returns>Table permission</returns>
167173
public TablePermissions GetTablePermissions(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
168174
{
169-
throw new NotImplementedException();
175+
return this.tablePermissions;
176+
}
177+
178+
/// <summary>
179+
/// Set table permission
180+
/// </summary>
181+
/// <param name="table">Cloud table object</param>
182+
/// <param name="tablePermissions">table permissions</param>
183+
/// <param name="requestOptions">Table request options</param>
184+
/// <param name="operationContext">Operation context</param>
185+
/// <returns></returns>
186+
public void SetTablePermissions(CloudTable table, TablePermissions tablePermissions, TableRequestOptions requestOptions, OperationContext operationContext)
187+
{
188+
this.tablePermissions = tablePermissions;
189+
}
190+
191+
/// <summary>
192+
/// Return a task that asynchronously set table permissions
193+
/// </summary>
194+
/// <param name="table">target table</param>
195+
/// <param name="tablePermissions">permissions to set</param>
196+
/// <param name="requestOptions">request options</param>
197+
/// <param name="operationContext">context</param>
198+
/// <returns></returns>
199+
public Task SetTablePermissionsAsync(CloudTable table, TablePermissions tablePermissions, TableRequestOptions requestOptions, OperationContext operationContext)
200+
{
201+
return Task.Factory.StartNew(() => this.SetTablePermissions(table, tablePermissions, requestOptions, operationContext));
202+
}
203+
204+
/// <summary>
205+
/// Return a task that asynchronously fetch table permissions
206+
/// </summary>
207+
/// <param name="table">target table</param>
208+
/// <param name="requestOptions">request options</param>
209+
/// <param name="operationContext">context</param>
210+
/// <returns></returns>
211+
public Task<TablePermissions> GetTablePermissionsAsync(CloudTable table, TableRequestOptions requestOptions, OperationContext operationContext)
212+
{
213+
return Task.Factory.StartNew(() => this.GetTablePermissions(table,
214+
requestOptions, operationContext));
170215
}
171216

172217
public AzureStorageContext StorageContext
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright 2012 Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.WindowsAzure.Commands.Storage.Test.Table
16+
{
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using Microsoft.WindowsAzure.Commands.Storage.Common;
19+
using Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet;
20+
using Microsoft.WindowsAzure.Storage.Table;
21+
using System;
22+
using System.Collections.Generic;
23+
24+
[TestClass]
25+
public class GetAzureStorageTableStoredAccessPolicyTest : StorageTableStorageTestBase
26+
{
27+
public GetAzureStorageTableStoredAccessPolicyCommand command = null;
28+
29+
[TestInitialize]
30+
public void InitCommand()
31+
{
32+
command = new GetAzureStorageTableStoredAccessPolicyCommand(tableMock)
33+
{
34+
CommandRuntime = MockCmdRunTime
35+
};
36+
CurrentTableCmd = command;
37+
}
38+
39+
[TestCleanup]
40+
public void CleanCommand()
41+
{
42+
command = null;
43+
}
44+
45+
[TestMethod]
46+
public void GetStoredAccessPolicyNotExistsTest()
47+
{
48+
AddTestStoredAccessPolicy();
49+
string policyName = "Policy" + Guid.NewGuid();
50+
string tableName = "sampleTable";
51+
52+
MockCmdRunTime.ResetPipelines();
53+
command.Table = tableName;
54+
command.Policy = policyName;
55+
RunAsyncCommand(() => command.ExecuteCmdlet());
56+
57+
Assert.AreEqual(0, MockCmdRunTime.OutputPipeline.Count);
58+
59+
MockCmdRunTime.ResetPipelines();
60+
clearTest();
61+
}
62+
63+
[TestMethod]
64+
public void GetStoredAccessPolicySuccessTest()
65+
{
66+
AddTestStoredAccessPolicy();
67+
string policyName = TestPolicy1;
68+
string tableName = "sampleTable";
69+
70+
MockCmdRunTime.ResetPipelines();
71+
command.Table = tableName;
72+
command.Policy = policyName;
73+
RunAsyncCommand(() => command.ExecuteCmdlet());
74+
75+
Assert.AreEqual(1, MockCmdRunTime.OutputPipeline.Count);
76+
77+
MockCmdRunTime.ResetPipelines();
78+
clearTest();
79+
}
80+
81+
[TestMethod]
82+
public void GetAllStoredAccessPolicySuccessTest()
83+
{
84+
AddTestStoredAccessPolicy();
85+
string tableName = "sampleTable";
86+
87+
MockCmdRunTime.ResetPipelines();
88+
command.Table = tableName;
89+
RunAsyncCommand(() => command.ExecuteCmdlet());
90+
91+
Assert.AreEqual(2, MockCmdRunTime.OutputPipeline.Count);
92+
93+
MockCmdRunTime.ResetPipelines();
94+
clearTest();
95+
}
96+
}
97+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright 2012 Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.WindowsAzure.Commands.Storage.Test.Table
16+
{
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using Microsoft.WindowsAzure.Commands.Storage.Common;
19+
using Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet;
20+
using Microsoft.WindowsAzure.Storage.Table;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Globalization;
24+
25+
[TestClass]
26+
public class NewAzureStorageTableStoredAccessPolicyTest : StorageTableStorageTestBase
27+
{
28+
public NewAzureStorageTableStoredAccessPolicyCommand command = null;
29+
30+
[TestInitialize]
31+
public void InitCommand()
32+
{
33+
command = new NewAzureStorageTableStoredAccessPolicyCommand(tableMock)
34+
{
35+
CommandRuntime = MockCmdRunTime
36+
};
37+
}
38+
39+
[TestCleanup]
40+
public void CleanCommand()
41+
{
42+
command = null;
43+
}
44+
45+
[TestMethod]
46+
public void CreateAzureTableStoredAccessPolicyWithInvalidNameTest()
47+
{
48+
clearTest();
49+
//policy name lenght longer than 64
50+
string policyName = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
51+
string tableName = "sampleTable";
52+
AssertThrows<ArgumentException>(() => command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, null, null, null),
53+
String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, policyName));
54+
clearTest();
55+
}
56+
57+
58+
[TestMethod]
59+
public void CreateAzureTableStoredAccessPolicySuccessTest()
60+
{
61+
clearTest();
62+
string policyName = "Policy" + Guid.NewGuid();
63+
string tableName = "sampleTable";
64+
65+
string permission = "rd";
66+
DateTime startTime = DateTime.Today;
67+
DateTime expiryTime = DateTime.Today.AddDays(1);
68+
command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, startTime, expiryTime, permission);
69+
70+
SharedAccessTablePermissions expectedPermissions = SharedAccessTablePermissions.None;
71+
expectedPermissions |= SharedAccessTablePermissions.Query;
72+
expectedPermissions |= SharedAccessTablePermissions.Delete;
73+
SharedAccessTablePolicy resultPolicy = tableMock.tablePermissions.SharedAccessPolicies[policyName];
74+
Assert.IsNotNull(resultPolicy);
75+
Assert.AreEqual<SharedAccessTablePermissions>(expectedPermissions, resultPolicy.Permissions);
76+
Assert.AreEqual<DateTimeOffset?>(startTime.ToUniversalTime(), resultPolicy.SharedAccessStartTime);
77+
Assert.AreEqual<DateTimeOffset?>(expiryTime.ToUniversalTime(), resultPolicy.SharedAccessExpiryTime);
78+
clearTest();
79+
}
80+
81+
82+
[TestMethod]
83+
public void CreateStoredAccessPolicyAlreadyExistsTest()
84+
{
85+
clearTest();
86+
string policyName = "Policy" + Guid.NewGuid();
87+
string tableName = "sampleTable";
88+
89+
command.Table = tableName;
90+
command.Policy = policyName;
91+
command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, null, null, null);
92+
AssertThrows<ResourceAlreadyExistException>(() => command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, null, null, null),
93+
String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, policyName));
94+
clearTest();
95+
}
96+
97+
[TestMethod]
98+
public void CreateStoredAccessPolicyInvalidExpirtTime()
99+
{
100+
clearTest();
101+
string policyName = "Policy" + Guid.NewGuid();
102+
string tableName = "sampleTable";
103+
104+
DateTime? startTime = DateTime.Today;
105+
DateTime? expiryTime = DateTime.Today.AddDays(-1);
106+
DateTimeOffset? expectedSharedAccessStartTime = startTime.Value.ToUniversalTime();
107+
DateTimeOffset? expectedSharedAccessExpiryTime = expiryTime.Value.ToUniversalTime();
108+
AssertThrows<ArgumentException>(() => command.CreateAzureTableStoredAccessPolicy(command.Channel, tableName, policyName, startTime, expiryTime, null),
109+
String.Format(CultureInfo.CurrentCulture, Resources.ExpiryTimeGreatThanStartTime, expectedSharedAccessExpiryTime, expectedSharedAccessStartTime));
110+
clearTest();
111+
}
112+
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright 2012 Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.WindowsAzure.Commands.Storage.Test.Table
16+
{
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using Microsoft.WindowsAzure.Commands.Storage.Common;
19+
using Microsoft.WindowsAzure.Commands.Storage.Table.Cmdlet;
20+
using Microsoft.WindowsAzure.Storage.Table;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Globalization;
24+
using System.Linq;
25+
26+
[TestClass]
27+
public class RemoveAzureStorageTableStoredAccessPolicyTest : StorageTableStorageTestBase
28+
{
29+
public RemoveAzureStorageTableStoredAccessPolicyCommand command = null;
30+
31+
[TestInitialize]
32+
public void InitCommand()
33+
{
34+
command = new RemoveAzureStorageTableStoredAccessPolicyCommand(tableMock)
35+
{
36+
CommandRuntime = MockCmdRunTime
37+
};
38+
}
39+
40+
[TestCleanup]
41+
public void CleanCommand()
42+
{
43+
command = null;
44+
}
45+
46+
[TestMethod]
47+
public void RemoveStoredAccessPolicyNotExistsTest()
48+
{
49+
AddTestStoredAccessPolicy();
50+
string policyName = "Policy" + Guid.NewGuid();
51+
string tableName = "sampleTable";
52+
53+
AssertThrows<ResourceNotFoundException>(() => command.RemoveAzureTableStoredAccessPolicy(tableMock, tableName, policyName),
54+
String.Format(CultureInfo.CurrentCulture, Resources.PolicyNotFound, policyName));
55+
clearTest();
56+
}
57+
58+
[TestMethod]
59+
public void RemoveStoredAccessPolicySuccessTest()
60+
{
61+
AddTestStoredAccessPolicy();
62+
string policyName = TestPolicy1;
63+
string tableName = "sampleTable";
64+
65+
command.RemoveAzureTableStoredAccessPolicy(tableMock, tableName, policyName);
66+
Assert.IsTrue(!tableMock.tablePermissions.SharedAccessPolicies.Keys.Contains(policyName));
67+
68+
clearTest();
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)