Commit 2d3564c5 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs

New: Delete functionality for Projects and Resources and additional fields

Merge branch 'Product/243-addEditProject' into 'master'

See merge request !20
parents 4990362b ea903e3a
using Coscine.Api.Project.Models;
using Coscine.Api.Project.Controllers;
using Coscine.Api.Project.Models;
using Coscine.ApiCommons.Utils;
using Coscine.Database.Model;
using Microsoft.AspNetCore.Http;
......@@ -24,6 +25,13 @@ namespace Coscine.Api.Project.Tests
protected readonly List<ProjectRole> ProjectRoles = new List<ProjectRole>();
protected readonly List<ResourceType> ResourceTypes = new List<ResourceType>();
protected readonly List<Resource> Resources = new List<Resource>();
protected readonly List<ResourceDiscipline> ResourceDisciplines = new List<ResourceDiscipline>();
protected Discipline Discipline { get; set; }
protected Institute Institute { get; set; }
protected Visibility Visibility { get; set; }
protected License License { get; set; }
protected RDSResourceType RdsResourceType { get; set; }
protected int Previous;
......@@ -35,6 +43,32 @@ namespace Coscine.Api.Project.Tests
[OneTimeSetUp]
public void Setup()
{
DisciplineModel disciplineModel = new DisciplineModel();
Discipline = new Discipline()
{
DisplayName = "TestDiscipline",
Url = "http://rwth-aachen.de"
};
disciplineModel.Insert(Discipline);
InstituteModel instituteModel = new InstituteModel();
Institute = new Institute()
{
DisplayName = "TestInstitute",
IKZ = "022000"
};
instituteModel.Insert(Institute);
VisibilityModel visibilityModel = new VisibilityModel();
Visibility = visibilityModel.GetWhere((visibility) => visibility.DisplayName == "Public");
LicenseModel licenseModel = new LicenseModel();
License = new License()
{
DisplayName = "MIT"
};
licenseModel.Insert(License);
ProjectModel projectModel = new ProjectModel();
Previous = projectModel.GetAll().ToArray().Length;
......@@ -54,63 +88,120 @@ namespace Coscine.Api.Project.Tests
var project = new Coscine.Database.Model.Project()
{
Description = "Description",
Organization = "Organization",
ProjectName = "TestProject",
DisplayName = "TestProject",
StartDate = DateTime.Now,
EndDate = DateTime.Now.AddYears(1),
Keywords = "Test1;Test2"
Keywords = "Test1;Test2",
GrantId = "testid",
PrincipleInvestigators = "TestInvestigator",
VisibilityId = Visibility.Id
};
projectModel.Insert(project);
var projectRole = projectModel.SetOwner(project, user);
ProjectRoles.Add(projectRole);
ProjectDisciplineModel projectDisciplineModel = new ProjectDisciplineModel();
projectDisciplineModel.Insert(new ProjectDiscipline()
{
DisciplineId = Discipline.Id,
ProjectId = project.Id
});
ProjectInstituteModel projectInstituteModel = new ProjectInstituteModel();
projectInstituteModel.Insert(new ProjectInstitute()
{
InstituteId = Institute.Id,
ProjectId = project.Id
});
Projects.Add(projectModel.GetById(project.Id));
var project2 = new Coscine.Database.Model.Project()
{
Description = "Description2",
Organization = "Organization2",
ProjectName = "TestProject",
DisplayName = "TestProject2",
StartDate = DateTime.Now,
EndDate = DateTime.Now.AddYears(1),
Keywords = "Test1;Test2"
Keywords = "Test1;Test2",
GrantId = "testid",
PrincipleInvestigators = "TestInvestigator",
VisibilityId = Visibility.Id
};
projectModel.Insert(project2);
Projects.Add(projectModel.GetById(project2.Id));
ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
var resourceType = new ResourceType()
var resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "rds");
RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
RdsResourceType = new RDSResourceType()
{
DisplayName = "TestType"
AccessKey = "a",
SecretKey = "b",
BucketName = "c",
};
resourceTypeModel.Insert(resourceType);
ResourceTypes.Add(resourceType);
rdsResourceTypeModel.Insert(RdsResourceType);
ResourceModel resourceModel = new ResourceModel();
var resource = new Resource()
{
ExternalId = "123",
Url = "http://rwth-aachen.de/test",
DisplayName = "ResourceTest1",
ResourceName = "ResourceTest1",
Keywords = "ResourceTest1",
UsageRights = "ResourceTest1",
TypeId = resourceType.Id,
Type = resourceType
Type = resourceType,
Visibility = Visibility,
VisibilityId = Visibility.Id,
LicenseId = License.Id,
ResourceTypeOptionId = RdsResourceType.Id
};
resourceModel.Insert(resource);
projectModel.AddResource(project, resource);
Resources.Add(resource);
resourceType = resourceTypeModel.GetWhere((dbResourceType) => dbResourceType.DisplayName == "gitlab");
var resource2 = new Resource()
{
ExternalId = "12345",
Url = "http://rwth-aachen.de/test2",
DisplayName = "ResourceTest2",
ResourceName = "ResourceTest2",
Keywords = "ResourceTest2",
UsageRights = "ResourceTest2",
TypeId = resourceType.Id,
Type = resourceType
Type = resourceType,
Visibility = Visibility,
VisibilityId = Visibility.Id,
LicenseId = License.Id
};
resourceModel.Insert(resource2);
projectModel.AddResource(project2, resource2);
Resources.Add(resource2);
ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel();
ResourceDiscipline resourceDiscipline = new ResourceDiscipline()
{
DisciplineId = Discipline.Id,
ResourceId = resource.Id
};
resourceDisciplineModel.Insert(resourceDiscipline);
ResourceDisciplines.Add(resourceDiscipline);
resourceDiscipline = new ResourceDiscipline()
{
DisciplineId = Discipline.Id,
ResourceId = resource2.Id
};
resourceDisciplineModel.Insert(resourceDiscipline);
ResourceDisciplines.Add(resourceDiscipline);
}
......@@ -193,26 +284,43 @@ namespace Coscine.Api.Project.Tests
{
projectResourceModel.Delete(projectResource);
}
ProjectModel projectModel = new ProjectModel();
ProjectController projectController = new ProjectController();
foreach (var project in Projects)
{
projectModel.Delete(project);
projectController.DeleteProject(project);
}
UserModel userModel = new UserModel();
foreach (var user in Users)
{
userModel.Delete(user);
}
ResourceDisciplineModel resourceDisciplineModel = new ResourceDisciplineModel();
foreach (var resourceDiscipline in ResourceDisciplines)
{
resourceDisciplineModel.Delete(resourceDiscipline);
}
ResourceModel resourceModel = new ResourceModel();
foreach (var resource in Resources)
{
resourceModel.Delete(resource);
resourceModel.DeleteResource(resource);
}
ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
foreach (var resourceType in ResourceTypes)
{
resourceTypeModel.Delete(resourceType);
}
DisciplineModel disciplineModel = new DisciplineModel();
disciplineModel.Delete(Discipline);
InstituteModel instituteModel = new InstituteModel();
instituteModel.Delete(Institute);
LicenseModel licenseModel = new LicenseModel();
licenseModel.Delete(License);
RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
rdsResourceTypeModel.Delete(RdsResourceType);
}
}
}
\ No newline at end of file
......@@ -64,8 +64,8 @@
<Reference Include="Coscine.Configuration, Version=1.4.0.0, Culture=neutral, PublicKeyToken=ce3d7a32d7dc1e5a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Configuration.1.4.0\lib\net461\Coscine.Configuration.dll</HintPath>
</Reference>
<Reference Include="Coscine.Database, Version=1.5.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Database.1.5.0\lib\net461\Coscine.Database.dll</HintPath>
<Reference Include="Coscine.Database, Version=1.6.0.0, Culture=neutral, PublicKeyToken=767d77427707b70a, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.Database.1.6.0\lib\net461\Coscine.Database.dll</HintPath>
</Reference>
<Reference Include="Coscine.ProxyApi, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Coscine.ProxyApi.1.2.0\lib\net461\Coscine.ProxyApi.dll</HintPath>
......@@ -565,6 +565,7 @@
<Compile Include="ProjectControllerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceControllerTests.cs" />
<Compile Include="ResourceTypeControllerTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
......
This diff is collapsed.
......@@ -4,8 +4,10 @@ using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
namespace Coscine.Api.Project.Tests
......@@ -33,13 +35,13 @@ namespace Coscine.Api.Project.Tests
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
Assert.IsTrue(okObjectResult.Value.GetType() == typeof(ResourceObject));
ResourceObject projectObject = (ResourceObject)okObjectResult.Value;
ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;
Assert.IsTrue(projectObject.Id == Resources[0].Id);
Assert.IsTrue(projectObject.ExternalId == Resources[0].ExternalId);
Assert.IsTrue(projectObject.Type.Id == Resources[0].Type.Id);
Assert.IsTrue(projectObject.Type.DisplayName == Resources[0].Type.DisplayName);
Assert.IsTrue(projectObject.Url == Resources[0].Url);
Assert.IsTrue(resourceObject.Id == Resources[0].Id);
Assert.IsTrue(resourceObject.DisplayName == Resources[0].DisplayName);
Assert.IsTrue(resourceObject.Visibility.DisplayName == Resources[0].Visibility.DisplayName);
Assert.IsTrue(resourceObject.Type.Id == Resources[0].Type.Id);
Assert.IsTrue(resourceObject.Type.DisplayName == Resources[0].Type.DisplayName);
}
......@@ -50,7 +52,8 @@ namespace Coscine.Api.Project.Tests
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
ResourceObject resourceObject = (ResourceObject)okObjectResult.Value;
resourceObject.ExternalId = "ChangedExternalId";
resourceObject.DisplayName = "OtherName";
resourceObject.ResourceTypeOption = JObject.FromObject(new RDSResourceTypeObject(Guid.NewGuid(), "PITLABTTEST", "accesskey", "secretkey"));
Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
......@@ -83,7 +86,18 @@ namespace Coscine.Api.Project.Tests
[Test]
public void TestControllerStoreToProject()
{
ResourceObject resourceObject = new ResourceObject(Guid.NewGuid(), "externalId", "http://rwth-aachen.de/newTest", "TestResource", new ResourceTypeObject(ResourceTypes[0].Id, ResourceTypes[0].DisplayName));
ResourceObject resourceObject = new ResourceObject(
Guid.NewGuid(),
"Test",
"testR",
"keys",
"usageR",
new ResourceTypeObject(Resources[0].Type.Id, Resources[0].Type.DisplayName),
new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayName) },
new VisibilityObject(Visibility.Id, Visibility.DisplayName),
new LicenseObject(License.Id, License.DisplayName),
JObject.FromObject(new RDSResourceTypeObject(Guid.NewGuid(), "PITLABTTEST", "accesskey", "secretkey"))
);
Stream stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
......@@ -97,16 +111,14 @@ namespace Coscine.Api.Project.Tests
// Cleanup
stream.Close();
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.GetById(resourceObject.Id);
stream = ObjectFactory<ResourceObject>.SerializeToStream(resourceObject);
ProjectResourceModel projectResourceModel = new ProjectResourceModel();
foreach(var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ProjectId == Projects[0].Id && projectResource.ResourceId == resource.Id))
{
projectResourceModel.Delete(projectResource);
}
FakeControllerContext(Users[0], stream);
actionResult = Controller.Delete(resourceObject.Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
resourceModel.Delete(resource);
stream.Close();
}
}
}
using Coscine.Api.Project.Controllers;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.Tests
{
[TestFixture]
public class ResourceTypeControllerTests : DefaultControllerTests<ResourceTypeController>
{
public ResourceTypeControllerTests() : base(new ResourceTypeController())
{
}
[Test]
public void TestGettingFields()
{
var actionResult = Controller.Fields(Resources[0].Type.Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
List<string> fields = (List<string>) okObjectResult.Value;
if(fields.Count() == 3)
{
Assert.IsTrue(fields[0] == "BucketName");
Assert.IsTrue(fields[1] == "AccessKey");
Assert.IsTrue(fields[2] == "SecretKey");
}
else
{
Assert.Fail("Wrong field count!");
}
}
}
}
......@@ -88,7 +88,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Coscine.Database" publicKeyToken="767d77427707b70a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.5.0.0" newVersion="1.5.0.0" />
<bindingRedirect oldVersion="0.0.0.0-1.6.0.0" newVersion="1.6.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
......
......@@ -7,7 +7,7 @@
<package id="Coscine.Action" version="1.3.0" targetFramework="net472" />
<package id="Coscine.ApiCommons" version="1.2.1" targetFramework="net472" />
<package id="Coscine.Configuration" version="1.4.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.5.0" targetFramework="net472" />
<package id="Coscine.Database" version="1.6.0" targetFramework="net472" />
<package id="Coscine.ProxyApi" version="1.2.0" targetFramework="net472" />
<package id="Coscine.SharePoint.Webparts.Vue" version="1.4.0" targetFramework="net472" />
<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
......
......@@ -91,7 +91,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Coscine.Database" publicKeyToken="767d77427707b70a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.5.0.0" newVersion="1.5.0.0" />
<bindingRedirect oldVersion="0.0.0.0-1.6.0.0" newVersion="1.6.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
......
......@@ -75,11 +75,17 @@ namespace Coscine.Api.Project.Controllers
if (resource.Type.DisplayName.ToLower() == "rds")
{
authHeader = BuildRdsAuthHeader(resource.ExternalId);
RDSResourceTypeModel rdsResourceTypeModel = new RDSResourceTypeModel();
var rdsResourceType = rdsResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
authHeader = BuildRdsAuthHeader(rdsResourceType);
}
else if (resource.Type.DisplayName.ToLower() == "gitlab")
{
authHeader = BuildGitlabAuthHeader(resource.ExternalId, resource.Url);
GitlabResourceTypeModel gitlabResourceTypeModel = new GitlabResourceTypeModel();
var gitlabResourceType = gitlabResourceTypeModel.GetById(resource.ResourceTypeOptionId.Value);
authHeader = BuildGitlabAuthHeader(gitlabResourceType);
}
if (authHeader != null)
......@@ -146,39 +152,39 @@ namespace Coscine.Api.Project.Controllers
return _jwtHandler.GenerateJwtToken(payload);
}
private string BuildRdsAuthHeader(string bucketname)
private string BuildRdsAuthHeader(RDSResourceType rdsResourceType)
{
var auth = new Dictionary<string, object>();
var credentials = new Dictionary<string, object>
{
{ "access_key", _configuration.GetString("coscine/global/rds_access_key") },
{ "secret_key", _configuration.GetString("coscine/global/rds_secret_key") }
{ "access_key", rdsResourceType.AccessKey },
{ "secret_key", rdsResourceType.SecretKey }
};
var settings = new Dictionary<string, object>
{
{ "bucket", bucketname }
{ "bucket", rdsResourceType.BucketName }
};
return BuildWaterbutlerPayload(auth, credentials, settings);
}
private string BuildGitlabAuthHeader(string externalId, string url)
private string BuildGitlabAuthHeader(GitlabResourceType gitlabResourceType)
{
var auth = new Dictionary<string, object>();
var credentials = new Dictionary<string, object>
{
{ "token", _configuration.GetString("coscine/global/gitlabtoken") }
{ "token", gitlabResourceType.Token }
};
var settings = new Dictionary<string, object>
{
{"owner", "Tester"},
{"repo", url},
{ "repo_id", externalId},
{ "repo", gitlabResourceType.RepositoryUrl},
{ "repo_id", gitlabResourceType.RepositoryNumber},
{ "host", "https://git.rwth-aachen.de"}
};
......
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.Controllers
{
public class DisciplineController : Controller
{
private readonly Authenticator _authenticator;
private readonly DisciplineModel _disciplineModel;
public DisciplineController()
{
_authenticator = new Authenticator(this, Program.Configuration);
_disciplineModel = new DisciplineModel();
}
[Route("[controller]")]
public IActionResult Index()
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
return _disciplineModel.GetAll().Select((discipline) => new DisciplineObject(discipline.Id, discipline.Url, discipline.DisplayName));
}));
}
}
}
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.Controllers
{
public class InstituteController : Controller
{
private readonly Authenticator _authenticator;
private readonly InstituteModel _instituteModel;
public InstituteController()
{
_authenticator = new Authenticator(this, Program.Configuration);
_instituteModel = new InstituteModel();
}
[Route("[controller]")]
public IActionResult Index()
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
return _instituteModel.GetAll().Select((institute) => new InstituteObject(institute.Id, institute.IKZ, institute.DisplayName));
}));
}
}
}
......@@ -41,7 +41,7 @@ namespace Coscine.Api.Project.Controllers
where projectRole.User == user
&& projectRole.Role.DisplayName == "Owner"
select projectRole).Any()
).Select((project) => new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords));
).Select((project) => _projectModel.CreateReturnObjectFromDatabaseObject(project));
}));
}
......@@ -53,7 +53,7 @@ namespace Coscine.Api.Project.Controllers
var project = _projectModel.GetById(Guid.Parse(id));
if (_projectModel.CanSeeProject(user, project))
{
return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
return _projectModel.CreateReturnObjectFromDatabaseObject(project);
}
else
{
......@@ -78,12 +78,7 @@ namespace Coscine.Api.Project.Controllers
select projectResource).Any())
.Select((resource) =>
{
ResourceType resourceType = resource.Type;
if (resourceType == null)
{
resourceType = resourceTypeModel.GetById(resource.TypeId);
}
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, resource.DisplayName, new ResourceTypeObject(resourceType.Id, resourceType.DisplayName));
return resourceModel.CreateReturnObjectFromDatabaseObject(resource);
});
}
else
......@@ -111,6 +106,79 @@ namespace Coscine.Api.Project.Controllers
}));
}
[HttpDelete("[controller]/{id}")]
public IActionResult Delete(string id)
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
var project = _projectModel.GetById(Guid.Parse(id));
if (_projectModel.OwnsProject(user, project))
{
DeleteProject(project);
return _projectModel.CreateReturnObjectFromDatabaseObject(project);
}
else
{
throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project!");
}
}));
}
public void DeleteProject(Coscine.Database.Model.Project project)
{
SubProjectModel subProjectModel = new SubProjectModel();
foreach(var subProject in subProjectModel.GetAllWhere((subProject) => subProject.ProjectId == project.Id))
{
subProjectModel.Delete(subProject);
DeleteProject(_projectModel.GetById(subProject.SubProjectId));
}