@@ -0,0 +1,3254 @@
+package nl.fhict.happynews.api.auth.oauth2;%0A%0Aimport nl.fhict.happynews.api.TestConfig;%0Aimport nl.fhict.happynews.api.auth.User;%0Aimport nl.fhict.happynews.api.auth.UserRepository;%0Aimport org.json.JSONObject;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.boot.test.context.SpringBootTest;%0Aimport org.springframework.security.test.context.support.WithMockUser;%0Aimport org.springframework.test.context.ActiveProfiles;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0Aimport org.springframework.test.context.web.WebAppConfiguration;%0Aimport org.springframework.test.web.servlet.MockMvc;%0Aimport org.springframework.test.web.servlet.MvcResult;%0Aimport org.springframework.test.web.servlet.setup.MockMvcBuilders;%0Aimport org.springframework.web.context.WebApplicationContext;%0A%0Aimport java.util.Collections;%0A%0Aimport static org.hamcrest.Matchers.hasSize;%0Aimport static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;%0A%0A@RunWith(SpringJUnit4ClassRunner.class)%0A@WebAppConfiguration%0A@SpringBootTest(classes = TestConfig.class)%0A@ActiveProfiles(%22test%22)%0Apublic class OAuth2Test %7B%0A%0A    @Autowired%0A    private UserRepository userRepository;%0A%0A    @Autowired%0A    private WebApplicationContext context;%0A%0A    private MockMvc mockMvc;%0A%0A    private User user;%0A%0A    /**%0A     * Create default user.%0A     */%0A    @Before%0A    public void setUp() %7B%0A        mockMvc = MockMvcBuilders.webAppContextSetup(context)%0A            .apply(springSecurity())%0A            .build();%0A%0A        User user = new User();%0A        user.setUsername(%22admin%22);%0A        user.setPassword(%22password%22);%0A        user.setRoles(Collections.singleton(%22ROLE_ADMIN%22));%0A%0A        this.user = userRepository.save(user);%0A    %7D%0A%0A    @Test%0A    @WithMockUser(username = %22happynews-editor%22, password = %22OuNNQtRGBIfUTG2IDICCdOUt%22)%0A    public void testToken() throws Exception %7B%0A%0A        mockMvc.perform(get(%22/admin/users%22))%0A            .andExpect(status().isUnauthorized());%0A%0A        MvcResult mvcResult = mockMvc.perform(%0A            post(%22/oauth/token%22)%0A                .param(%22grant_type%22, %22password%22)%0A                .param(%22username%22, user.getUsername())%0A                .param(%22password%22, user.getPassword())%0A        )%0A            .andExpect(status().isOk())%0A            .andReturn();%0A%0A        JSONObject body = new JSONObject(mvcResult.getResponse().getContentAsString());%0A%0A        String accessToken = body.getString(%22access_token%22);%0A%0A        mockMvc.perform(%0A            get(%22/admin/users%22)%0A                .header(%22Authorization%22, %22Bearer %22 + accessToken)%0A        )%0A            .andExpect(status().isOk())%0A            .andExpect(jsonPath(%22$%22, hasSize(1)));%0A    %7D%0A%0A    @After%0A    public void tearDown() %7B%0A        userRepository.deleteAll();%0A    %7D%0A%7D%0A

Test OAuth2