@Before public void setup() { doortts = User.findByLoginId("doortts"); nori = User.findByLoginId("nori"); yobi = Project.findByOwnerAndProjectName("yobi", "projectYobi"); cubrid = Project.findByOwnerAndProjectName("doortts", "CUBRID"); }
@Test public void detachLabel() { // Given Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); Label label1 = new Label("OS", "linux"); label1.save(); project.labels.add(label1); project.update(); Long labelId = label1.id; assertThat(project.labels.contains(label1)).isTrue(); Map<String, String> data = new HashMap<>(); data.put("_method", "DELETE"); User admin = User.findByLoginId("admin"); String user = "******"; String projectName = "projectYobi"; // When Result result = callAction( controllers.routes.ref.ProjectApp.detachLabel(user, projectName, labelId), fakeRequest(POST, routes.ProjectApp.detachLabel(user, projectName, labelId).url()) .withFormUrlEncodedBody(data) .withHeader("Accept", "application/json") .withSession(UserApp.SESSION_USERID, admin.id.toString())); // Then assertThat(status(result)).isEqualTo(204); assertThat(Project.findByOwnerAndProjectName("yobi", "projectYobi").labels.contains(label1)) .isFalse(); }
@Test public void delete() { // Given User member = User.find.byId(2L); Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); RecentlyVisitedProjects.addNewVisitation(member, project); // When project.delete(); // Then assertThat(Project.findByOwnerAndProjectName("yobi", "projectYobi")).isNull(); }
@Test public void optimisticLockException() { Project project1 = Project.findByOwnerAndProjectName("yobi", "projectYobi"); Project project2 = Project.findByOwnerAndProjectName("yobi", "projectYobi"); issue = new Issue(); issue.setProject(project1); issue.setTitle("a"); issue.save(); issue = new Issue(); issue.setProject(project2); issue.setTitle("b"); issue.save(); }
@Test public void label() { // Given Map<String, String> data = new HashMap<>(); data.put("category", "OS"); data.put("name", "linux"); User admin = User.findByLoginId("admin"); String user = "******"; String projectName = "projectYobi"; // When Result result = callAction( controllers.routes.ref.ProjectApp.attachLabel(user, projectName), fakeRequest(POST, routes.ProjectApp.attachLabel(user, projectName).url()) .withFormUrlEncodedBody(data) .withHeader("Accept", "application/json") .withSession(UserApp.SESSION_USERID, admin.id.toString())); // Then assertThat(status(result)).isEqualTo(CREATED); Iterator<Map.Entry<String, JsonNode>> fields = Json.parse(contentAsString(result)).getFields(); Map.Entry<String, JsonNode> field = fields.next(); Label expected = new Label(field.getValue().get("category").asText(), field.getValue().get("name").asText()); expected.id = Long.valueOf(field.getKey()); assertThat(expected.category).isEqualTo("OS"); assertThat(expected.name).isEqualTo("linux"); assertThat(Project.findByOwnerAndProjectName("yobi", "projectYobi").labels.contains(expected)) .isTrue(); }
@Test public void roleOf() { // GIVEN String loginId = "yobi"; Project project = Project.findByOwnerAndProjectName(loginId, "projectYobi"); // WHEN String roleName = ProjectUser.roleOf(loginId, project); // THEN assertThat(roleName).isEqualTo("manager"); // WHEN roleName = ProjectUser.roleOf("admin", project); // THEN assertThat(roleName).isEqualTo("sitemanager"); // WHEN roleName = ProjectUser.roleOf((String) null, project); // THEN assertThat(roleName).isEqualTo("anonymous"); // WHEN roleName = ProjectUser.roleOf("keesun", project); // THEN assertThat(roleName).isEqualTo("anonymous"); // WHEN roleName = ProjectUser.roleOf("laziel", project); // THEN assertThat(roleName).isEqualTo("member"); }
@IsAllowed(Operation.UPDATE) public static Result labelsForm(String ownerName, String projectName) { Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<IssueLabel> labels = IssueLabel.findByProject(project); return ok(views.html.project.issuelabels.render(project, labels)); }
@Test public void projectOverviewUpdateTest() { // Given Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); String newDescription = "new overview description"; ObjectNode requestJson = Json.newObject(); requestJson.put("overview", newDescription); User member = User.findByLoginId("yobi"); // When Result result = callAction( controllers.routes.ref.ProjectApp.projectOverviewUpdate("yobi", "projectYobi"), fakeRequest(PUT, "/yobi/projectYobi") .withJsonBody(requestJson) .withHeader("Accept", "application/json") .withHeader("Content-Type", "application/json") .withSession(UserApp.SESSION_USERID, member.id.toString())); // Then assertThat(status(result)).isEqualTo(200); // response status code assertThat(contentAsString(result)) .isEqualTo("{\"overview\":\"new overview description\"}"); // response json body project.refresh(); assertThat(project.overview).isEqualTo(newDescription); // is model updated }
/** * Responds to a request to add an issue label category for the specified project. * * <p>Adds an issue label category created with values taken from {@link * Form#bindFromRequest(java.util.Map, String...)} in the project specified by the {@code * ownerName} and {@code projectName}. But if there has already been the same issue label category * in name, then this method returns an empty 204 No Content response. * * <p>When a new category is added, this method encodes the category's fields: {@link * IssueLabelCategory#id}, {@link IssueLabelCategory#name}, {@link * IssueLabelCategory#isExclusive}, and includes them in the body of the 201 Created response. But * if the client cannot accept {@code application/json}, it returns the 201 Created with no * response body. * * @param ownerName the name of a project owner * @param projectName the name of a project * @return the response to the request to add a new issue label category */ @IsCreatable(ResourceType.ISSUE_LABEL_CATEGORY) public static Result newCategory(String ownerName, String projectName) { Form<IssueLabelCategory> form = new Form<>(IssueLabelCategory.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(); } IssueLabelCategory category = form.get(); category.project = Project.findByOwnerAndProjectName(ownerName, projectName); if (category.exists()) { return noContent(); } category.save(); if (!request().accepts("application/json")) { return created(); } Map<String, String> categoryPropertyMap = new HashMap<>(); categoryPropertyMap.put("id", "" + category.id); categoryPropertyMap.put("name", category.name); categoryPropertyMap.put("isExclusive", "" + category.isExclusive); return created(toJson(categoryPropertyMap)).as("application/json"); }
@Test public void testAcceptTransferWithWrongKey() throws IOException, ServletException, ClientException { // Given GitRepository.setRepoPrefix("resources/test/repo/git/"); Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); RepositoryService.createRepository(project); User sender = User.findByLoginId("yobi"); User newOwner = User.findByLoginId("doortts"); ProjectTransfer pt = ProjectTransfer.requestNewTransfer(project, sender, newOwner.loginId); assertThat(pt.confirmKey).isNotNull(); // When Result result = callAction( controllers.routes.ref.ProjectApp.acceptTransfer(pt.id, "wrongKey"), fakeRequest(PUT, routes.ProjectApp.acceptTransfer(pt.id, "wrongKey").url()) .withSession(UserApp.SESSION_USERID, newOwner.id.toString())); // Then assertThat(status(result)).isEqualTo(400); support.Files.rm_rf(new File(GitRepository.getRepoPrefix())); }
private static Result labelsAsPjax(String ownerName, String projectName) { response().setHeader("Cache-Control", "no-cache, no-store"); Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<IssueLabel> labels = IssueLabel.findByProject(project); return ok(views.html.project.partial_issuelabels_list.render(project, labels)); }
@Test public void memberCanSearchPublicProjectsAsJson() { // Given User member = User.find.byId(2L); Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); // When // Then testProjectSearch(member, project, acceptJson, contains(project.owner + "/" + project.name)); }
@Test public void adminCanSearchPrivateProjectsAsJson() { // Given User admin = User.find.byId(1L); Project project = Project.findByOwnerAndProjectName("laziel", "Jindo"); // When // Then testProjectSearch(admin, project, acceptJson, contains(project.owner + "/" + project.name)); }
@Test public void anonymousCanSearchPublicProjectsAsJson() { // Given User anonymous = User.anonymous; Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); // When // Then testProjectSearch(anonymous, project, acceptJson, contains(project.owner + "/" + project.name)); }
@Test public void memberCannotSearchPrivateProjectsAsJson() { // Given User member = User.find.byId(3L); Project project = Project.findByOwnerAndProjectName("laziel", "Jindo"); // When // Then testProjectSearch( member, project, acceptJson, doesNotContains(project.owner + "/" + project.name)); }
@Test public void anonymousCannotSearchPrivateProjectsAsJson() { // Given User anonymous = User.anonymous; Project project = Project.findByOwnerAndProjectName("laziel", "Jindo"); // When // Then testProjectSearch( anonymous, project, acceptJson, doesNotContains(project.owner + "/" + project.name)); }
/** * Responds to a request for an issue label category. * * <p>Returns 406 Not Acceptable if the client cannot accept {@code application/json}. Success * response can only be returned when the content type of the body is {@code application/json}. * * @param ownerName Don't use. * @param projectName Don't use. * @param id the id of the category * @return the response to the request for an issue label category */ @IsAllowed(value = Operation.READ, resourceType = ResourceType.ISSUE_LABEL_CATEGORY) public static Result category(String ownerName, String projectName, Long id) { if (!request().accepts("application/json")) { return status(Http.Status.NOT_ACCEPTABLE); } Project project = Project.findByOwnerAndProjectName(ownerName, projectName); IssueLabelCategory category = IssueLabelCategory.find.byId(id); return ok(toJson(toMap(category))).as("application/json"); }
@Test public void adminCanSearchPrivateProjects() { // Given User admin = User.find.byId(1L); Project project = Project.findByOwnerAndProjectName("laziel", "Jindo"); // When // Then testProjectSearch( admin, project, acceptHtml, contains(routes.ProjectApp.project(project.owner, project.name).url())); }
@Test public void memberCannotSearchPrivateProjects() { // Given User member = User.find.byId(3L); Project project = Project.findByOwnerAndProjectName("laziel", "Jindo"); // When // Then testProjectSearch( member, project, acceptHtml, doesNotContains(routes.ProjectApp.project(project.owner, project.name).url())); }
@Test public void memberCanSearchPublicProjects() { // Given User member = User.find.byId(2L); Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); // When // Then testProjectSearch( member, project, acceptHtml, contains(routes.ProjectApp.project(project.owner, project.name).url())); }
@Test public void isAllowedToSettings() { // GIVEN String loginId = "yobi"; Project project = Project.findByOwnerAndProjectName(loginId, "projectYobi"); // WHEN // THEN assertThat(ProjectUser.isAllowedToSettings(loginId, project)).isTrue(); // WHEN // THEN assertThat(ProjectUser.isAllowedToSettings("admin", project)).isTrue(); // WHEN // THEN assertThat(ProjectUser.isAllowedToSettings(null, project)).isFalse(); // WHEN // THEN assertThat(ProjectUser.isAllowedToSettings("keesun", project)).isFalse(); }
@Test public void anonymousCannotSearchPrivateProjects() { // Given User anonymous = User.anonymous; Project project = Project.findByOwnerAndProjectName("laziel", "Jindo"); // When // Then testProjectSearch( anonymous, project, acceptHtml, doesNotContains(routes.ProjectApp.project(project.owner, project.name).url())); }
@Test public void anonymousCanSearchPublicProjects() { // Given User anonymous = User.anonymous; Project project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); // When // Then testProjectSearch( anonymous, project, acceptHtml, contains(routes.ProjectApp.project(project.owner, project.name).url())); }
/** * Responds to a request for issue label categories of the specified project. * * <p>Retrieves a project corresponding to {@code ownerName} and {@code projectName}, and returns * its list of all issue label categories in {@code application/json}. Each category has three * fields: {@link IssueLabelCategory#id}, {@link IssueLabelCategory#name}, {@link * IssueLabelCategory#isExclusive}. * * <p>Returns 403 Forbidden if the user has no permission to access to the project. * * <p>Returns 406 Not Acceptable if the client cannot accept {@code application/json}. Success * response can only be returned when the content type of the body is {@code application/json}. * * @param ownerName the name of a project owner * @param projectName the name of a project * @return the response to the request for issue label categories */ @IsAllowed(Operation.READ) public static Result categories(String ownerName, String projectName) { if (!request().accepts("application/json")) { return status(Http.Status.NOT_ACCEPTABLE); } Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<Map<String, String>> categories = new ArrayList<>(); for (IssueLabelCategory category : IssueLabelCategory.findByProject(project)) { categories.add(toMap(category)); } return ok(toJson(categories)).as("application/json"); }
@IsAllowed(value = Operation.UPDATE, resourceType = ResourceType.ISSUE_LABEL_CATEGORY) public static Result updateCategory(String ownerName, String projectName, Long id) { Form<IssueLabelCategory> form = new Form<>(IssueLabelCategory.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(form.errorsAsJson()); } IssueLabelCategory category = form.get(); category.id = id; category.project = Project.findByOwnerAndProjectName(ownerName, projectName); category.update(); return ok(); }
/** * Responds to a request for the CSS styles of all issue labels in the specified project. * * <p>This method is used when CSS styles of issue labels are required in any page which uses * issue label. * * @param ownerName the name of a project owner * @param projectName the name of a project * @return the response to the request for the css styles in text/css. */ @IsAllowed(Operation.READ) public static Result labelStyles(String ownerName, String projectName) { Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<IssueLabel> labels = IssueLabel.findByProject(project); String eTag = "\"" + labels.hashCode() + "\""; String ifNoneMatchValue = request().getHeader("If-None-Match"); if (ifNoneMatchValue != null && ifNoneMatchValue.equals(eTag)) { response().setHeader("ETag", eTag); return status(NOT_MODIFIED); } response().setHeader("ETag", eTag); return ok(views.html.common.issueLabelColor.render(labels)).as("text/css"); }
@Before public void before() { project = Project.findByOwnerAndProjectName("yobi", "projectYobi"); admin = User.findByLoginId("admin"); manager = User.findByLoginId("yobi"); member = User.findByLoginId("laziel"); author = User.findByLoginId("nori"); nonmember = User.findByLoginId("doortts"); anonymous = new NullUser(); issue = new Issue(); issue.setProject(project); issue.setTitle("hello"); issue.setBody("world"); issue.setAuthor(author); issue.state = State.OPEN; issue.save(); }
/** * Responds to a request to add an issue label for the specified project. * * <p>This method is used when a user tries to add a issue label in issue list, editing issue or * new issue page. * * <p>Adds an issue label created with values taken from {@link * Form#bindFromRequest(java.util.Map, String...)} in the project specified by the {@code * ownerName} and {@code projectName}. But if there has already been the same issue label in * category, name and color, then this method returns an empty 204 No Content response. * * <p>When a new label is added, this method encodes the label's fields: {@link IssueLabel#id}, * {@link IssueLabel#name}, {@link IssueLabel#color}, and {@link IssueLabel#category} into {@code * application/json}, and includes them in the body of the 201 Created response. But if the client * cannot accept {@code application/json}, it returns the 201 Created with no response body. * * <p>Returns 403 Forbidden, if the user has no permission to add a new label in the project. * * @param ownerName the name of a project owner * @param projectName the name of a project * @return the response to the request to add a new issue label */ @Transactional @IsCreatable(ResourceType.ISSUE_LABEL) public static Result newLabel(String ownerName, String projectName) { Project project = Project.findByOwnerAndProjectName(ownerName, projectName); Form<NewLabel> newLabelForm = form(NewLabel.class).bindFromRequest(); if (newLabelForm.hasErrors()) { return badRequest(newLabelForm.errorsAsJson()); } IssueLabelCategory category = newLabelForm.get().getIssueLabelCategory(project); if (category.exists()) { category = IssueLabelCategory.findBy(category); } else { category.save(); } IssueLabel label = newLabelForm.get().getIssueLabel(project, category); if (label.exists()) { return noContent(); } else { label.save(); if (!request().accepts("application/json")) { return created(); } response().setHeader("Content-Type", "application/json"); Map<String, String> labelPropertyMap = new HashMap<>(); labelPropertyMap.put("id", "" + label.id); labelPropertyMap.put("name", label.name); labelPropertyMap.put("color", label.color); labelPropertyMap.put("category", label.category.name); labelPropertyMap.put("categoryId", "" + label.category.id); return created(toJson(labelPropertyMap)); } }
@Override public final Result call(Context context) throws Throwable { PathParser parser = new PathParser(context); String ownerLoginId = parser.getOwnerLoginId(); String projectName = parser.getProjectName(); Project project = Project.findByOwnerAndProjectName(ownerLoginId, projectName); if (project == null) { return AccessLogger.log( context.request(), notFound(ErrorViews.NotFound.render("error.notfound.project")), null); } if (!AccessControl.isAllowed(UserApp.currentUser(), project.asResource(), Operation.READ)) { return AccessLogger.log( context.request(), notFound(ErrorViews.NotFound.render("error.notfound.project")), null); } return call(project, context, parser); }
/** * Retrieves a project corresponding to {@code ownerName} and {@code projectName}, and returns its * list of all issue labels in {@code application/json}. Each label has four fields: {@link * IssueLabel#id}, {@link IssueLabel#category}, {@link IssueLabel#color}, and {@link * IssueLabel#name}. * * <p>Returns 406 Not Acceptable if the client cannot accept {@code application/json}. Success * response can only be returned when the content type of the body is {@code application/json}. * * @param ownerName * @param projectName * @return the response to the request for issue labels */ private static Result labelsAsJSON(String ownerName, String projectName) { if (!request().accepts("application/json")) { return status(Http.Status.NOT_ACCEPTABLE); } Project project = Project.findByOwnerAndProjectName(ownerName, projectName); List<Map<String, String>> labels = new ArrayList<>(); for (IssueLabel label : IssueLabel.findByProject(project)) { Map<String, String> labelPropertyMap = new HashMap<>(); labelPropertyMap.put("id", "" + label.id); labelPropertyMap.put("category", label.category.name); labelPropertyMap.put("categoryId", "" + label.category.id); labelPropertyMap.put("color", label.color); labelPropertyMap.put("name", label.name); labels.add(labelPropertyMap); } response().setHeader("Content-Type", "application/json"); return ok(toJson(labels)); }