Skip to content

Commit

Permalink
Merge pull request #28 from FrendsPlatform/issue-17
Browse files Browse the repository at this point in the history
Frends.Files.Delete Initial implementation
  • Loading branch information
Svenskapojkarna authored Feb 16, 2023
2 parents 6c9c7d5 + b092b15 commit 54b4ab1
Show file tree
Hide file tree
Showing 18 changed files with 677 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/Delete_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Delete_build_main

on:
push:
branches:
- main
paths:
- 'Frends.Files.Delete/**'
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/build_main.yml@main
with:
workdir: Frends.Files.Delete
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
18 changes: 18 additions & 0 deletions .github/workflows/Delete_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Delete_build_test

on:
push:
branches-ignore:
- main
paths:
- 'Frends.Files.Delete/**'
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/build_test.yml@main
with:
workdir: Frends.Files.Delete
secrets:
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }}
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }}
12 changes: 12 additions & 0 deletions .github/workflows/Delete_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Delete_release

on:
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/release.yml@main
with:
workdir: Frends.Files.Delete
secrets:
feed_api_key: ${{ secrets.TASKS_FEED_API_KEY }}
5 changes: 5 additions & 0 deletions Frends.Files.Delete/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [1.0.0] - 2023-02-13
### Added
- Initial implementation
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Frends.Files.Delete\Frends.Files.Delete.csproj" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions Frends.Files.Delete/Frends.Files.Delete.Tests/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.DirectoryServices;
using System.Runtime.InteropServices;

namespace Frends.Files.Delete.Tests;

internal class Helper
{
public static void CreateTestFiles(string directory)
{
if (!File.Exists(directory))
Directory.CreateDirectory(directory);

var list = new List<string>
{
Path.Combine(directory, "Test1.txt"),
Path.Combine(directory, "Test2.txt"),
Path.Combine(directory, "Test1.xml"),
Path.Combine(directory, "pro_test.txt"),
Path.Combine(directory, "pref_test.txt"),
Path.Combine(directory, "_test.txt"),
Path.Combine(directory, "prof_test.txt"),
};

// Create test files and edit creation date
foreach (var path in list)
{
File.WriteAllText(path, $"Test {path}");
}
}

public static void DeleteTestFolder(string directory)
{
Directory.Delete(directory, true);
}

public static void CreateTestUser(string domain, string name, string pwd)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new PlatformNotSupportedException("UseGivenCredentials feature is only supported on Windows.");

DirectoryEntry AD = new DirectoryEntry("WinNT://" + domain + ",computer");
DirectoryEntry NewUser = AD.Children.Add(name, "user");
NewUser.Invoke("SetPassword", new object[] { pwd });
NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
NewUser.CommitChanges();
DirectoryEntry grp;

grp = AD.Children.Find("Administrators", "group");
if (grp != null)
grp.Invoke("Add", new object[] { NewUser.Path.ToString() });
}

public static void DeleteTestUser(string name)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new PlatformNotSupportedException("UseGivenCredentials feature is only supported on Windows.");

DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
DirectoryEntries users = localDirectory.Children;
DirectoryEntry user = users.Find(name);
users.Remove(user);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Frends.Files.Delete.Definitions;
using NUnit.Framework;
using System;
using System.IO;

namespace Frends.Files.Delete.Tests;

[TestFixture]
class ImpersonationTests
{
/// <summary>
/// Impersonation tests needs to be run as administrator so that the OneTimeSetup can create a local test user. Impersonation tests can only be run in Windows OS.
/// </summary>
private readonly string _dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../TestData/");
Input? _input;
Options? _options;

private readonly string _domain = Environment.MachineName;
private readonly string _name = "test";
private readonly string _pwd = "pas5woRd!";


[OneTimeSetUp]
public void OneTimeSetup()
{
Helper.CreateTestUser(_domain, _name, _pwd);

_input = new Input
{
Directory = _dir,
Pattern = "*"
};

_options = new Options
{
UseGivenUserCredentialsForRemoteConnections = true,
UserName = $"{_domain}\\{_name}",
Password = _pwd
};
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
Helper.DeleteTestUser(_name);
}

[SetUp]
public void Setup()
{
Helper.CreateTestFiles(_dir);
}

[TearDown]
public void TearDown()
{
Helper.DeleteTestFolder(_dir);
}

[Test]
public void FileDeleteTestWithCredentials()
{
var result = Files.Delete(
_input,
_options, default);

Assert.AreEqual(7, result.Files.Count);
Assert.IsFalse(File.Exists(result.Files[0].Path));
}

[Test]
public void FileDeleteTestWithUsernameWithoutDomain()
{
var options = new Options
{
UseGivenUserCredentialsForRemoteConnections = true,
UserName = "test",
Password = _pwd
};

var ex = Assert.Throws<ArgumentException>(() => Files.Delete(_input, options, default));
Assert.AreEqual($@"UserName field must be of format domain\username was: {options.UserName}", ex.Message);
}
}
88 changes: 88 additions & 0 deletions Frends.Files.Delete/Frends.Files.Delete.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Frends.Files.Delete.Definitions;
using NUnit.Framework;
using System;
using System.IO;

