Ejemplo n.º 1
0
def test_add_remove_recipe(client, user, product, ingredient):
    product_id = product.product_id
    ingredient_id = ingredient.ingredient_id
    conftest.login(client, user)
    assert Recipe.query.filter(
        and_(Recipe.product_id == product_id, Recipe.ingredient_id
             == ingredient_id)).first() is None
    unit = Unit.query.get(ingredient.unit_id)
    if unit.symbol:
        name = unit.symbol
    else:
        name = unit.name
    data = {'ingredient': ingredient.name, 'amount': 500, 'unit': name}
    client.post(f"{conftest.ROUTES['view_product']}{product_id}/", data=data)
    assert Recipe.query.filter(
        and_(Recipe.product_id == product_id,
             Recipe.ingredient_id == ingredient_id)).first() is not None
    assert (conftest.MESSAGES['existing_ingredient']
            in client.post(f"{conftest.ROUTES['view_product']}{product_id}/",
                           data=data).data)
    client.get(f"{conftest.ROUTES['view_product']}{product_id}"
               f"{conftest.ROUTES['delete_ingredient']}{ingredient_id}/")
    assert Recipe.query.filter(
        and_(Recipe.product_id == product_id, Recipe.ingredient_id
             == ingredient_id)).first() is None
    conftest.logout(client)
Ejemplo n.º 2
0
def test_group_binding(user, group_admin, client: FlaskClient):
    login(client)

    resp = client.post(url_for("relation.RelationView"))  # 获取验证码

    data = {
        "platform": "qq",
        "message": {  # 如果 update 类型是 message
            "type": "group",  # 'private' or 'group'
            "user": {
                "id": "123456",  # 用户 id,QQ 号等
                "nickname": "riki",
                "role": "owner",  # 群组 owner/admin/other
            },
            "group": {
                "id": "234567",  # 群 id
                "name": "234567",
                "at_me": False,  # 是否是 at 我
            },
            "text":
            f"?群组绑定 {resp.json['verification_code']}",  # 消息的 text 部分。(去除掉了表情、at 和多媒体数据)
        },
    }
    reply = dispatcher.handle_update(data)
    assert reply['message']['text'] == "绑定完成!"
    group = get_group(platform="qq", group_id="234567")
    assert group is not None
    assert group in user.auth_groups
Ejemplo n.º 3
0
def test_post_table_query(client: FlaskClient):
    login(client)

    payload = {
        "version": "0.0.1",
        "table_name": "小鹤音形拆字表",
        "table_type": "xhup",
        "table": """比: bi bibb*=拆分: 比左 匕=首末: 比左 匕=编码: b  b
顷: qkb qkbr=拆分: 比左 一 ノ 冂 人=首末: 比左 人=编码: b  r
皆: jpb jpbb=拆分: 比左 匕 白=首末: 比左 白=编码: b  b""",
        "group_id": group_id,
        "platform": platform,
    }
    resp: Response = client.post(url_for("characters.TableView"), json=payload)

    assert resp.status_code == 201
    assert resp.json['version'] == payload['version']

    resp_2: Response = client.get(url_for("characters.CharView"),
                                  data={
                                      "char": "比",
                                      "version": "0.0.1",
                                      "table_name": "小鹤音形拆字表"
                                  })

    info = resp_2.json['info']
    assert info['codes'] == "bi bibb*"
    assert info['split'] == '比左 匕'
    assert info['other_info'] == {'编码': 'b b', '首末': '比左 匕'}
Ejemplo n.º 4
0
def test_delete_product(client, user, product):
    product_id = product.product_id
    conftest.login(client, user)
    client.get(f"{conftest.ROUTES['view_product']}"
               f"{product_id}{conftest.ROUTES['delete_product']}")
    assert Product.query.get(product_id) is None
    conftest.logout(client)
Ejemplo n.º 5
0
def test_edit_profile(client, user, user2, path, data, message):
    conftest.login(client, user)
    assert message in client.post(path, data=data, follow_redirects=True).data
    if User.query.get(user.user_id).email == NEW_EMAIL:
        User.query.get(user.user_id).email = EMAIL
        conftest.db.session.commit()
    conftest.logout(client)
