Skip to content

Commit

Permalink
moar (also fixes #1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Sep 6, 2023
1 parent e8bb0ac commit 8fd86ff
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import java.util.function.Consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

@Service
public class ProjectsApiService extends HangarComponent {
Expand All @@ -41,6 +43,9 @@ public ProjectsApiService(final ProjectsApiDAO projectsApiDAO, final UsersApiSer
public Project getProject(final String slug) {
final boolean seeHidden = this.getGlobalPermissions().has(Permission.SeeHidden);
final Project project = this.projectsApiDAO.getProject(slug, seeHidden, this.getHangarUserId());
if (project == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Project " + slug + " not found");
}
project.setAvatarUrl(this.avatarService.getProjectAvatarUrl(project.getId(), project.getNamespace().getOwner()));
return project;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.papermc.hangar.controller.api.v1;

import io.papermc.hangar.controller.api.v1.helper.ControllerTest;
import io.papermc.hangar.controller.api.v1.helper.TestData;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.internal.api.requests.CreateAPIKeyForm;
import java.util.Set;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

class ProjectsControllerTest extends ControllerTest {

@Test
void testGetProject() throws Exception {
this.mockMvc.perform(get("/api/v1/projects/TestProject")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.name", is("TestProject")))
.andExpect(jsonPath("$.namespace.owner", is("PaperMC")));
}

@Test
void testGetHiddenProject() throws Exception {
this.mockMvc.perform(get("/api/v1/projects/TestProject")
.with(this.apiKey(TestData.KEY_PROJECT_ONLY)))
.andExpect(status().is(404));
}

@Test
void testGetMembers() throws Exception {
this.mockMvc.perform(get("/api/v1/projects/TestProject/members")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.pagination.count", is(1)))
.andExpect(jsonPath("$.result[0].user", is("PaperMC")))
.andExpect(jsonPath("$.result[0].roles[0].title", is("Owner")));
}

@Test
void testGetStats() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetStargazers() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetWatchers() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetProjectsByAuthor() throws Exception {
this.mockMvc.perform(get("/api/v1/projects?owner=PaperMC")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.pagination.count", is(1)))
.andExpect(jsonPath("$.result[0].name", is("TestProject")))
.andExpect(jsonPath("$.result[0].namespace.owner", is("PaperMC")));
}

@Test
void testGetProjectsByQuery() throws Exception {
this.mockMvc.perform(get("/api/v1/projects?q=Test")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.pagination.count", is(1)))
.andExpect(jsonPath("$.result[0].name", is("TestProject")))
.andExpect(jsonPath("$.result[0].namespace.owner", is("PaperMC")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.papermc.hangar.controller.api.v1;

import io.papermc.hangar.controller.api.v1.helper.ControllerTest;
import io.papermc.hangar.controller.api.v1.helper.TestData;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.internal.api.requests.CreateAPIKeyForm;
import java.util.Set;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

class UserControllerTest extends ControllerTest {

@Test
void testGetUserOrg() throws Exception {
this.mockMvc.perform(get("/api/v1/users/" + TestData.ORG.getName())
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.name", is(TestData.ORG.getName())))
.andExpect(jsonPath("$.isOrganization", is(true)));
}

@Test
void testGetUser() throws Exception {
this.mockMvc.perform(get("/api/v1/users/" + TestData.USER_ADMIN.getName())
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.name", is(TestData.USER_ADMIN.getName())))
.andExpect(jsonPath("$.isOrganization", is(false)));
}

@Test
void testGetUsers() throws Exception {
this.mockMvc.perform(get("/api/v1/users?query=Test")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.pagination.count", is(3)))
.andExpect(jsonPath("$.result[*].name", contains("TestUser", "TestMember", "TestAdmin")));
}

@Test
void testGetStarred() {
// TODO
throw new RuntimeException();
}

@Test
void testGetWatching() {
// TODO
throw new RuntimeException();
}

@Test
void testGetPinned() {
// TODO
throw new RuntimeException();
}

@Test
void testGetAuthors() throws Exception {
this.mockMvc.perform(get("/api/v1/authors?query=PaperMC")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.pagination.count", is(1)))
.andExpect(jsonPath("$.result[*].name", contains("PaperMC")));
}

@Test
void testGetStaff() throws Exception {
this.mockMvc.perform(get("/api/v1/staff?query=Test")
.with(this.apiKey(TestData.KEY_ADMIN)))
.andExpect(status().is(200))
.andExpect(jsonPath("$.pagination.count", is(1)))
.andExpect(jsonPath("$.result[*].name", contains("TestAdmin")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.papermc.hangar.controller.api.v1;

import io.papermc.hangar.controller.api.v1.helper.ControllerTest;
import io.papermc.hangar.controller.api.v1.helper.TestData;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.internal.api.requests.CreateAPIKeyForm;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;

import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

class VersionsControllerTest extends ControllerTest {

@Test
void testUpload() throws Exception {
// TODO
this.mockMvc.perform(post("/api/v1/projects/TestProject/upload")
.with(this.apiKey(TestData.KEY_ADMIN))
.header("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE))
.andExpect(status().is(201))
.andExpect(jsonPath("$.url", is("")));
}

@Test
void testGetVersion() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetVersions() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetLatestRelease() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetLatestVersion() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testGetStats() throws Exception {
// TODO
throw new RuntimeException();
}

@Test
void testDownload() throws Exception {
// TODO
throw new RuntimeException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.papermc.hangar.service.internal.perms.roles.GlobalRoleService;
import io.papermc.hangar.service.internal.projects.ProjectFactory;
import io.papermc.hangar.service.internal.projects.ProjectPageService;
import io.papermc.hangar.service.internal.projects.ProjectService;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -69,6 +70,8 @@ public class TestData {
private ProjectFactory projectFactory;
@Autowired
private ProjectPageService projectPageService;
@Autowired
private ProjectService projectService;

@EventListener(ApplicationStartedEvent.class)
public void prepare() {
Expand Down Expand Up @@ -99,6 +102,8 @@ public void prepare() {
KEY_PROJECT_ONLY = this.apiKeyService.createApiKey(USER_NORMAL, new CreateAPIKeyForm("Project Only", Set.of(NamedPermission.CREATE_PROJECT)), Permission.All);
KEY_SEE_HIDDEN = this.apiKeyService.createApiKey(USER_NORMAL, new CreateAPIKeyForm("See Hidden", Set.of(NamedPermission.SEE_HIDDEN)), Permission.All);

this.projectService.refreshHomeProjects();

HangarApplication.TEST_PRINCIPAL = Optional.empty();
HangarApplication.TEST_MODE = false;
}
Expand Down

0 comments on commit 8fd86ff

Please sign in to comment.