    @Test
    public void authorizeTest() throws Exception {


        // Normal authentication
        LoginVM vm = new LoginVM();
        vm.setUsername("admin");
        vm.setPassword("admin");
        vm.setRememberMe(true);

        Mockito.doReturn("fakeToken").when(tokenProvider)
            .createToken(Mockito.any(Authentication.class), Mockito.anyBoolean());

        mock.perform(post("/api/authenticate")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL)
            .content(new ObjectMapper().writeValueAsString(vm)))
            .andExpect(content().string("{\"id_token\":\"fakeToken\"}"))
            .andExpect(status().isOk());


        // Authentication exception throws
        Mockito.doThrow(new AuthenticationException(null){}).when(tokenProvider)
            .createToken(Mockito.any(Authentication.class), Mockito.anyBoolean());

        MvcResult res = mock.perform(post("/api/authenticate")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL)
            .content("{\"username\":\"fakeUsername\",\"password\":\"fakePassword\",\"rememberMe\":false}"))
            .andExpect(status().isUnauthorized())
            .andReturn();

        assertTrue(res.getResponse().getContentAsString().startsWith("{\"AuthenticationException\""));


        // Bad credentials
        vm.setUsername("badcred");
        vm.setPassword("badcred");

        Mockito.doThrow(new BadCredentialsException("Bad credentials")).when(authenticationManager)
            .authenticate(new UsernamePasswordAuthenticationToken(vm.getUsername(), vm.getPassword()));

        mock.perform(post("/api/authenticate")
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL)
            .content(new ObjectMapper().writeValueAsString(vm)))
            .andExpect(status().isUnauthorized())
            .andExpect(content().string("{\"AuthenticationException\":\"Bad credentials\"}"));
    }