public async Task <IActionResult> Apply(ResourceInputModel model) { var apiResources = await _resourceStore.FindApiResourceAsync(model.ApiName); if (apiResources != null) { var client = await _clientStore.FindClientByIdAsync(model.ClientId); if (client != null) { if (!client.AllowedScopes.Contains(apiResources.Name)) { client.AllowedScopes = client.AllowedScopes ?? new string[] { }; client.AllowedScopes.Add(apiResources.Name); _configurationDbContext.Clients.Update(client.ToEntity()); await _configurationDbContext.SaveChangesAsync(); } } } return(null); }
public ActionResult Upload(int id, ResourceInputModel model) { if (!this.ModelState.IsValid) { return this.View(model); } string pathDb = "/Resources/" + model.File.FileName.Replace(" ", "-"); string pathFileSystem = this.Server.MapPath("~/Resources/" + model.File.FileName.Replace(" ", "-")); model.File.SaveAs(pathFileSystem); this.courseService.AddResourse(model.Name, pathDb, id); return this.RedirectToAction("Details", "Courses", new { area = "Public", id = id }); }
public void ExpectTrainerToUploadResourseFileSuccessfuly() { const string ResourceName = "Testing Resource"; const string FullFileName = "Test File.pdf"; const int CourseId = 3; string expectedDbPath = "/Resources/" + FullFileName.Replace(" ", "-"); string expectedServerPath = "C:/Resources/" + FullFileName.Replace(" ", "-"); var courseServiceMock = new Mock<ICoursesService>(); var fileMock = new Mock<HttpPostedFileBase>(); var serverMock = new Mock<HttpServerUtilityBase>(); var httpContextMock = new Mock<HttpContextBase>(); serverMock.Setup(s => s.MapPath(It.IsAny<string>())).Returns<string>((c) => { return "C:/" + c.Replace("~/", string.Empty); }); httpContextMock.Setup(h => h.Server).Returns(serverMock.Object); courseServiceMock.Setup(c => c.AddResourse(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>())) .Callback<string, string, int>((name, path, courseId) => { Assert.AreEqual(expectedDbPath, path); Assert.AreEqual(ResourceName, name); Assert.AreEqual(CourseId, courseId); }); fileMock.Setup(f => f.FileName).Returns(FullFileName); fileMock.Setup(f => f.SaveAs(It.IsAny<string>())) .Callback<string>(serverPath => { Assert.AreEqual(expectedServerPath, serverPath); }); var controller = new CoursesController(null, courseServiceMock.Object); var resourceModel = new ResourceInputModel() { File = fileMock.Object, Name = ResourceName }; controller.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), controller); controller.WithCallTo(c => c.Upload(CourseId, resourceModel)) .ShouldRedirectTo<Web.Areas.Public.Controllers.CoursesController>(c => c.Details(CourseId)); }