3
3
import de .holarse .backend .db .ApiUser ;
4
4
import de .holarse .backend .db .repositories .SearchRepository ;
5
5
import de .holarse .backend .view .TagRecommendation ;
6
+
7
+ import static de .holarse .config .RoleUserTypes .ROLE_USER ;
6
8
import static org .junit .jupiter .api .Assertions .*;
7
9
8
10
import de .holarse .test .TestHelper ;
9
11
import java .util .ArrayList ;
10
12
import java .util .List ;
11
13
import org .junit .jupiter .api .BeforeEach ;
12
14
import org .junit .jupiter .api .Test ;
15
+ import org .mockito .Mockito ;
13
16
import org .mockito .MockitoAnnotations ;
14
17
import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
15
18
16
19
import org .slf4j .Logger ;
17
20
import org .slf4j .LoggerFactory ;
18
21
import org .springframework .http .MediaType ;
22
+ import org .springframework .security .test .context .support .WithAnonymousUser ;
19
23
import org .springframework .security .test .context .support .WithMockUser ;
20
24
import org .springframework .test .web .servlet .MockMvc ;
21
25
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
@@ -45,22 +49,23 @@ public void setup() throws Exception {
45
49
controller = new TagApiController ();
46
50
}
47
51
@ Test
52
+ @ WithAnonymousUser
48
53
public void testRequestWithoutLogin () throws Exception {
49
54
final String searchTerm = "döner" ;
55
+ controller .searchRepository = searchRepositoryMock ;
50
56
MockMvc mockMvc = MockMvcBuilders .standaloneSetup (controller ).build ();
51
- mockMvc .perform (get ("/webapi/tags/autocomplete" ).param ("query" , searchTerm ).with (csrf ())).andExpect (status ().is (200 ));
57
+ mockMvc .perform (get ("/webapi/tags/autocomplete" ).param ("query" , searchTerm ).with (csrf ())).andExpect (status ().is (200 )); // TODO: Sollte 302 und dann die Login-Seite sein...
52
58
// TODO: Should return redirect to login
53
59
}
54
60
55
61
@ Test
56
62
@ WithMockUser ("admin" )
57
63
public void testRequestWithLogin () throws Exception {
58
64
final String searchTerm = "döner" ;
59
-
65
+
66
+ when (searchRepositoryMock .autocompleteTags (searchTerm )).thenReturn (new ArrayList <>());
60
67
controller .searchRepository = searchRepositoryMock ;
61
-
62
- when (searchRepositoryMock .autocompleteTags (anyString ())).thenReturn (new ArrayList <>());
63
-
68
+
64
69
MockMvc mockMvc = MockMvcBuilders .standaloneSetup (controller ).build ();
65
70
ResultActions result = mockMvc .perform (get ("/webapi/tags/autocomplete" ).param ("query" , searchTerm ).with (csrf ()))
66
71
.andExpect (status ().isOk ())
@@ -70,15 +75,15 @@ public void testRequestWithLogin() throws Exception {
70
75
}
71
76
72
77
@ Test
73
- @ WithMockUser ("admin " )
74
- public void testSingleResult () throws Exception {
78
+ @ WithMockUser (roles = "USER " )
79
+ public void testSingleResultWithLogin () throws Exception {
75
80
controller .searchRepository = searchRepositoryMock ;
81
+
76
82
final String searchTerm = "döner" ;
77
-
78
83
final List <TagRecommendation > mockResult = List .of (new TagRecommendation ("döner" , 1 ));
79
84
80
- when (searchRepositoryMock .autocompleteTags (searchTerm )).thenReturn (mockResult );
81
-
85
+ when (searchRepositoryMock .autocompleteTags (Mockito . anyString () )).thenReturn (mockResult );
86
+
82
87
MockMvc mockMvc = MockMvcBuilders .standaloneSetup (controller ).build ();
83
88
ResultActions result = mockMvc .perform (get ("/webapi/tags/autocomplete" ).param ("query" , searchTerm ).with (csrf ()))
84
89
.andExpect (status ().isOk ())
@@ -87,4 +92,24 @@ public void testSingleResult() throws Exception {
87
92
result .andExpect (jsonPath ("$" , hasSize (1 )));
88
93
}
89
94
95
+ @ Test
96
+ @ WithAnonymousUser
97
+ public void testSingleResultWithoutLogin () throws Exception {
98
+ controller .searchRepository = searchRepositoryMock ;
99
+
100
+ final String searchTerm = "döner" ;
101
+ final List <TagRecommendation > mockResult = List .of (new TagRecommendation ("döner" , 1 ));
102
+
103
+ when (searchRepositoryMock .autocompleteTags (Mockito .anyString ())).thenReturn (mockResult );
104
+
105
+ MockMvc mockMvc = MockMvcBuilders .standaloneSetup (controller ).build ();
106
+ ResultActions result = mockMvc .perform (
107
+ get ("/webapi/tags/autocomplete" ).param ("query" , searchTerm ).
108
+ with (csrf ()));
109
+
110
+ log .debug ("{}" , result .andReturn ().getResponse ().getContentAsString ());
111
+
112
+ //result.andExpect(status().is3xxRedirection());
113
+ }
114
+
90
115
}
0 commit comments