This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package com.starry.admin.api;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.starry.admin.common.apitest.ApiTestDataSeeder;
|
||||
import com.starry.admin.modules.blindbox.module.entity.BlindBoxConfigEntity;
|
||||
import com.starry.admin.modules.blindbox.service.BlindBoxConfigService;
|
||||
import com.starry.admin.modules.order.module.constant.OrderConstant;
|
||||
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
|
||||
import com.starry.admin.utils.SecurityUtils;
|
||||
import com.starry.common.constant.Constants;
|
||||
import com.starry.common.context.CustomSecurityContextHolder;
|
||||
import com.starry.common.utils.IdUtils;
|
||||
import java.math.BigDecimal;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
class WxBlindBoxOrderApiTest extends WxCustomOrderApiTestSupport {
|
||||
|
||||
@Autowired
|
||||
private BlindBoxConfigService blindBoxConfigService;
|
||||
|
||||
@Test
|
||||
void blindBoxPurchaseFailsWhenBalanceInsufficient() throws Exception {
|
||||
SecurityUtils.setTenantId(ApiTestDataSeeder.DEFAULT_TENANT_ID);
|
||||
String configId = "blind-" + IdUtils.getUuid();
|
||||
try {
|
||||
BlindBoxConfigEntity config = new BlindBoxConfigEntity();
|
||||
config.setId(configId);
|
||||
config.setTenantId(ApiTestDataSeeder.DEFAULT_TENANT_ID);
|
||||
config.setName("API盲盒-" + IdUtils.getUuid().substring(0, 6));
|
||||
config.setPrice(new BigDecimal("66.00"));
|
||||
config.setStatus(1);
|
||||
blindBoxConfigService.save(config);
|
||||
|
||||
setCustomerBalance(BigDecimal.ZERO);
|
||||
String customerToken = wxTokenService.createWxUserToken(ApiTestDataSeeder.DEFAULT_CUSTOMER_ID);
|
||||
customUserInfoService.updateTokenById(ApiTestDataSeeder.DEFAULT_CUSTOMER_ID, customerToken);
|
||||
|
||||
ensureTenantContext();
|
||||
long beforeCount = playOrderInfoService.lambdaQuery()
|
||||
.eq(PlayOrderInfoEntity::getPurchaserBy, ApiTestDataSeeder.DEFAULT_CUSTOMER_ID)
|
||||
.eq(PlayOrderInfoEntity::getOrderType, OrderConstant.OrderType.BLIND_BOX_PURCHASE.getCode())
|
||||
.count();
|
||||
|
||||
String payload = "{" +
|
||||
"\"blindBoxId\":\"" + configId + "\"," +
|
||||
"\"clerkId\":\"" + ApiTestDataSeeder.DEFAULT_CLERK_ID + "\"," +
|
||||
"\"weiChatCode\":\"apitest-customer-wx\"" +
|
||||
"}";
|
||||
|
||||
mockMvc.perform(post("/wx/blind-box/order/purchase")
|
||||
.header(USER_HEADER, DEFAULT_USER)
|
||||
.header(TENANT_HEADER, DEFAULT_TENANT)
|
||||
.header(Constants.CUSTOM_USER_LOGIN_TOKEN, Constants.TOKEN_PREFIX + customerToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(payload))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(998));
|
||||
|
||||
ensureTenantContext();
|
||||
long afterCount = playOrderInfoService.lambdaQuery()
|
||||
.eq(PlayOrderInfoEntity::getPurchaserBy, ApiTestDataSeeder.DEFAULT_CUSTOMER_ID)
|
||||
.eq(PlayOrderInfoEntity::getOrderType, OrderConstant.OrderType.BLIND_BOX_PURCHASE.getCode())
|
||||
.count();
|
||||
Assertions.assertThat(afterCount).isEqualTo(beforeCount);
|
||||
} finally {
|
||||
blindBoxConfigService.removeById(configId);
|
||||
CustomSecurityContextHolder.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user