package com.ryanpmartz.booktrackr.authentication;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ryanpmartz.booktrackr.BooktrackrApplication;
import com.ryanpmartz.booktrackr.authentication.dto.LoginRequest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.junit.Assert.assertTrue;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.hasText;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {BooktrackrApplication.class})
@WebIntegrationTest(randomPort = true)
@Sql("/integration-test-data.sql")
public class JwtAuthenticationIT {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .apply(springSecurity())
                .build();
    }

    @Test
    public void testCanAuthenticateAndUseTokenToAccessProtectedResource() throws Exception {
        mockMvc.perform(get("/books")).andExpect(status().isForbidden());

        LoginRequest login = new LoginRequest();
        login.setUsername("booktrackr@ryanpmartz.com");
        login.setPassword("password");

        ObjectMapper mapper = new ObjectMapper();

        MvcResult authenticationResult = mockMvc.perform(post("/authenticate").content(mapper.writeValueAsString(login)).contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk()).andReturn();

        String authHeaderValue = authenticationResult.getResponse().getHeader("Authorization");
        assertTrue(hasText(authHeaderValue));

        mockMvc.perform(get("/books").header("Authorization", authHeaderValue))
                .andDo(print()).andExpect(status().isOk());
    }

}

@@ -1986,16 +1986,184 @@
 %0A    %7D%0A%0A
+    /**%0A     * Test to ensure that %60/books%60 is denied to an unauthenticated user but can be accessed%0A     * once authenticated.%0A     *%0A     * @throws Exception%0A     */%0A
     @Tes

comment to test explaining purpose