Fakultas Ilmu Komputer UI

Commit 1bb91b17 authored by MUHAMMAD AAQIL ABDULLAH's avatar MUHAMMAD AAQIL ABDULLAH
Browse files

Fix tests

parents c1bc9f00 9883e969
Pipeline #134285 canceled with stages
in 20 minutes and 20 seconds
......@@ -63,7 +63,8 @@ public class AdminController {
}
@GetMapping(path="/list-payment")
public String paymentList(Model model){
public String paymentList(Model model, Principal principal){
model.addAttribute("user", userService.getUserByName(principal.getName()));
model.addAttribute("payments", paymentService.getAllPayments());
return "admin-payment-list";
}
......
......@@ -3,6 +3,7 @@ package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.forms.AddPaymentForm;
import id.ac.ui.cs.advprog.landiandfriends.model.PaymentModel;
import id.ac.ui.cs.advprog.landiandfriends.service.PaymentServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -10,13 +11,17 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.security.Principal;
@Controller
public class PaymentController {
private final PaymentServiceImpl paymentService;
private final UserServiceImpl userService;
public PaymentController(PaymentServiceImpl paymentService) {
public PaymentController(PaymentServiceImpl paymentService, UserServiceImpl userService) {
this.paymentService = paymentService;
this.userService = userService;
}
@GetMapping(path={"/add-card"})
......@@ -33,9 +38,10 @@ public class PaymentController {
}
@GetMapping(path="/payment-methods")
public String paymentList(Model model){
public String paymentList(Model model, Principal principal){
paymentService.getUserPayments();
model.addAttribute("payments", paymentService.getUserPayments());
model.addAttribute("user", userService.getUserByName(principal.getName()));
return "payment-methods";
}
......
......@@ -34,7 +34,8 @@ public class ProfileController {
}
@GetMapping(path = "/delivery-address")
public String addressList(Model model) {
public String addressList(Model model, Principal principal) {
model.addAttribute("user", userService.getUserByName(principal.getName()));
model.addAttribute("address", addressService.getAddress());
return "my-address";
}
......
......@@ -30,11 +30,11 @@
/* Style the search box inside the navigation bar */
.search[type=text] {
float: right;
width: 50%;
float: left;
width: 80%;
padding: 6px;
border: none;
margin-right: 2px;
margin-right: 10px;
padding-left: 10px;
font-size: 17px;
}
......@@ -42,12 +42,55 @@
padding-top: 5px;
min-width: 150px;
}
.forminput{
width:100%;
}
</style>
</head>
<body>
<div class="header mb-4">
<div style="width: 1%"></div>
<a href="/"><img src="https://i.imgur.com/cyBT3sP.png" alt="RBS" width="100" height="100" style="padding-left: 5px"></a>
<a href="/"><img src="https://i.imgur.com/aREsEE8.png" alt="Ryan's Book Store" height="100"></a>
<div style="width: 1%"></div>
<div class="forminput">
<form id="search-form" th:action="@{/book/search/}" method="get">
<input id = "bookTitle" class="search" type="text" placeholder="Search by title..">
<button type="submit">
<img src="https://i.imgur.com/WWk7FEt.png" class = "search-button" alt="search" height="40">
</button>
</form>
</div>
<div style="width: 3%"></div>
<a href="/wishlist"><img src="https://i.imgur.com/HwTZHG8.png" class = "wishlist-button" alt="wishlist" height="40"></a>
<div style="width: 3%"></div>
<a href="/cart"><img src="https://i.imgur.com/EPveDOI.png" class = "cart-button" alt="cart" height="40"></a>
<div style="width: 3%"></div>
<a href ="/profile"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" height="40" style="padding-right: 10px"></a>
<a href ="/profile" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${user.username}"></a>
</div>
<div class = "container">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link " href="/profile">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/delivery-address">Delivery Address</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('USER')}">
<a class="nav-link" href="/payment-methods">Payment Method</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('ADMIN')}">
<a class="nav-link" href="/admin/list-payment">Admin Payment Methods</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('CURATOR')}">
<a class="nav-link" href="/curator">Curator</a>
</li>
</ul>
</div>
<div class="container">
<h2 class="subtitle">Payment Methods</h2>
<button class="btn btn-outline-primary" th:onclick="|window.location.href='/admin/admin-add-card'|">Add Card</button>
<div class="row">
<div class="col-md-4">
<div th:each="payment : ${payments}" >
......
......@@ -65,17 +65,23 @@
<body>
<div class = "container">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link" href="/profile">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/delivery-address">Delivery Address</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/payment-methods">Payment Method</a>
</li>
</ul>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link " href="/profile">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/delivery-address">Delivery Address</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('USER')}">
<a class="nav-link" href="/payment-methods">Payment Method</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('ADMIN')}">
<a class="nav-link" href="/admin/list-payment">Admin Payment Methods</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('CURATOR')}">
<a class="nav-link" href="/curator">Curator</a>
</li>
</ul>
</div>
<div class="container">
......
......@@ -29,11 +29,11 @@
/* Style the search box inside the navigation bar */
.search[type=text] {
float: right;
width: 50%;
float: left;
width: 80%;
padding: 6px;
border: none;
margin-right: 2px;
margin-right: 10px;
padding-left: 10px;
font-size: 17px;
}
......@@ -41,27 +41,36 @@
padding-top: 5px;
min-width: 150px;
}
.forminput{
width:100%;
}
</style>
</head>
<body>
<div class="header">
<div class="header mb-4">
<div style="width: 1%"></div>
<img src="https://i.imgur.com/cyBT3sP.png" alt="RBS" width="100" height="100" style="padding-left: 5px">
<img src="https://i.imgur.com/aREsEE8.png" alt="Ryan's Book Store" height="100">
<a href="/"><img src="https://i.imgur.com/cyBT3sP.png" alt="RBS" width="100" height="100" style="padding-left: 5px"></a>
<a href="/"><img src="https://i.imgur.com/aREsEE8.png" alt="Ryan's Book Store" height="100"></a>
<div style="width: 1%"></div>
<input class="search" type="text" placeholder="Search by title..">
<img src="https://i.imgur.com/WWk7FEt.png" class = "search-button" alt="search" height="40">
<div class="forminput">
<form id="search-form" th:action="@{/book/search/}" method="get">
<input id = "bookTitle" class="search" type="text" placeholder="Search by title..">
<button type="submit">
<img src="https://i.imgur.com/WWk7FEt.png" class = "search-button" alt="search" height="40">
</button>
</form>
</div>
<div style="width: 3%"></div>
<a href="/wishlist"><img src="https://i.imgur.com/HwTZHG8.png" class = "wishlist-button" alt="wishlist" height="40"></a>
<div style="width: 3%"></div>
<a href="/cart"><img src="https://i.imgur.com/EPveDOI.png" class = "cart-button" alt="cart" height="40"></a>
<div style="width: 3%"></div>
<a th:href ="${'/profile'}"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" alt="sign in" height="40" style="padding-right: 10px"></a>
<a th:href ="${'/profile'}" class="sign-in-text" style="text-decoration: none; color: inherit" >Sign In</a>
<a href ="/profile"><img src="https://i.imgur.com/KxpWso4.png" class = "signin-button" height="40" style="padding-right: 10px"></a>
<a href ="/profile" class="sign-in-text" style="text-decoration: none; color: inherit" th:text="${user.username}"></a>
</div>
<div class = "container">
<body>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link " href="/profile">Profile</a>
......@@ -69,8 +78,14 @@
<li class="nav-item">
<a class="nav-link" href="/delivery-address">Delivery Address</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/payment-methods">Payment Method</a>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('USER')}">
<a class="nav-link" href="/payment-methods">Payment Method</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('ADMIN')}">
<a class="nav-link" href="/admin/list-payment">Admin Payment Methods</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('CURATOR')}">
<a class="nav-link" href="/curator">Curator</a>
</li>
</ul>
</div>
......
......@@ -80,17 +80,23 @@
</div>
<div class = "container">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/delivery-address">Delivery Address</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/payment-methods">Payment Method</a>
</li>
</ul>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link " href="/profile">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/delivery-address">Delivery Address</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('USER')}">
<a class="nav-link" href="/payment-methods">Payment Method</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('ADMIN')}">
<a class="nav-link" href="/admin/list-payment">Admin Payment Methods</a>
</li>
<li class="nav-item" th:if="${user.getRoles() != null && user.getRoles().getName().equals('CURATOR')}">
<a class="nav-link" href="/curator">Curator</a>
</li>
</ul>
</div>
<div class="container">
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.Role;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.security.RyanUserDetails;
import id.ac.ui.cs.advprog.landiandfriends.service.PaymentServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
......@@ -156,6 +163,11 @@ class AdminControllerTest {
@Test
@WithMockUser(authorities={"ADMIN"})
void whenGetPaymentListPage() throws Exception {
var user = new User();
user.setUsername("test");
user.setRoles(new Role("test"));
when(userService.getUserByName(any())).thenReturn(new User());
mockMvc.perform(get("/admin/list-payment"))
.andExpect(model().attributeExists("payments"))
.andExpect(handler().methodName("paymentList"))
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.PaymentModel;
import id.ac.ui.cs.advprog.landiandfriends.model.Role;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.UserRepository;
import id.ac.ui.cs.advprog.landiandfriends.service.PaymentServiceImpl;
......@@ -80,8 +81,13 @@ class PaymentControllerTest {
@Test
@WithMockUser(authorities={"USER"})
void whenGetPaymentMethodsAuthenticated() throws Exception {
var user = new User();
user.setUsername("test");
user.setRoles(new Role("test"));
List<PaymentModel> paymentModels = new ArrayList<>();
when(paymentService.getUserPayments()).thenReturn(paymentModels);
when(userService.getUserByName(any())).thenReturn(user);
mockMvc.perform(get("/payment-methods"))
.andExpect(status().isOk())
......
......@@ -47,13 +47,16 @@ class ProfileControllerTest {
private UserRepository userRepository;
@Test
@WithMockUser(authorities={"USER"})
void addressList() throws Exception{
User user = new User();
var user = new User();
user.setEmail("test@gmail.com");
user.setUsername("fauzan");
user.setPassword("12345");
AddressModel addressModel = new AddressModel();
when(userService.getUserByName(any())).thenReturn(user);
var addressModel = new AddressModel();
addressModel.setCity("Jakarta");
addressModel.setDescription("Jalan Tebet Timur");
addressModel.setPostalCode("12345");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment