public void RetrieveDenialsForDirectory_Root_Group() { var mocks = new MockRepository(); var prov = mocks.DynamicMock <ISettingsStorageProviderV30>(); var filesProv = mocks.DynamicMock <IFilesStorageProviderV30>(); var aclManager = mocks.DynamicMock <IAclManager>(); Expect.Call(prov.AclManager).Return(aclManager).Repeat.Any(); var dirName = Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(filesProv, "/"); Expect.Call(aclManager.RetrieveEntriesForSubject("G.Group")).Return( new[] { new AclEntry(dirName, Actions.ForDirectories.List, "G.Group", Value.Deny), new AclEntry(dirName, Actions.FullControl, "G.Group", Value.Grant), new AclEntry("D." + AuthTools.GetDirectoryName(filesProv, "/Other/"), Actions.ForDirectories.UploadFiles, "G.Group", Value.Deny) }); mocks.Replay(prov); mocks.Replay(aclManager); Collectors.SettingsProvider = prov; var grants = AuthReader.RetrieveDenialsForDirectory(new UserGroup("Group", "Group", null), filesProv, "/"); Assert.AreEqual(1, grants.Length, "Wrong denial count"); Assert.AreEqual(Actions.ForDirectories.List, grants[0], "Wrong denial"); }
public void RetrieveGrantsForDirectory_Sub_User() { var mocks = new MockRepository(); var prov = mocks.DynamicMock <ISettingsStorageProviderV30>(); var filesProv = mocks.DynamicMock <IFilesStorageProviderV30>(); var aclManager = mocks.DynamicMock <IAclManager>(); Expect.Call(prov.AclManager).Return(aclManager).Repeat.Any(); var dirName = Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(filesProv, "/Dir/Sub/"); Expect.Call(aclManager.RetrieveEntriesForSubject("U.User")).Return( new[] { new AclEntry(dirName, Actions.ForDirectories.UploadFiles, "U.User", Value.Grant), new AclEntry(dirName, Actions.FullControl, "U.User", Value.Deny), new AclEntry("D." + AuthTools.GetDirectoryName(filesProv, "/"), Actions.ForDirectories.List, "U.User", Value.Grant) }); mocks.Replay(prov); mocks.Replay(aclManager); Collectors.SettingsProvider = prov; var grants = AuthReader.RetrieveGrantsForDirectory( new UserInfo("User", "User", "*****@*****.**", true, DateTime.Now, null), filesProv, "/Dir/Sub/"); Assert.AreEqual(1, grants.Length, "Wrong grant count"); Assert.AreEqual(Actions.ForDirectories.UploadFiles, grants[0], "Wrong grant"); }
public void RetrieveSubjectsForDirectory_Sub() { var mocks = new MockRepository(); var prov = mocks.DynamicMock <ISettingsStorageProviderV30>(); var filesProv = mocks.DynamicMock <IFilesStorageProviderV30>(); var aclManager = mocks.DynamicMock <IAclManager>(); Expect.Call(prov.AclManager).Return(aclManager).Repeat.Any(); var dirName = Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(filesProv, "/Dir/Sub/"); Expect.Call(aclManager.RetrieveEntriesForResource(dirName)).Return( new[] { new AclEntry(dirName, Actions.ForDirectories.List, "U.User1", Value.Grant), new AclEntry(dirName, Actions.ForDirectories.UploadFiles, "U.User", Value.Grant), new AclEntry(dirName, Actions.ForDirectories.DownloadFiles, "G.Group", Value.Grant), new AclEntry(dirName, Actions.ForDirectories.CreateDirectories, "U.User", Value.Deny) }); mocks.Replay(prov); mocks.Replay(aclManager); Collectors.SettingsProvider = prov; var infos = AuthReader.RetrieveSubjectsForDirectory(filesProv, "/Dir/Sub/"); Assert.AreEqual(3, infos.Length, "Wrong info count"); Array.Sort(infos, delegate(SubjectInfo x, SubjectInfo y) { return(x.Name.CompareTo(y.Name)); }); Assert.AreEqual("Group", infos[0].Name, "Wrong subject name"); Assert.AreEqual(SubjectType.Group, infos[0].Type, "Wrong subject type"); Assert.AreEqual("User", infos[1].Name, "Wrong subject name"); Assert.AreEqual(SubjectType.User, infos[1].Type, "Wrong subject type"); Assert.AreEqual("User1", infos[2].Name, "Wrong subject name"); Assert.AreEqual(SubjectType.User, infos[2].Type, "Wrong subject type"); mocks.Verify(prov); mocks.Verify(aclManager); }
public void MigrateFilesStorageProviderData() { var mocks = new MockRepository(); var source = mocks.StrictMock <IFilesStorageProviderV30>(); var destination = mocks.StrictMock <IFilesStorageProviderV30>(); var settingsProvider = mocks.StrictMock <ISettingsStorageProviderV30>(); var aclManager = mocks.StrictMock <IAclManager>(); Expect.Call(settingsProvider.AclManager).Return(aclManager).Repeat.Any(); // Setup SOURCE ----------------- // Directories Expect.Call(source.ListDirectories("/")).Return(new[] { "/Dir1/", "/Dir2/" }); Expect.Call(source.ListDirectories("/Dir1/")).Return(new[] { "/Dir1/Sub/" }); Expect.Call(source.ListDirectories("/Dir2/")).Return(new string[0]); Expect.Call(source.ListDirectories("/Dir1/Sub/")).Return(new string[0]); // Settings (permissions) Expect.Call(aclManager.RenameResource( Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(source, "/"), Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(destination, "/"))) .Return(true); Expect.Call(aclManager.RenameResource( Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(source, "/Dir1/"), Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(destination, "/Dir1/"))) .Return(true); Expect.Call(aclManager.RenameResource( Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(source, "/Dir2/"), Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(destination, "/Dir2/"))) .Return(true); Expect.Call(aclManager.RenameResource( Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(source, "/Dir1/Sub/"), Actions.ForDirectories.ResourceMasterPrefix + AuthTools.GetDirectoryName(destination, "/Dir1/Sub/"))) .Return(true); // Filenames Expect.Call(source.ListFiles("/")).Return(new[] { "/File1.txt", "/File2.txt" }); Expect.Call(source.ListFiles("/Dir1/")).Return(new[] { "/Dir1/File.txt" }); Expect.Call(source.ListFiles("/Dir2/")).Return(new string[0]); Expect.Call(source.ListFiles("/Dir1/Sub/")).Return(new[] { "/Dir1/Sub/File.txt" }); // File content Expect.Call(source.RetrieveFile("/File1.txt", null, false)).Constraints( RMC.Is.Equal("/File1.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do( new RetrieveFile( delegate(string file, Stream stream, bool count) { var stuff = Encoding.Unicode.GetBytes("content1"); stream.Write(stuff, 0, stuff.Length); return(true); })); Expect.Call(source.RetrieveFile("/File2.txt", null, false)).Constraints( RMC.Is.Equal("/File2.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do( new RetrieveFile( delegate(string file, Stream stream, bool count) { var stuff = Encoding.Unicode.GetBytes("content2"); stream.Write(stuff, 0, stuff.Length); return(true); })); Expect.Call(source.RetrieveFile("/Dir1/File.txt", null, false)).Constraints( RMC.Is.Equal("/Dir1/File.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do( new RetrieveFile( delegate(string file, Stream stream, bool count) { var stuff = Encoding.Unicode.GetBytes("content3"); stream.Write(stuff, 0, stuff.Length); return(true); })); Expect.Call(source.RetrieveFile("/Dir1/Sub/File.txt", null, false)).Constraints( RMC.Is.Equal("/Dir1/Sub/File.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do( new RetrieveFile( delegate(string file, Stream stream, bool count) { var stuff = Encoding.Unicode.GetBytes("content4"); stream.Write(stuff, 0, stuff.Length); return(true); })); // File details Expect.Call(source.GetFileDetails("/File1.txt")).Return(new FileDetails(8, DateTime.Now, 52)); Expect.Call(source.GetFileDetails("/File2.txt")).Return(new FileDetails(8, DateTime.Now, 0)); Expect.Call(source.GetFileDetails("/Dir1/File.txt")).Return(new FileDetails(8, DateTime.Now, 21)); Expect.Call(source.GetFileDetails("/Dir1/Sub/File.txt")).Return(new FileDetails(8, DateTime.Now, 123)); // Page attachments Expect.Call(source.GetPagesWithAttachments()).Return(new[] { "MainPage", "Sub.Page", "Sub.Another" }); Expect.Call(source.ListPageAttachments(null)) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "MainPage"); })) .Return(new[] { "Attachment.txt" }); Expect.Call(source.ListPageAttachments(null)) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Page"); })) .Return(new[] { "Attachment2.txt" }); Expect.Call(source.ListPageAttachments(null)) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Another"); })) .Return(new string[0]); // Page attachment content Expect.Call(source.RetrievePageAttachment(null, "Attachment.txt", null, false)).Constraints( RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "MainPage"); }), RMC.Is.Equal("Attachment.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do( new RetrieveAttachment( delegate(PageInfo page, string name, Stream stream, bool count) { var stuff = Encoding.Unicode.GetBytes("content5"); stream.Write(stuff, 0, stuff.Length); return(true); })); Expect.Call(source.RetrievePageAttachment(null, "Attachment2.txt", null, false)).Constraints( RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Page"); }), RMC.Is.Equal("Attachment2.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do( new RetrieveAttachment( delegate(PageInfo page, string name, Stream stream, bool count) { var stuff = Encoding.Unicode.GetBytes("content6"); stream.Write(stuff, 0, stuff.Length); return(true); })); // Attachment details Expect.Call(source.GetPageAttachmentDetails(null, "Attachment.txt")).Constraints( RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "MainPage"); }), RMC.Is.Equal("Attachment.txt")).Return(new FileDetails(8, DateTime.Now, 8)); Expect.Call(source.GetPageAttachmentDetails(null, "Attachment2.txt")).Constraints( RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Page"); }), RMC.Is.Equal("Attachment2.txt")).Return(new FileDetails(8, DateTime.Now, 29)); // Setup DESTINATION ------------------------ // Directories Expect.Call(destination.CreateDirectory("/", "Dir1")).Return(true); Expect.Call(destination.CreateDirectory("/", "Dir2")).Return(true); Expect.Call(destination.CreateDirectory("/Dir1/", "Sub")).Return(true); // Files Expect.Call(destination.StoreFile("/File1.txt", null, false)).Constraints( RMC.Is.Equal("/File1.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do(new StoreFile( delegate(string name, Stream stream, bool overwrite) { var buff = new byte[512]; var read = stream.Read(buff, 0, (int)stream.Length); Assert.AreEqual("content1", Encoding.Unicode.GetString(buff, 0, read), "Wrong data"); return(true); })); Expect.Call(destination.StoreFile("/File2.txt", null, false)).Constraints( RMC.Is.Equal("/File2.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do(new StoreFile( delegate(string name, Stream stream, bool overwrite) { var buff = new byte[512]; var read = stream.Read(buff, 0, (int)stream.Length); Assert.AreEqual("content2", Encoding.Unicode.GetString(buff, 0, read), "Wrong data"); return(true); })); Expect.Call(destination.StoreFile("/Dir1/File.txt", null, false)).Constraints( RMC.Is.Equal("/Dir1/File.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do(new StoreFile( delegate(string name, Stream stream, bool overwrite) { var buff = new byte[512]; var read = stream.Read(buff, 0, (int)stream.Length); Assert.AreEqual("content3", Encoding.Unicode.GetString(buff, 0, read), "Wrong data"); return(true); })); Expect.Call(destination.StoreFile("/Dir1/Sub/File.txt", null, false)).Constraints( RMC.Is.Equal("/Dir1/Sub/File.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do(new StoreFile( delegate(string name, Stream stream, bool overwrite) { var buff = new byte[512]; var read = stream.Read(buff, 0, (int)stream.Length); Assert.AreEqual("content4", Encoding.Unicode.GetString(buff, 0, read), "Wrong data"); return(true); })); // File retrieval count destination.SetFileRetrievalCount("/File1.txt", 52); LastCall.On(destination).Repeat.Once(); destination.SetFileRetrievalCount("/File2.txt", 0); LastCall.On(destination).Repeat.Once(); destination.SetFileRetrievalCount("/Dir1/File.txt", 21); LastCall.On(destination).Repeat.Once(); destination.SetFileRetrievalCount("/Dir1/Sub/File.txt", 123); LastCall.On(destination).Repeat.Once(); // Page attachments Expect.Call(destination.StorePageAttachment(null, "Attachment.txt", null, false)).Constraints( RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "MainPage"); }), RMC.Is.Equal("Attachment.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do(new StoreAttachment( delegate(PageInfo page, string name, Stream stream, bool overwrite) { var buff = new byte[512]; var read = stream.Read(buff, 0, (int)stream.Length); Assert.AreEqual("content5", Encoding.Unicode.GetString(buff, 0, read), "Wrong data"); return(true); })); Expect.Call(destination.StorePageAttachment(null, "Attachment2.txt", null, false)).Constraints( RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Page"); }), RMC.Is.Equal("Attachment2.txt"), RMC.Is.TypeOf <Stream>(), RMC.Is.Equal(false)).Do(new StoreAttachment( delegate(PageInfo page, string name, Stream stream, bool overwrite) { var buff = new byte[512]; var read = stream.Read(buff, 0, (int)stream.Length); Assert.AreEqual("content6", Encoding.Unicode.GetString(buff, 0, read), "Wrong data"); return(true); })); // Attachment retrieval count destination.SetPageAttachmentRetrievalCount(null, "Attachment.txt", 8); LastCall.On(destination) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "MainPage"); }), RMC.Is.Equal("Attachment.txt"), RMC.Is.Equal(8)) .Repeat.Once(); destination.SetPageAttachmentRetrievalCount(null, "Attachment2.txt", 29); LastCall.On(destination) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Page"); }), RMC.Is.Equal("Attachment2.txt"), RMC.Is.Equal(29)) .Repeat.Once(); // Delete source content Expect.Call(source.DeleteFile("/File1.txt")).Return(true); Expect.Call(source.DeleteFile("/File2.txt")).Return(true); Expect.Call(source.DeleteDirectory("/Dir1/")).Return(true); Expect.Call(source.DeleteDirectory("/Dir2/")).Return(true); Expect.Call(source.DeletePageAttachment(null, "Attachment.aspx")) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "MainPage"); }), RMC.Is.Equal("Attachment.txt")) .Return(true); Expect.Call(source.DeletePageAttachment(null, "Attachment2.aspx")) .Constraints(RMC.Is.Matching(delegate(PageInfo p) { return(p.FullName == "Sub.Page"); }), RMC.Is.Equal("Attachment2.txt")) .Return(true); mocks.Replay(source); mocks.Replay(destination); mocks.Replay(settingsProvider); mocks.Replay(aclManager); DataMigrator.MigrateFilesStorageProviderData(source, destination, settingsProvider); mocks.Verify(source); mocks.Verify(destination); mocks.Verify(settingsProvider); mocks.Verify(aclManager); }