Ejemplo n.º 6
0
def test_edit_available_upon_login(client, user):
    user_id = user.user_id
    assert (conftest.MESSAGES['edit']
            not in client.get(f"{conftest.ROUTES['profile']}{user_id}/").data)
    conftest.login(client, user)
    assert (conftest.MESSAGES['edit']
            in client.get(f"{conftest.ROUTES['profile']}{user_id}/").data)
    conftest.logout(client)
Ejemplo n.º 7
0
    def test_get(self, client: FlaskClient):
        login(client)

        resp: Response = client.get(url_for("session.SessionView"))

        assert resp.status_code == 200
        assert resp.json['username'] == username
        assert resp.json['email'] == email
Ejemplo n.º 8
0
 def test_alreadyLoggedIn2(self, testClient, testUser2):
     self.form = {"username": "******", 'password': '******'}
     with testClient:
         login(testClient, "sarahsmith", 'password1234')
         assert current_user == testUser2
         response = testClient.get(url_for('main.index', _external=False),
                                   follow_redirects=True)
         assert b'Search for Business' in response.data
         logout(testClient)
Ejemplo n.º 9
0
def test_group_user_put(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)

    creds = {401: r_std, 200: r_adm}
    for cred in creds:
        response = client.put('/group/2/user/2', headers=creds[cred].headers)
        assert response.status_code == cred
Ejemplo n.º 10
0
def test_user_key_get_route(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)

    creds = [r_std, r_adm]
    for cred in creds:
        response = client.get('/user/2/keys', headers=cred.headers)
        assert response.status_code == 200
        assert response.json == keys.user_key_get_response
Ejemplo n.º 11
0
def test_group_id_get(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)

    creds = [r_std, r_adm]
    for cred in creds:
        response = client.get('/group/2', headers=cred.headers)
        print(response.json)
        assert response.json == groups.group_id_template
Ejemplo n.º 12
0
def test_share_product(client, user, product):
    conftest.login(client, user)
    product_id = product.product_id
    public = product.public
    client.get(f"{conftest.ROUTES['view_product']}{product_id}"
               f"{conftest.ROUTES['share']}")
    assert Product.query.get(product_id).public != public
    client.get(f"{conftest.ROUTES['view_product']}{product_id}"
               f"{conftest.ROUTES['share']}")
    assert Product.query.get(product_id).public == public
    conftest.logout(client)
Ejemplo n.º 13
0
    def test_get(self, client: FlaskClient):
        """
        测试获取账号信息
        """
        login(client)

        resp: Response = client.get(url_for("user.UserView"))

        assert resp.status_code == 200
        assert resp.json['username'] == username
        assert resp.json['email'] == email
Ejemplo n.º 14
0
def test_product_add_success(client, user):
    conftest.login(client, user)
    data = {'name': 'test_product', 'amount': 500, 'unit': 'g', 'public': True}
    assert (client.post(
        conftest.ROUTES['new_product'], data=data,
        follow_redirects=True).status_code == conftest.HTTP_CODES['ok'])
    product = Product.query.filter(
        and_(Product.name == data['name'],
             Product.user_id == user.user_id)).first()
    assert product is not None
    conftest.delete(product)
    conftest.logout(client)
Ejemplo n.º 15
0
def test_keys_get_route(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)

    creds = {200: r_adm}
    for cred in creds:
        response = client.get('/keys', headers=creds[cred].headers)
        print(response.json)
        assert response.status_code == cred
        if response.status_code == 200:
            assert response.json == keys.keys_get_response
Ejemplo n.º 16
0
def test_comp_article_box(user, client: FlaskClient):
    login(client)

    # 1. 给定文档
    payload = {
        "content_type": "given_text",  # 给定文档
        "length": 500,
        # "delta": 30,  # 默认就是 30
        "count": 10,  # 添加赛文的最大篇数
        "content_subtype": "小说",  # 文档类型为小说
        "title": "青铜葵花",
        "text": test_text,
        # "separator": "",  # 如果提前用分割符分好段了的话
    }
    resp = client.post(url_for("comp_article.CompArticleBoxView"), json=payload)
    assert resp.status_code == 201
    assert resp.json == {
        "box_id": 1,
        'content_type': "小说",
        "count": 10
    }

    # 2. 随机散文
    payload = {
        "content_type": "random_article",  # 给定文档
        "length": 500,
        "count": 10,  # 添加赛文的最大篇数
    }
    resp = client.post(url_for("comp_article.CompArticleBoxView"), json=payload)
    assert resp.status_code == 201
    assert resp.json == {
        "box_id": 2,
        'content_type': "散文",
        "count": 10
    }

    # 3. 乱序单字
    payload = {
        "content_type": "shuffle_chars",  # 给定文档
        "length": 80,
        "count": 10,  # 添加赛文的最大篇数
        "content_subtype": "top_500"
    }
    resp = client.post(url_for("comp_article.CompArticleBoxView"), json=payload)
    assert resp.status_code == 201
    assert resp.json == {
        "box_id": 3,
        'content_type': "单字前五百",
        "count": 10
    }
