package cn.edu.ecnu.stu.bookstore;

import cn.edu.ecnu.stu.bookstore.component.Constants;
import cn.edu.ecnu.stu.bookstore.component.Result;
import cn.edu.ecnu.stu.bookstore.mapper.OrderMapper;
import cn.edu.ecnu.stu.bookstore.util.RequestUtil;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@SpringBootTest
public class AuthTest {

    @Test
    public void testRegister() {
        String url = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        String username = UUID.randomUUID().toString();
        String password = username + "x";
        map.put("username", username);
        map.put("password", password);
        Result result = RequestUtil.post(url, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        map.put("username", username);
        result = RequestUtil.post(url, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.CLIENT_ERROR);
    }

    @Test
    public void testLogin() {
        String url = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        String username = UUID.randomUUID().toString();
        String password = username + "x";
        map.put("username", username);
        map.put("password", password);
        Result result = RequestUtil.post(url, map, null);

        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        url = Constants.URL_PREFIX + "/auth/login";
        result = RequestUtil.post(url, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        map.put("password", password + "x");
        result = RequestUtil.post(url, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.AUTHENTICATION_ERROR);
    }

    @Test
    public void testUnregister() {
        String registerUrl = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        String username = UUID.randomUUID().toString();
        String password = username + "x";
        map.put("username", username);
        map.put("password", password);
        Result result = RequestUtil.post(registerUrl, map, null);

        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        String loginUrl = Constants.URL_PREFIX + "/auth/login";
        result = RequestUtil.post(loginUrl, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        String unregisterUrl = Constants.URL_PREFIX + "/auth/unregister";
        result = RequestUtil.post(unregisterUrl, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        result = RequestUtil.post(loginUrl, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.CLIENT_ERROR);
    }

    @Test
    public void testLogout() {
        String registerUrl = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        String username = UUID.randomUUID().toString();
        String password = username + "x";
        map.put("username", username);
        map.put("password", password);
        Result result = RequestUtil.post(registerUrl, map, null);

        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        String loginUrl = Constants.URL_PREFIX + "/auth/login";
        result = RequestUtil.post(loginUrl, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        JSONObject data = (JSONObject) result.getData();
        String token = data.getString("token");
        String testUrl = Constants.URL_PREFIX + "/test";
        result = RequestUtil.get(testUrl, null, token);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        String logoutUrl = Constants.URL_PREFIX + "/auth/logout";
        result = RequestUtil.post(logoutUrl, map, token);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        result = RequestUtil.get(testUrl, null, token);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.AUTHENTICATION_ERROR);
    }

    @Test
    public void testPassword() {
        String registerUrl = Constants.URL_PREFIX + "/auth/register";
        Map<String, Object> map = new HashMap<>();
        String username = UUID.randomUUID().toString();
        String password = username + "x";
        map.put("username", username);
        map.put("password", password);
        Result result = RequestUtil.post(registerUrl, map, null);

        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        String newPassword = password + "x";
        map.put("oldPassword", password);
        map.put("newPassword", newPassword);
        String passwordUrl = Constants.URL_PREFIX + "/auth/password";
        result = RequestUtil.post(passwordUrl, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);

        map.put("password", newPassword);
        String loginUrl = Constants.URL_PREFIX + "/auth/login";
        result = RequestUtil.post(loginUrl, map, null);
        assert result != null && result.getCode() != null && result.getCode().equals(Constants.SUCCESS);
    }

}

