Skip to content

Commit 41726ec

Browse files
committed
10_02_HW9_test
1 parent e68e747 commit 41726ec

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

src/test/java/ru/javawebinar/topjava/web/RootControllerTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package ru.javawebinar.topjava.web;
22

3-
import org.junit.jupiter.api.Disabled;
43
import org.junit.jupiter.api.Test;
54

65
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
76
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
87
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
9-
import static ru.javawebinar.topjava.MealTestData.meals;
108
import static ru.javawebinar.topjava.TestUtil.userAuth;
119
import static ru.javawebinar.topjava.UserTestData.admin;
12-
import static ru.javawebinar.topjava.util.MealsUtil.getTos;
10+
import static ru.javawebinar.topjava.UserTestData.user;
1311

1412
class RootControllerTest extends AbstractControllerTest {
1513

@@ -32,13 +30,12 @@ void unAuth() throws Exception {
3230
}
3331

3432
@Test
35-
@Disabled
3633
void getMeals() throws Exception {
37-
perform(get("/meals"))
34+
perform(get("/meals")
35+
.with(userAuth(user)))
3836
.andDo(print())
3937
.andExpect(status().isOk())
4038
.andExpect(view().name("meals"))
41-
.andExpect(forwardedUrl("/WEB-INF/jsp/meals.jsp"))
42-
.andExpect(model().attribute("meals", getTos(meals, SecurityUtil.authUserCaloriesPerDay())));
39+
.andExpect(forwardedUrl("/WEB-INF/jsp/meals.jsp"));
4340
}
4441
}

src/test/java/ru/javawebinar/topjava/web/meal/MealRestControllerTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ru.javawebinar.topjava.web.meal;
22

33

4-
import org.junit.jupiter.api.Disabled;
54
import org.junit.jupiter.api.Test;
65
import org.springframework.beans.factory.annotation.Autowired;
76
import org.springframework.http.MediaType;
@@ -18,12 +17,12 @@
1817
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
1918
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2019
import static ru.javawebinar.topjava.MealTestData.*;
20+
import static ru.javawebinar.topjava.TestUtil.userHttpBasic;
2121
import static ru.javawebinar.topjava.UserTestData.USER_ID;
2222
import static ru.javawebinar.topjava.UserTestData.user;
2323
import static ru.javawebinar.topjava.util.MealsUtil.createTo;
2424
import static ru.javawebinar.topjava.util.MealsUtil.getTos;
2525

26-
@Disabled
2726
class MealRestControllerTest extends AbstractControllerTest {
2827

2928
private static final String REST_URL = MealRestController.REST_URL + '/';
@@ -33,16 +32,24 @@ class MealRestControllerTest extends AbstractControllerTest {
3332

3433
@Test
3534
void get() throws Exception {
36-
perform(MockMvcRequestBuilders.get(REST_URL + MEAL1_ID))
35+
perform(MockMvcRequestBuilders.get(REST_URL + MEAL1_ID)
36+
.with(userHttpBasic(user)))
3737
.andExpect(status().isOk())
3838
.andDo(print())
3939
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
4040
.andExpect(MEAL_MATCHER.contentJson(meal1));
4141
}
4242

43+
@Test
44+
void getUnauth() throws Exception {
45+
perform(MockMvcRequestBuilders.get(REST_URL + MEAL1_ID))
46+
.andExpect(status().isUnauthorized());
47+
}
48+
4349
@Test
4450
void delete() throws Exception {
45-
perform(MockMvcRequestBuilders.delete(REST_URL + MEAL1_ID))
51+
perform(MockMvcRequestBuilders.delete(REST_URL + MEAL1_ID)
52+
.with(userHttpBasic(user)))
4653
.andExpect(status().isNoContent());
4754
assertThrows(NotFoundException.class, () -> mealService.get(MEAL1_ID, USER_ID));
4855
}
@@ -51,6 +58,7 @@ void delete() throws Exception {
5158
void update() throws Exception {
5259
Meal updated = getUpdated();
5360
perform(MockMvcRequestBuilders.put(REST_URL + MEAL1_ID).contentType(MediaType.APPLICATION_JSON)
61+
.with(userHttpBasic(user))
5462
.content(JsonUtil.writeValue(updated)))
5563
.andExpect(status().isNoContent());
5664

@@ -62,6 +70,7 @@ void createWithLocation() throws Exception {
6270
Meal newMeal = getNew();
6371
ResultActions action = perform(MockMvcRequestBuilders.post(REST_URL)
6472
.contentType(MediaType.APPLICATION_JSON)
73+
.with(userHttpBasic(user))
6574
.content(JsonUtil.writeValue(newMeal)))
6675
.andExpect(status().isCreated());
6776

@@ -74,7 +83,8 @@ void createWithLocation() throws Exception {
7483

7584
@Test
7685
void getAll() throws Exception {
77-
perform(MockMvcRequestBuilders.get(REST_URL))
86+
perform(MockMvcRequestBuilders.get(REST_URL)
87+
.with(userHttpBasic(user)))
7888
.andExpect(status().isOk())
7989
.andDo(print())
8090
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
@@ -85,15 +95,17 @@ void getAll() throws Exception {
8595
void getBetween() throws Exception {
8696
perform(MockMvcRequestBuilders.get(REST_URL + "filter")
8797
.param("startDate", "2020-01-30").param("startTime", "07:00")
88-
.param("endDate", "2020-01-31").param("endTime", "11:00"))
98+
.param("endDate", "2020-01-31").param("endTime", "11:00")
99+
.with(userHttpBasic(user)))
89100
.andExpect(status().isOk())
90101
.andDo(print())
91102
.andExpect(TO_MATCHER.contentJson(createTo(meal5, true), createTo(meal1, false)));
92103
}
93104

94105
@Test
95106
void getBetweenAll() throws Exception {
96-
perform(MockMvcRequestBuilders.get(REST_URL + "filter?startDate=&endTime="))
107+
perform(MockMvcRequestBuilders.get(REST_URL + "filter?startDate=&endTime=")
108+
.with(userHttpBasic(user)))
97109
.andExpect(status().isOk())
98110
.andExpect(TO_MATCHER.contentJson(getTos(meals, user.getCaloriesPerDay())));
99111
}

0 commit comments

Comments
 (0)