def test_should_redirect_update_when_logged_in_as_wrong_user(
        client, test_users):
    user = test_users['michael']
    other_user = test_users['archer']
    with client:
        # ログインする
        log_in_as(client, other_user.email)

        # csrf tokenをsessionに設定する
        token = 'token'
        with client.session_transaction() as sess:
            sess['csrf_token'] = token

        # patch method を投げる
        response = client.post(f'/users/{user.id}',
                               data={
                                   '_method': 'patch',
                                   'name': user.name,
                                   'email': user.email,
                                   'password': '',
                                   'password_confirmation': '',
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')

        # flashメッセージが無いことを確認
        flashed_message = get_flashed_messages()
        assert not flashed_message

        # homeにredirectされていることを確認
        ref = render_template('static_pages/home.html',
                              micropost=other_user.microposts.build())
        assert are_same_templates(ref, contents)
示例#2
0
def test_should_redirect_destroy_for_wrong_micropost(client,
                                                     test_users, test_microposts):
    user = test_users['michael']
    micropost = test_microposts['ants']
    with client:
        log_in_as(client, user.email)
        before_count = Micropost.count()
        response = client.post(
            f'/microposts/{micropost.id}',
            data={'_method': 'delete'},
            follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        # マイクロポスト数が変わらないことを確認
        after_count = Micropost.count()
        assert before_count == after_count
        # home画面にredirectされていることを確認
        micropost = user.microposts.build()
        feed_items = user.feed()
        page = 1
        per_page = 30
        total = len(feed_items)
        feed_items = feed_items[(page-1)*per_page:page*per_page]
        pagination = Pagination(
            page=page, total=total, per_page=per_page,
            href=url_for('static_pages.home')+'?page={0}',
            prev_label='← Previous', next_label='Next →',
            css_framework='bootstrap3')
        ref = render_template('static_pages/home.html',
                              micropost=micropost, feed_items=feed_items,
                              pagination=pagination)
        #print(f'\nref:\n{ref}\n\ncontents\n{contents}\n')
        assert are_same_templates(ref, contents)
def test_should_redirect_destroy_when_logged_in_as_a_non_admin(
        client, test_users):
    user = test_users['michael']
    other_user = test_users['archer']
    with client:
        # ログインする
        log_in_as(client, other_user.email)

        # csrf tokenをsessionに設定する
        token = 'token'
        with client.session_transaction() as sess:
            sess['csrf_token'] = token

        # ユーザー数を保存する
        before_count = User.count()

        # delete method を投げる
        response = client.post(f'/users/{user.id}',
                               data={
                                   '_method': 'delete',
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')

        # ユーザー数が変わらないことを確認する
        after_count = User.count()
        assert before_count == after_count

        # homeにredirectされていることを確認
        ref = render_template('static_pages/home.html',
                              micropost=other_user.microposts.build())
        assert are_same_templates(ref, contents)
def test_index_as_admin_including_pagination_and_delete_links(client, test_users):
    admin = test_users['michael']
    non_admin = test_users['archer']
    with client:
        # adminとしてログインする
        log_in_as(client, admin.email)

        # ユーザー一覧ページにアクセス
        response = client.get('/users', follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')

        # ユーザー一覧ページが表示されたことを確認
        page = 1
        per_page = 30
        users = User.all()
        total = len(users)
        users = users[(page-1)*per_page:page*per_page]
        # Pagination内でview_argsにアクセスしているがtest_clientコンテキストでは
        # 存在しないのでエラーにならないようダミーを設定する
        request.view_args = {}
        pagination = Pagination(page=page, total=total, per_page=per_page,
                                prev_label='← Previous',
                                next_label='Next →',
                                css_framework='bootstrap3')
        ref = render_template('users/index.html', users=users, pagination=pagination)
        #print(f'\nref:\n{ref}\n\ncontents\n{contents}\n')
        assert are_same_templates(ref, contents)

        # paginationクラスの要素があることを確認
        assert re.search(r'class="pagination"', contents)

        # 最初のページに含まれるユーザーが表示されていることを確認
        for user in User.paginate(page=page):
            assert re.search(f'<a href="/users/{user.id}">{user.name}', contents)

        # csrf tokenをsessionに設定する
        token = 'token'
        with client.session_transaction() as sess:
            sess['csrf_token'] = token

        # ユーザー数を保存する
        before_count = User.count()

        # delete method を投げる
        response = client.post(
            f'/users/{non_admin.id}',
            data={'_method': 'delete',
                  'authenticity_token': token},
            follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')

        # ユーザーが一人減ったことを確認する
        after_count = User.count()
        assert before_count-1 == after_count
示例#5
0
def test_following_page(client, test_users, test_relationships):
    user = test_users['michael']
    with client:
        log_in_as(client, user.email)
        response = client.get(f'/users/{user.id}/following')
        contents = response.data.decode(encoding='utf-8')
        following = user.following()
        assert following
        assert re.search(f'{len(following)}', contents)
        for u in following:
            assert re.search(f'<a href="/users/{u.id}"', contents)
def test_index_as_non_admin(client, test_users):
    non_admin = test_users['archer']
    with client:
        # 一般ユーザーとしてログインする
        log_in_as(client, non_admin.email)

        # ユーザー一覧ページにアクセス
        response = client.get('/users', follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')

        # deleteリンクが表示されていないことを確認
        assert not re.search(r'<a .*>delete</a>', contents)
示例#7
0
def test_login_without_remembering(client, test_users):
    test_user = test_users['michael']
    with client:
        # cookieを保存してログイン
        response = log_in_as(client, test_user.email, remember_me='1')
        contents = response.data.decode(encoding='utf-8')

        # ログアウト
        response = log_out(client, contents)

        # cookieを削除してログイン
        log_in_as(client, test_user.email, remember_me='0')
        assert not request.cookies.get('remember_token')
示例#8
0
def test_successful_edit(client, test_users):
    user = test_users['michael']
    with client:
        # ログインする
        log_in_as(client, user.email)

        # 編集ページアクセス
        response = client.get(f'/users/{user.id}/edit')
        contents = response.data.decode(encoding='utf-8')
        m = AUTHENTICITY_TOKEN_PATTERN.search(contents)
        assert len(m.groups()) == 1
        token = m.groups()[0]
        # 編集ページが表示されたことを確認
        ref = render_template('users/edit.html', user=user, csrf_token=token)
        assert are_same_templates(ref, contents)

        # 有効な編集を行う。
        name = 'Foo Bar'
        email = '*****@*****.**'
        response = client.post(f'/users/{user.id}',
                               data={
                                   '_method': 'patch',
                                   'name': name,
                                   'email': email,
                                   'password': '',
                                   'password_confirmation': '',
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        # m = AUTHENTICITY_TOKEN_PATTERN.search(contents)
        # assert len(m.groups()) == 1
        # token = m.groups()[0]

        # flashメッセージが存在することを確認
        flashed_message = get_flashed_messages()
        assert flashed_message

        user.reload()
        assert name == user.name
        assert email == user.email

        # redirectされていることを確認
        ref = render_template('users/show.html', user=user)
        contents = response.data.decode(encoding='utf-8')
        assert are_same_templates(ref, contents)
示例#9
0
def test_should_follow_a_user_the_standard_way(client, test_users):
    user = test_users['michael']
    other = test_users['archer']
    with client:
        log_in_as(client, user.email)
        before_count = len(user.following())
        # csrf tokenをsessionに設定する
        token = 'token'
        with client.session_transaction() as sess:
            sess['csrf_token'] = token
        response = client.post('/relationships',
                               data={
                                   'followed_id': other.id,
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        after_count = len(user.following())
        assert before_count + 1 == after_count
def test_should_redirect_edit_when_logged_in_as_wrong_user(client, test_users):
    user = test_users['michael']
    other_user = test_users['archer']
    with client:
        # ログインする
        log_in_as(client, other_user.email)
        # 編集ページにアクセス
        response = client.get(f'/users/{user.id}/edit', follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')

        # flashメッセージが無いことを確認
        flashed_message = get_flashed_messages()
        assert not flashed_message

        # homeにredirectされていることを確認
        ref = render_template('static_pages/home.html',
                              micropost=other_user.microposts.build())
        contents = response.data.decode(encoding='utf-8')
        assert are_same_templates(ref, contents)
示例#11
0
def test_should_unfollow_a_user_the_standard_way(client, test_users):
    user = test_users['michael']
    other = test_users['archer']
    user.follow(other)
    relationship = user.active_relationships_find_by(other.id)
    with client:
        log_in_as(client, user.email)
        before_count = len(user.following())
        # csrf tokenをsessionに設定する
        token = 'token'
        with client.session_transaction() as sess:
            sess['csrf_token'] = token
        response = client.post(f'/relationships/{relationship.id}',
                               data={
                                   '_method': 'delete',
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        after_count = len(user.following())
        assert before_count - 1 == after_count
示例#12
0
def test_login_with_valid_information_followed_by_logout(client, test_users):
    test_user = test_users['michael']
    with client:
        # 登録ユーザーでログインを行う
        response = log_in_as(client, test_user.email, 'password')

        # ログインしていることを確認
        assert is_logged_in()

        # redirectされていることを確認
        ref = render_template('users/show.html', user=test_user)
        contents = response.data.decode(encoding='utf-8')
        assert are_same_templates(ref, contents)

        # login へのリンクが無いことを確認
        m = re.search(r'<a.*href="/login"', response.data.decode('utf-8'))
        assert not m

        # logout へのリンクがあることを確認
        m = re.search(r'<a.*href="/logout"', response.data.decode('utf-8'))
        assert m

        # プロフィールへのリンクがあることを確認
        m = re.search(rf'<a.*(href="/users/{test_user.id}")',
                      response.data.decode('utf-8'))
        assert m

        # ログアウト
        response = log_out(client, contents)

        # ログアウトしたことを確認
        assert not is_logged_in()

        # Homeページへリダイレクトしたことを確認
        ref = render_template('static_pages/home.html')
        assert are_same_templates(ref, response.data.decode(encoding='utf-8'))

        # 2番目のウィンドウでログアウトをクリックするユーザーをシミュレートする
        contents = response.data.decode(encoding='utf-8')
        response = log_out(client, contents)

        # login へのリンクがあることを確認
        m = re.search(r'<a.*href="/login"', response.data.decode('utf-8'))
        assert m

        # logout へのリンクが無いことを確認
        m = re.search(r'<a.*href="/logout"', response.data.decode('utf-8'))
        assert not m

        # プロフィールへのリンクが無いことを確認
        m = re.search(rf'<a.*(href="/users/{test_user.id}")',
                      response.data.decode('utf-8'))
        assert not m
示例#13
0
def test_unsuccessful_edit(client, test_users):
    user = test_users['michael']
    with client:
        # ログインする
        log_in_as(client, user.email)

        # 編集ページにアクセス
        response = client.get(f'/users/{user.id}/edit')
        contents = response.data.decode(encoding='utf-8')
        m = AUTHENTICITY_TOKEN_PATTERN.search(contents)
        assert len(m.groups()) == 1
        token = m.groups()[0]
        # 編集ページが表示されたことを確認
        ref = render_template('users/edit.html', user=user, csrf_token=token)
        assert are_same_templates(ref, contents)
        # 無効な編集を行う
        response = client.post(f'/users/{user.id}',
                               data={
                                   '_method': 'patch',
                                   'name': '',
                                   'email': 'foo@invalid',
                                   'password': '******',
                                   'password_confirmation': 'bar',
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        m = AUTHENTICITY_TOKEN_PATTERN.search(contents)
        assert len(m.groups()) == 1
        token = m.groups()[0]
        # エラーメッセージを表示させるためuserをupdateし失敗させる
        user.update(name='',
                    email='foo@invalid',
                    password='******',
                    password_confirmation='bar')
        # 編集に失敗し編集ページに戻されたことを確認
        ref = render_template('users/edit.html', user=user, csrf_token=token)
        #print(f'\nref:\n{ref}\n\ncontents\n{contents}\n')
        assert are_same_templates(ref, contents)
示例#14
0
def test_login_with_valid_email_invalid_password(client, test_users):
    test_user = test_users['michael']
    with client:
        # 無効なデータでログインを行う
        response = log_in_as(client, test_user.email, password='')

        # ログインしてないことを確認
        assert not is_logged_in()

        # ログインページに戻されることを確認
        ref = render_template('sessions/new.html')
        assert are_same_templates(ref, response.data.decode(encoding='utf-8'))

        # flashメッセージが存在することを確認
        flashed_message = get_flashed_messages()
        assert flashed_message

        # Homeに移動しflashメッセージが消えていることを確認
        response = client.get('/')
        flashed_message = get_flashed_messages()
        assert not flashed_message
def test_micropost_interface(client, test_users, test_microposts):
    user = test_users['michael']
    with client:
        log_in_as(client, user.email)
        response = client.get('/')
        contents = response.data.decode(encoding='utf-8')
        assert re.search('pagination', contents)
        # 無効な送信
        before_count = Micropost.count()
        # csrf tokenをsessionに設定する
        token = 'token'
        with client.session_transaction() as sess:
            sess['csrf_token'] = token
        response = client.post('/microposts',
                               data={
                                   'content': '',
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        # マイクロポスト数が変わらないことを確認
        after_count = Micropost.count()
        assert before_count == after_count
        #print(f'\n{contents}\n')
        assert re.search('error_explanation', contents)
        assert re.search(r'<a href="/\?page=2">', contents)
        # 有効な送信
        before_count = Micropost.count()
        content = "This micropost really ties the room together"
        response = client.post('/microposts',
                               data={
                                   'content': content,
                                   'authenticity_token': token
                               },
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        # マイクロポスト数が1増えたことを確認
        after_count = Micropost.count()
        assert before_count + 1 == after_count
        # home画面にredirectされていることを確認
        micropost = user.microposts.build()
        feed_items = user.feed()
        page = 1
        per_page = 30
        total = len(feed_items)
        feed_items = feed_items[(page - 1) * per_page:page * per_page]
        pagination = Pagination(page=page,
                                total=total,
                                per_page=per_page,
                                href=url_for('static_pages.home') +
                                '?page={0}',
                                prev_label='&larr; Previous',
                                next_label='Next &rarr;',
                                css_framework='bootstrap3')
        ref = render_template('static_pages/home.html',
                              micropost=micropost,
                              feed_items=feed_items,
                              pagination=pagination)
        #print(f'\nref:\n{ref}\n\ncontents\n{contents}\n')
        assert are_same_templates(ref, contents)
        assert re.search(content, contents)
        # 投稿を削除する
        before_count = Micropost.count()
        assert re.search(r'<a.*>delete</a>', contents)
        first_micropost = user.microposts()[0]
        response = client.post(f'/microposts/{first_micropost.id}',
                               data={'_method': 'delete'},
                               follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        after_count = Micropost.count()
        assert before_count - 1 == after_count
        user2 = test_users['archer']
        response = client.get(f'/users/{user2.id}', follow_redirects=True)
        contents = response.data.decode(encoding='utf-8')
        print(f'\n{contents}\n')
        assert not re.search(r'<a.*>delete</a>', contents)
示例#16
0
def test_valid_signup_information_with_account_activation(app, client):
    valid_email = '*****@*****.**'
    with client:
        try:
            response = client.get('/signup')
            contents = response.data.decode(encoding='utf-8')
            m = AUTHENTICITY_TOKEN_PATTERN.search(contents)
            assert len(m.groups()) == 1
            token = m.groups()[0]

            before_count = User.count()
            user = User(name='Example User',
                        email=valid_email,
                        password='******',
                        password_confirmation='password')

            mail = Mail(app)
            with mail.record_messages() as outbox:
                response = client.post('/users',
                                       data={
                                           'name': user.name,
                                           'email': user.email,
                                           'password': user.password,
                                           'password_confirmation':
                                           user.password_confirmation,
                                           'authenticity_token': token
                                       },
                                       follow_redirects=True)
                assert len(outbox) == 1

            # activation状態をデータベースから読み出す
            user = User.find_by(email=user.email)[0]
            assert not user.activated

            # activation digestが作られていることを確認する
            assert user.activation_digest

            # テストからactivation tokenを使用するためにtoken, digestを再設定する
            user.activation_token = User.new_token()
            digest = User.digest(user.activation_token)
            user.update_attribute('activation_digest', digest)

            # 有効化していない状態でログインしてみる
            log_in_as(client, user.email)
            assert not is_logged_in()
            # 有効化トークンが不正な場合
            url = url_for('account_activations.edit',
                          id='invalid token',
                          email=user.email,
                          _external=True)
            client.get(url, follow_redirects=True)
            assert not is_logged_in()
            # トークンは正しいがメールアドレスが無効な場合
            url = url_for('account_activations.edit',
                          id=user.activation_token,
                          email='wrong',
                          _external=True)
            client.get(url, follow_redirects=True)
            assert not is_logged_in()
            # 有効化トークンが正しい場合
            url = url_for('account_activations.edit',
                          id=user.activation_token,
                          email=user.email,
                          _external=True)
            response = client.get(url, follow_redirects=True)
            contents = response.data.decode(encoding='utf-8')
            user.reload()
            assert user.activated
            ref = render_template('users/show.html', user=user)
            assert are_same_templates(ref, contents)
            assert is_logged_in()
        finally:
            # 登録したユーザーを削除
            users = User.find_by(email=valid_email)
            if users:
                users[0].destroy()
示例#17
0
def test_login_with_remembering(client, test_users):
    test_user = test_users['michael']
    with client:
        log_in_as(client, test_user.email, remember_me='1')
        assert request.cookies.get('remember_token')