|
| 1 | +package edu.sjsu.moth.controllers; |
| 2 | + |
| 3 | +import de.flapdoodle.embed.mongo.config.Net; |
| 4 | +import de.flapdoodle.embed.mongo.distribution.Version; |
| 5 | +import de.flapdoodle.embed.mongo.transitions.Mongod; |
| 6 | +import de.flapdoodle.embed.mongo.transitions.RunningMongodProcess; |
| 7 | +import de.flapdoodle.embed.process.io.ProcessOutput; |
| 8 | +import de.flapdoodle.reverse.TransitionWalker; |
| 9 | +import de.flapdoodle.reverse.transitions.Start; |
| 10 | +import edu.sjsu.moth.IntegrationTest; |
| 11 | +import edu.sjsu.moth.server.MothServerMain; |
| 12 | +import edu.sjsu.moth.server.controller.StatusController; |
| 13 | +import edu.sjsu.moth.server.db.Account; |
| 14 | +import edu.sjsu.moth.server.db.AccountRepository; |
| 15 | +import edu.sjsu.moth.server.util.MothConfiguration; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.Assertions; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; |
| 23 | +import org.springframework.boot.test.context.SpringBootTest; |
| 24 | +import org.springframework.context.annotation.ComponentScan; |
| 25 | +import org.springframework.http.MediaType; |
| 26 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.util.Random; |
| 30 | + |
| 31 | +import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockJwt; |
| 32 | + |
| 33 | +@SpringBootTest(classes = { TimelineControllerTest.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 34 | +@AutoConfigureDataMongo |
| 35 | +@ComponentScan(basePackageClasses = MothServerMain.class) |
| 36 | +@AutoConfigureWebTestClient |
| 37 | +public class TimelineControllerTest { |
| 38 | + public static final String TIMELINE_END_POINTS = "/api/v1/timelines/public"; |
| 39 | + public static final String POST_STATUS_ENDPOINT = "/api/v1/statuses"; |
| 40 | + static private final int RAND_MONGO_PORT = 27017 + new Random().nextInt(17, 37); |
| 41 | + // https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/main/docs/Howto.md documents how to startup |
| 42 | + // embedded mongodb |
| 43 | + static private TransitionWalker.ReachedState<RunningMongodProcess> eMongod; |
| 44 | + |
| 45 | + final WebTestClient webTestClient; |
| 46 | + final AccountRepository accountRepository; |
| 47 | + |
| 48 | + static { |
| 49 | + try { |
| 50 | + var fullname = IntegrationTest.class.getResource("/test.cfg").getFile(); |
| 51 | + // we need to fake out MothConfiguration |
| 52 | + System.out.println(new MothConfiguration(new File(fullname)).properties); |
| 53 | + } catch (Exception e) { |
| 54 | + System.err.println(e.getMessage()); |
| 55 | + System.exit(2); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + @Autowired |
| 61 | + public TimelineControllerTest(WebTestClient webTestClient, AccountRepository accountRepository) { |
| 62 | + this.webTestClient = webTestClient; |
| 63 | + this.accountRepository = accountRepository; |
| 64 | + } |
| 65 | + |
| 66 | + @AfterAll |
| 67 | + static void clean() { |
| 68 | + eMongod.close(); |
| 69 | + } |
| 70 | + |
| 71 | + @BeforeAll |
| 72 | + static void setup() { |
| 73 | + eMongod = Mongod.builder().processOutput(Start.to(ProcessOutput.class).initializedWith(ProcessOutput.silent())) |
| 74 | + .net(Start.to(Net.class).initializedWith(Net.defaults().withPort(RAND_MONGO_PORT))).build() |
| 75 | + .start(Version.Main.V6_0); |
| 76 | + System.setProperty("spring.data.mongodb.port", Integer.toString(RAND_MONGO_PORT)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void checkAutoWires() { |
| 81 | + Assertions.assertNotNull(webTestClient); |
| 82 | + Assertions.assertNotNull(accountRepository); |
| 83 | + } |
| 84 | + |
| 85 | + private void prepareStatus() { |
| 86 | + String statusCreator = "test-user"; |
| 87 | + accountRepository.save(new Account(statusCreator)).block(); |
| 88 | + |
| 89 | + StatusController.V1PostStatus request; |
| 90 | + String[] visibilities = { "public", "unlisted", "follower", "direct" }; |
| 91 | + for (String visibility : visibilities) { |
| 92 | + request = new StatusController.V1PostStatus(); |
| 93 | + request.status = String.format("This is a %s status", visibility); |
| 94 | + request.visibility = visibility; |
| 95 | + |
| 96 | + webTestClient.mutateWith(mockJwt().jwt(jwt -> jwt.claim("sub", statusCreator))).post() |
| 97 | + .uri(POST_STATUS_ENDPOINT).contentType(MediaType.APPLICATION_JSON).bodyValue(request).exchange() |
| 98 | + .expectStatus().isOk(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testTimelineGetAllPublicStatus() { |
| 104 | + prepareStatus(); |
| 105 | + accountRepository.save(new Account("test-fetch")).block(); |
| 106 | + // Mock the authentication |
| 107 | + webTestClient |
| 108 | + .mutateWith(mockJwt().jwt(jwt -> jwt.claim("sub", "test-fetch"))) |
| 109 | + .get() |
| 110 | + .uri(TIMELINE_END_POINTS) |
| 111 | + .exchange().expectStatus().isOk() |
| 112 | + .expectBody() |
| 113 | + .jsonPath("$.length()").isEqualTo(1) |
| 114 | + .jsonPath("$[0].visibility").isEqualTo("public"); |
| 115 | + } |
| 116 | +} |
0 commit comments