def test_save_authorized(client, monkeypatch):
    storage = MemoryStorage({"access_token": "fake-token"})
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        with client.session_transaction() as session:
            session["user_id"] = "dummy"
            session["spotify_wordcloud_text"] = "test1 test2 test3"
            session["spotify_wordcloud_hash"] = "test"
        res0 = client.get("/history", base_url="https://example.com")
        res1 = client.post("/save", base_url="https://example.com")
        res2 = client.get("/history", base_url="https://example.com")

    assert res0.status_code == 200
    text = res0.get_data(as_text=True)
    assert "過去に作成した画像一覧" in text
    assert "まだ画像が保存されていません。" in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        in text
    )

    assert res1.status_code == 200

    assert res2.status_code == 200
    text = res2.get_data(as_text=True)
    assert "過去に作成した画像一覧" in text
    assert "まだ画像が保存されていません。" not in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        in text
    )
def test_save_unauthorized(client, monkeypatch):
    storage = MemoryStorage()
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        res = client.post("/save", base_url="https://example.com")

    assert res.status_code == 401
Beispiel #3
0
def client():
    with app.test_client() as client:
        with app.app_context():
            db.create_all()
        yield client

    db.session.query(Pictures).filter(Pictures.user_id == "dummy").delete()
    db.session.commit()
Beispiel #4
0
def test_logout(monkeypatch):
    storage = MemoryStorage({"access_token": "fake-token"})
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        res = client.get("/logout", base_url="https://example.com")

    assert res.status_code == 302
    assert res.headers["Location"] == "https://example.com/"
Beispiel #5
0
def test_login(monkeypatch):
    storage = MemoryStorage()
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        res = client.get("/login", base_url="https://example.com")

    assert res.status_code == 302
    assert res.headers["Location"] == "https://example.com/login/spotify"
def client():
    app.config["WTF_CSRF_ENABLED"] = False

    with app.test_client() as client:
        with app.app_context():
            db.create_all()
        yield client

    db.session.query(Pictures).filter(Pictures.user_id == "dummy").delete()
    db.session.commit()
Beispiel #7
0
def test_error(monkeypatch):
    storage = MemoryStorage()
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        res = client.get("/sonzaishinai", base_url="https://example.com")

    assert res.status_code == 404
    text = res.get_data(as_text=True)
    assert "このページはすでに削除されているか、URLが間違っている可能性があります。" in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        in text)
def test_regenrate_authorized(monkeypatch):
    storage = MemoryStorage({"access_token": "fake-token"})
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        with client.session_transaction() as session:
            session["user_id"] = "dummy"
            session["spotify_wordcloud_text"] = "test1 test2 test3"
            session["spotify_wordcloud_hash"] = "test"
        res = client.get("/regenerate", base_url="https://example.com")

    assert res.status_code == 200
    assert res.mimetype == "image/png"
def test_share_twitter_authorized(client, monkeypatch):
    storage = MemoryStorage({"access_token": "fake-token"})
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        with client.session_transaction() as session:
            session["user_id"] = "dummy"
            session["spotify_wordcloud_text"] = "test1 test2 test3"
            session["spotify_wordcloud_hash"] = "test"
        res = client.post("/shareTwitter", base_url="https://example.com")

    assert res.status_code == 302
    assert res.headers["Location"].startswith("https://twitter.com/")
Beispiel #10
0
def client_with_dummy_file():
    app.config["WTF_CSRF_ENABLED"] = False

    with app.test_client() as client:
        with app.app_context():
            db.create_all()
            record = Pictures(user_id="dummy", file_name="test.png")
            db.session.add(record)
            db.session.commit()
        yield client

    db.session.query(Pictures).filter(Pictures.user_id == "dummy").delete()
    db.session.commit()
Beispiel #11
0
def test_ogp(monkeypatch):
    storage = MemoryStorage()
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        res = client.get("/share/bfbe015a6d6318dd2b50b3c9918f4cdf",
                         base_url="https://example.com")

    assert res.status_code == 200
    text = res.get_data(as_text=True)
    assert '/wordclouds/bfbe015a6d6318dd2b50b3c9918f4cdf.png"' in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        not in text)
Beispiel #12
0
def test_index_unauthorized(monkeypatch):
    storage = MemoryStorage()
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        res = client.get("/", base_url="https://example.com")

    assert res.status_code == 200
    text = res.get_data(as_text=True)
    assert "よく聴いているアーティストの名前からワードクラウドを作ることができます。" in text
    assert "過去に作成した画像" not in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        in text
    )
Beispiel #13
0
def test_history_authorized_with_dummy_file(client_with_dummy_file,
                                            monkeypatch):
    storage = MemoryStorage({"access_token": "fake-token"})
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        with client.session_transaction() as session:
            session["user_id"] = "dummy"
        res = client.get("/history", base_url="https://example.com")

    assert res.status_code == 200
    text = res.get_data(as_text=True)
    assert "過去に作成した画像一覧" in text
    assert "まだ画像が保存されていません。" not in text
    assert "test.png" in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        in text)
Beispiel #14
0
def test_delete(client_with_dummy_file, monkeypatch):
    storage = MemoryStorage({"access_token": "fake-token"})
    monkeypatch.setattr(spotify_bp, "storage", storage)

    with app.test_client() as client:
        with client.session_transaction() as session:
            session["user_id"] = "dummy"
        res1 = client.post("/history/test",
                           base_url="https://example.com",
                           data={"_method": "DELETE"})
        res2 = client.get("/history", base_url="https://example.com")

    assert res1.status_code == 302
    assert res1.headers["Location"] == "https://example.com/history"

    assert res2.status_code == 200
    text = res2.get_data(as_text=True)
    assert "過去に作成した画像一覧" in text
    assert "まだ画像が保存されていません。" in text
    assert "test.png" not in text
    assert (
        'content="https://res.cloudinary.com/hellorusk/image/upload/v1611156028/top.png"'
        in text)