
def test_signup(user, assert_error):
    user("test")
    user("jack", email="<EMAIL>")
    with assert_error(400, "IDConflict"):
        user("test")
    with assert_error(400, "EmailConflict"):
        user("jack1234", email="<EMAIL>")


def test_login(res, user, assert_error):
    with assert_error(403, "UserNotFound"):
        res.user.post_login({"account": "jack", "password": "<PASSWORD>"})
    user("jack", email="<EMAIL>")
    with assert_error(403, "WrongPassword"):
        res.user.post_login({"account": "jack", "password": "<PASSWORD>"})
    res.user.post_login({
        "account": "<EMAIL>",
        "password": "<PASSWORD>"
    })


def test_get_me(res, user, assert_error):
    jack = user("jack")
    me = jack.user.get_me()
    assert me["id"] == "jack"
    with assert_error(403):
        res.user.get_me()


def test_get(user):
    jack = user("jack")
    jack_id = jack.user.get_me()["id"]
    tom = user("tom")
    assert tom.user.get({"id": jack_id})["id"] == "jack"


def test_put(user):
    jack = user("jack")
    me = jack.user.get_me()
    assert me["github"] == ""
    assert me["avatar"] == "http://purepage.org/static/avatar-default.png"
    jack.user.put({
        "github": "https://github.com/jack",
        "avatar": "http://purepage.org/static/logo.png"
    })
    me = jack.user.get_me()
    assert me["github"] == "https://github.com/jack"
    assert me["avatar"] == "http://purepage.org/static/logo.png"


def test_put_email(res, user, assert_error):
    jack = user("jack")
    with assert_error(403):
        res.user.post_login({
            "account": "<EMAIL>",
            "password": "<PASSWORD>"
        })
    jack.user.put_email({
        "email": "<EMAIL>",
        "password": "<PASSWORD>"
    })
    res.user.post_login({
        "account": "<EMAIL>",
        "password": "<PASSWORD>"
    })


def test_put_password(res, user, assert_error):
    jack = user("jack")
    with assert_error(403):
        res.user.post_login({
            "account": "jack",
            "password": "<PASSWORD>"
        })
    jack.user.put_password({
        "new_password": "<PASSWORD>",
        "password": "<PASSWORD>"
    })
    res.user.post_login({
        "account": "jack",
        "password": "<PASSWORD>"
    })
    with assert_error(403):
        res.user.post_login({
            "account": "jack",
            "password": "<PASSWORD>"
        })


def test_forgot_reset(res, user, assert_error):
    import re
    from purepage.ext import mail
    user("tom")
    with assert_error(400, "UserNotFound"):
        res.user.post_forgot({"email": "<EMAIL>"})
    user("jack", email="<EMAIL>")
    with mail.record_messages() as outbox:
        res.user.post_forgot({"email": "<EMAIL>"})
    assert outbox[0].subject == "PurePage重置密码"
    token = re.findall(r'http://purepage.org/reset/(.*)"', outbox[0].html)
    assert token
    token = token[0]
    # TODO: 基于时间戳的验证机制待改进
    import time
    time.sleep(1)
    with assert_error(403, "InvalidToken"):
        res.user.post_reset({
            "token": "xxxxxx",
            "password": "<PASSWORD>"
        })
    res.user.post_reset({
        "token": token,
        "password": "<PASSWORD>"
    })
    # Token只能用一次
    with assert_error(403, "InvalidToken"):
        res.user.post_reset({
            "token": token,
            "password": "<PASSWORD>"
        })
    res.user.post_login({
        "account": "jack",
        "password": "<PASSWORD>"
    })
    with assert_error(403):
        res.user.post_login({
            "account": "jack",
            "password": "<PASSWORD>"
        })