namespace Frends.Files.Delete.Tests;

[TestFixture]
public class UnitTests
{
private readonly string _dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../TestData/");
Input? _input;
Options? _options;

[SetUp]
public void Setup()
{
Helper.CreateTestFiles(_dir);

_input = new Input
{
Directory = _dir,
Pattern = "*"
};

_options = new Options
{
UseGivenUserCredentialsForRemoteConnections = false
};
}

[TearDown]
public void TearDown()
{
Helper.DeleteTestFolder(_dir);
}

[Test]
public void FileDeleteAll()
{
var result = Files.Delete(_input, _options, default);

Assert.AreEqual(7, result.Files.Count);
Assert.IsFalse(File.Exists(result.Files[0].Path));
}

[Test]
public void FileDeleteWithPattern()
{
var result = Files.Delete(
new Input
{
Directory = _dir,
Pattern = "Test1*"
}, _options, default);

Assert.AreEqual(2, result.Files.Count);
Assert.IsFalse(File.Exists(result.Files[0].Path));
}

[Test]
public void FileDeleteShouldNotThrowIfNoFilesFound()
{
var result = Files.Delete(
new Input()
{
Directory = _dir,
Pattern = "**/*.unknown"
},
_options,
default);

Assert.IsEmpty(result.Files);
}

[Test]
public void FileDeleteShouldThrowIfDirectoryIsNotFound()
{
var input = new Input()
{
Directory = @"F:\directory\that\dont\exists",
Pattern = "**/*.unknown"
};

var ex = Assert.Throws<DirectoryNotFoundException>(() => Files.Delete(input, _options, default));
Assert.AreEqual($"Directory does not exist or you do not have read access. Tried to access directory '{input.Directory}'", ex.Message);
}
}
40 changes: 40 additions & 0 deletions Frends.Files.Delete/Frends.Files.Delete.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32319.34
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frends.Files.Delete", "Frends.Files.Delete\Frends.Files.Delete.csproj", "{35C305C0-8108-4A98-BB1D-AFE5C926239E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{78F7F22E-6E20-4BCE-8362-0C558568B729}"
ProjectSection(SolutionItems) = preProject
CHANGELOG.md = CHANGELOG.md
README.md = README.md
..\.github\workflows\Delete_build_and_test_on_main.yml = ..\.github\workflows\Delete_build_and_test_on_main.yml
..\.github\workflows\Delete_build_and_test_on_push.yml = ..\.github\workflows\Delete_build_and_test_on_push.yml
..\.github\workflows\Delete_release.yml = ..\.github\workflows\Delete_release.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Frends.Files.Delete.Tests", "Frends.Files.Delete.Tests\Frends.Files.Delete.Tests.csproj", "{92A4DC02-5B1A-4C0C-A0F2-6046CA601FE5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.Build.0 = Release|Any CPU
{92A4DC02-5B1A-4C0C-A0F2-6046CA601FE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92A4DC02-5B1A-4C0C-A0F2-6046CA601FE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92A4DC02-5B1A-4C0C-A0F2-6046CA601FE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92A4DC02-5B1A-4C0C-A0F2-6046CA601FE5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55BC6629-85C9-48D8-8CA2-B0046AF1AF4B}
EndGlobalSection
EndGlobal
32 changes: 32 additions & 0 deletions Frends.Files.Delete/Frends.Files.Delete/Definitions/FileItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;

namespace Frends.Files.Delete.Definitions;

/// <summary>
/// Class for return items.
/// </summary>
public class FileItem
{
/// <summary>
/// Name of the deleted file.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Full path of the deleted file.
/// </summary>
public string Path { get; set; }

/// <summary>
/// Size of the deleted file in mega bytes.
/// </summary>
public double SizeInMegaBytes { get; set; }

internal FileItem(FileInfo file)
{
Name = file.Name;
Path = file.FullName;
SizeInMegaBytes = file.Length / 1024d / 1024d;
}
}

Loading

0 comments on commit 54b4ab1

Please sign in to comment.