<s> import json <EOL> from pytest import fixture , raises <EOL> from flask import Flask <EOL> from six import b <EOL> from flask_slack import Slack <EOL> class App ( object ) : <EOL> def __init__ ( self ) : <EOL> self . app = Flask ( __name__ ) <EOL> self . app . debug = True <EOL> self . slack = Slack ( self . app ) <EOL> self . app . add_url_rule ( '<STR_LIT:/>' , view_func = self . slack . dispatch ) <EOL> self . client = self . app . test_client ( ) <EOL> def _jsonify_response ( res ) : <EOL> return json . loads ( res . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> @ fixture ( scope = '<STR_LIT>' ) <EOL> def app ( ) : <EOL> return App ( ) <EOL> def test_request_without_registering_commands ( app ) : <EOL> res = app . client . get ( '<STR_LIT:/>' ) <EOL> assert res . status_code == <NUM_LIT:200> <EOL> assert _jsonify_response ( res ) [ '<STR_LIT:text>' ] == ( <EOL> '<STR_LIT>' ) <EOL> def test_registering_commands ( app ) : <EOL> command = '<STR_LIT>' <EOL> token = '<STR_LIT>' <EOL> team_id = '<STR_LIT>' <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> text = '<STR_LIT>' <EOL> get_url = '<STR_LIT>' . format ( <EOL> token , team_id , command , text ) <EOL> @ app . slack . command ( command , token , team_id , methods ) <EOL> def _sing_a_song ( ** kwargs ) : <EOL> lyrics = '<STR_LIT>' <EOL> return app . slack . response ( lyrics ) <EOL> expected_commands = { <EOL> ( team_id , command ) : ( _sing_a_song , token , methods , { } ) , <EOL> } <EOL> assert app . slack . _commands == expected_commands <EOL> get_res = app . client . get ( get_url ) <EOL> assert get_res . status_code == <NUM_LIT:200> <EOL> assert _jsonify_response ( get_res ) [ '<STR_LIT:text>' ] == ( <EOL> '<STR_LIT>' ) <EOL> post_data = { <EOL> '<STR_LIT>' : command , <EOL> '<STR_LIT>' : token , <EOL> '<STR_LIT>' : team_id , <EOL> '<STR_LIT:text>' : text <EOL> } <EOL> post_res = app . client . post ( '<STR_LIT:/>' , data = post_data ) <EOL> assert post_res . status_code == <NUM_LIT:200> <EOL> assert _jsonify_response ( post_res ) [ '<STR_LIT:text>' ] == ( <EOL> '<STR_LIT>' ) <EOL> def test_commands_without_registering_team_id ( app ) : <EOL> command = '<STR_LIT>' <EOL> token = '<STR_LIT>' <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> with raises ( RuntimeError ) : <EOL> @ app . slack . command ( command , token , methods = methods ) <EOL> def _sing_a_song ( ** kwargs ) : <EOL> lyrics = '<STR_LIT>' <EOL> return app . slack . response ( lyrics ) <EOL> def test_invalid_token ( app ) : <EOL> command = '<STR_LIT>' <EOL> token = '<STR_LIT>' <EOL> team_id = '<STR_LIT>' <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> text = '<STR_LIT>' <EOL> invalid_token = '<STR_LIT>' <EOL> @ app . slack . command ( command , token , team_id , methods ) <EOL> def _sing_a_song ( ** kwargs ) : <EOL> lyrics = '<STR_LIT>' <EOL> return app . slack . response ( lyrics ) <EOL> post_data = { <EOL> '<STR_LIT>' : command , <EOL> '<STR_LIT>' : invalid_token , <EOL> '<STR_LIT>' : team_id , <EOL> '<STR_LIT:text>' : text <EOL> } <EOL> post_res = app . client . post ( '<STR_LIT:/>' , data = post_data ) <EOL> assert post_res . status_code == <NUM_LIT:200> <EOL> assert _jsonify_response ( post_res ) [ '<STR_LIT:text>' ] == ( <EOL> '<STR_LIT>' . format ( invalid_token ) ) <EOL> def test_response_type ( app ) : <EOL> command = '<STR_LIT>' <EOL> token = '<STR_LIT>' <EOL> team_id = '<STR_LIT>' <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> text = '<STR_LIT>' <EOL> @ app . slack . command ( command , token , team_id , methods ) <EOL> def _sing_a_song ( ** kwargs ) : <EOL> lyrics = '<STR_LIT>' <EOL> return app . slack . response ( lyrics , response_type = '<STR_LIT>' ) <EOL> post_data = { <EOL> '<STR_LIT>' : command , <EOL> '<STR_LIT>' : token , <EOL> '<STR_LIT>' : team_id , <EOL> '<STR_LIT:text>' : text <EOL> } <EOL> post_res = app . client . post ( '<STR_LIT:/>' , data = post_data ) <EOL> assert post_res . status_code == <NUM_LIT:200> <EOL> assert _jsonify_response ( post_res ) [ '<STR_LIT>' ] == '<STR_LIT>' </s>