Ejemplo n.º 17
0
    def test_delete(self, client: FlaskClient, db):
        """
        测试删除账号
        """
        login(client)

        assert db.session.query(MainUser).filter_by(
            username=username).first() is not None

        # 删除账号
        resp: Response = client.delete(url_for("user.UserView"),
                                       json={"password": password})

        assert resp.status_code == 204
        assert db.session.query(MainUser).filter_by(
            username=username).first() is None
Ejemplo n.º 18
0
def test_post_vote(test_client):
    """
    GIVEN: Flask app with test conf
    WHEN: '/post_full' rating section, valid rating count and message - login "vote up"/"vote up" after vote/logout vote
    THEN: vote == 1 without message/error message "already vote"/error message "need login"
    """
    login(test_client, app.config['USERNAME'], app.config['PASSWORD'])
    res = test_client.get('/post_full_vote', query_string=dict(vote='up'))
    assert b"\"rating\": 1" in res.data

    res = test_client.get('/post_full_vote', query_string=dict(vote='up'))
    assert b"You already rate this post" in res.data

    logout(test_client)
    rv = test_client.get('/post_full_vote')
    assert b"You need login for rate this post" in rv.data
 def test_delete_not_found(self, client):
     _ = login(client)
     response = client.post(
         "/blog/99/delete",
         allow_redirects=True,
     )
     assert response.status_code == 404
 def test_invalid_delete(self, client):
     _ = login(client, username="******")
     response = client.post(
         "/blog/1/delete",
         allow_redirects=True,
     )
     assert response.status_code == 401
Ejemplo n.º 21
0
def test_user_key_put(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)

    # Adding an SSH key to the admin account, admin should pass, standard should fail
    creds = {
        401: r_std,
        200: r_adm,
    }
    data = {
        'pub_ssh_key': TestConfig.ADMIN_PUB_SSH_KEY,
        'comment': 'Test Key',
    }
    for cred in creds.keys():
        response = client.put('/user/1/key',
                              json=data,
                              headers=creds[cred].headers)
        assert response.status_code == cred
        if response.status_code == 200:
            assert response.json['id'] == 1
            assert response.json['pub_ssh_key'] == TestConfig.ADMIN_PUB_SSH_KEY

    # Attempting to upload the same SSH key twice, should fail
    creds = {403: r_std, 403: r_adm}
    for cred in creds.keys():
        response = client.put('/user/2/key',
                              json=data,
                              headers=creds[cred].headers)
        print(response.json)
        assert response.status_code == cred

    # Adding a key to the standard account, both should pass
    data = {
        'pub_ssh_key': TestConfig.STANDARD_PUB_SSH_KEY,
        'comment': 'Test Key',
    }
    creds = {200: r_std, 200: r_adm}
    for cred in creds.keys():
        response = client.put('/user/2/key',
                              json=data,
                              headers=creds[cred].headers)
        print(response.json)
        assert response.status_code == cred
        assert response.json['id'] == 2
        assert response.json['pub_ssh_key'] == TestConfig.STANDARD_PUB_SSH_KEY
    def test_display_page(self, client):
        _ = login(client)
        response = client.get("/blog/create")

        assert response.status_code == 200
        assert b"New Post" in response.content
        assert b"Title" in response.content
        assert b"Body" in response.content
Ejemplo n.º 23
0
def test_users_path(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    assert r_std.token
    assert r_std.response.status_code == 200
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)
    assert r_adm.token
    assert r_adm.response.status_code == 200

    response = client.get('/users', headers=r_std.headers)
    assert response.status_code == 200
    assert response.json == users.users_get_response

    response = client.get('/users', headers=r_adm.headers)
    print(response.json)
    assert response.status_code == 200
    assert response.json == users.users_get_response
