def test_successful_login_returns_JWT(client):
    response = register_with(client, 'johndoe', 'abc123', '*****@*****.**')
    assert 201 == response.status_code

    response = login_with(client, 'johndoe', 'abc123')
    assert b'access_token' in response.data
    assert 200 == response.status_code
def client_with_regd_user(app):
    the_client = app.test_client()
    response = register_with(the_client, 'johndoe',
                             'abc123', '*****@*****.**')
    assert 201 == response.status_code

    yield the_client
    delete_test_db()
def test_login_with_wrong_password(client):
    response = register_with(client, 'johndoe', 'abc123', '*****@*****.**')
    assert 201 == response.status_code

    FAIL_MESSAGE = b'''{
    "error": "Login Failed.",
    "description": "Wrong password."\n}'''

    response = login_with(client, 'johndoe', 'wrongpwd123')
    assert 400 == response.status_code
    assert FAIL_MESSAGE in response.data
def client_with_logged_in_user(app):
    the_client = app.test_client()
    response = register_with(the_client, 'johndoe',
                             'abc123', '*****@*****.**')
    assert 201 == response.status_code

    response = login_with(the_client, 'johndoe', 'abc123')
    assert b'access_token' in response.data
    assert 200 == response.status_code

    str_resp = response.data.decode('utf-8')
    resp_dict = JSONDecoder().decode(str_resp)
    access_token = resp_dict['access_token']

    yield the_client, access_token
    delete_test_db()
def client_that_user_created_two_labels(app):
    the_client = app.test_client()

    response = register_with(the_client, 'johndoe',
                             'abc123', '*****@*****.**')
    assert 201 == response.status_code


    FIRST_LABEL_JSON_STR = '''{
    "text": "my-first-label"\n}'''
    

    r = the_client.post('/api/u/johndoe/labels', json=FIRST_LABEL_JSON_STR)
    assert 201 == r.status_code

    eb = b'''{
    "created_label": {
        "text": "my-first-label"
    }\n}'''
    assert eb in r.data


    SECOND_LABEL_JSON_STR = '''{
    "text": "mySecondLabel"\n}'''

    r = the_client.post('/api/u/johndoe/labels', json=SECOND_LABEL_JSON_STR)
    assert 201 == r.status_code
    
    eb = b'''{
    "created_label": {
        "text": "mySecondLabel"
    }\n}'''
    assert eb in r.data

    yield the_client
    delete_test_db()
def client_that_user_created_two_notes(app):
    the_client = app.test_client()
    response = register_with(the_client, 'johndoe',
                             'abc123', '*****@*****.**')
    assert 201 == response.status_code

    FIRST_NOTE_JSON_STR = '''{
    "title": "my first note",
    "text": "first note text",
    "pinned": "false",
    "archived": "true",
    "color_name": "purple",
    "images": [
        {
            "url": "www.firstNoteImage.com"
        }
    ],
    "labels": []\n}'''
    r = the_client.post('/api/u/johndoe/notes', json=FIRST_NOTE_JSON_STR)
    assert 201 == r.status_code
    eb = b'''{
    "created_note": {
        "id": "1",
        "title": "my first note",
        "text": "first note text",
        "pinned": "false",
        "archived": "true",
        "user_id": "1",
        "color_name": "purple",
        "images": [
            {
                "url": "www.firstNoteImage.com"
            }
        ],
        "labels": []
    }\n}'''
    assert eb in r.data


    SECOND_NOTE_JSON_STR = '''{
    "title": "my SECOND note",
    "text": "second note text",
    "pinned": "true",
    "archived": "false",
    "color_name": "green",
    "images": [
        {
            "url": "www.blabla.com"
        },
        {
            "url": "www.google.com"
        }
    ],
    "labels": [
        {
            "text": "my-label"
        },
        {
            "text": "anotherLabel"
        }
    ]\n}'''
    r = the_client.post('/api/u/johndoe/notes', json=SECOND_NOTE_JSON_STR)
    assert 201 == r.status_code

    eb = b'''{
    "created_note": {
        "id": "2",
        "title": "my SECOND note",
        "text": "second note text",
        "pinned": "true",
        "archived": "false",
        "user_id": "1",
        "color_name": "green",
        "images": [
            {
                "url": "www.blabla.com"
            },
            {
                "url": "www.google.com"
            }
        ],
        "labels": [
            {
                "text": "anotherLabel"
            },
            {
                "text": "my-label"
            }
        ]
    }\n}'''
    
    assert eb in r.data


    yield the_client
    delete_test_db()