Ejemplo n.º 24
0
def test_add_post(test_client):
    """
    GIVEN: Flask app with test conf
    WHEN: '/add_post' POST method with data, login/logout
    THEN: check possible to add post login user, cant add on logout
    """
    login(test_client, app.config['USERNAME'], app.config['PASSWORD'])
    rv = test_client.post('/add_post',
                          data=dict(title='Hello', body='World'),
                          follow_redirects=True)
    assert b"Hello" in rv.data
    assert b"World" in rv.data

    logout(test_client)
    rv = test_client.post('/add_post',
                          data=dict(title='NOTHello', body='NOTWorld'),
                          follow_redirects=True)
    assert b"NOTHello" not in rv.data
    assert b"NOTWorld" not in rv.data
Ejemplo n.º 25
0
def test_char_query_handler(user, client: FlaskClient, group_admin):
    """3. 测试小鹤拆字命令"""
    # 1. 首先上传拆字表
    payload = {
        "version": "0.0.1",
        "table_name": "小鹤音形拆字表",
        "table_type": "xhup",
        "description": "最初的版本,小鹤音形是双拼+形的四键编码方案。",
        "table": """比: bi bibb*=拆分: 比左 匕=首末: 比左 匕=编码: b  b
    顷: qkb qkbr=拆分: 比左 一 ノ 冂 人=首末: 比左 人=编码: b  r
    皆: jpb jpbb=拆分: 比左 匕 白=首末: 比左 白=编码: b  b""",
        "group_id": group_id,
        "platform": platform,
    }
    login(client)
    character.save_char_table(**payload, main_user=user)

    # 2. 测试查字
    data = {
        "platform": "qq",
        "message": {  # 如果 update 类型是 message
            "type": "group",  # 'private' or 'group'
            "user": {
                "id": "123456",  # 用户 id,QQ 号等
                "role": "member",  # 群组 owner/admin/other
            },
            "group": {
                "id": "234567",  # 群 id
                "at_me": False,  # 是否是 at 我
            },
            "text": "?皆",  # 消息的 text 部分。(去除掉了表情、at 和多媒体数据)
        },
    }

    # 1. 处理 at_me 的情况
    reply = dispatcher.handle_update(data)
    assert reply
    assert reply['message']['text'] == "皆: jpb jpbb\n" \
                                       "拆分: 比左 匕 白\n" \
                                       "首末: 比左 白\n" \
                                       "编码: b b\n" \
                                       "汉典:http://xhup.club/?UQ"
    def test_delete_post(self, client):
        _ = login(client)

        response = client.post(
            "/blog/2/delete",
            allow_redirects=True,
        )
        assert response.status_code == 200

        assert b"update post" not in response.content
        assert b"update successfully" not in response.content
 def test_update_not_found(self, client):
     _ = login(client)
     response = client.post(
         "/blog/99/update",
         data={
             "title": "does not exist",
             "body": "cannot be updated"
         },
         allow_redirects=True,
     )
     assert response.status_code == 404
Ejemplo n.º 28
0
def test_group_put(client):
    r_std = login(client, TestConfig.STANDARD_USERNAME,
                  TestConfig.STANDARD_PASSWORD)
    r_adm = login(client, Config.APP_DEFAULT_USERNAME,
                  Config.APP_DEFAULT_PASSWORD)

    creds = {
        401: r_std,
        200: r_adm,
    }
    data = {'name': TestConfig.STANDARD_GROUP_NAME}
    for cred in creds:
        response = client.put(
            "/group",
            json={"name": TestConfig.STANDARD_GROUP_NAME},
            headers=creds[cred].headers,
        )
        assert response.status_code == cred
        if response.status_code == 200:
            assert response.json['name'] == 'Test Group'
    def test_invalid_update(self, client):
        _ = login(client, username="******")

        response = client.post(
            "/blog/2/update",
            data={
                "title": "invalid update",
                "body": "should not be updated"
            },
            allow_redirects=True,
        )
        assert response.status_code == 401
Ejemplo n.º 30
0
def test_add_post_auth(test_client):
    """
    GIVEN: Flask app with test conf
    WHEN: '/' on login/logout user
    THEN: "Add post" visible on login, hide on logout
    #TODO: add_post only confirmed USER access
    """
    rv = login(test_client, app.config['USERNAME'], app.config['PASSWORD'])
    assert b"Add post" in rv.data

    rv = logout(test_client)
    assert b"Add post" not in rv.data