Ejemplo n.º 1
0
    def test_create_flow(self):
        with self.app.test_request_context():
            flow = self.oauth2._make_flow()
            state = json.loads(flow.params['state'])
            self.assertIn('google_oauth2_csrf_token', flask.session)
            self.assertEqual(
                flask.session['google_oauth2_csrf_token'], state['csrf_token'])
            self.assertEqual(flow.client_id, self.oauth2.client_id)
            self.assertEqual(flow.client_secret, self.oauth2.client_secret)
            self.assertIn('http', flow.redirect_uri)
            self.assertIn('oauth2callback', flow.redirect_uri)

            flow = self.oauth2._make_flow(return_url='/return_url')
            state = json.loads(flow.params['state'])
            self.assertEqual(state['return_url'], '/return_url')

            flow = self.oauth2._make_flow(extra_arg='test')
            self.assertEqual(flow.params['extra_arg'], 'test')

        # Test extra args specified in the constructor.
        app = flask.Flask(__name__)
        app.config['SECRET_KEY'] = 'notasecert'
        oauth2 = flask_util.UserOAuth2(
            app, client_id='client_id', client_secret='secret',
            extra_arg='test')

        with app.test_request_context():
            flow = oauth2._make_flow()
            self.assertEqual(flow.params['extra_arg'], 'test')
Ejemplo n.º 2
0
 def test_explicit_storage(self):
     storage_mock = mock.Mock()
     oauth2 = flask_util.UserOAuth2(flask.Flask(__name__),
                                    storage=storage_mock,
                                    client_id='id',
                                    client_secret='secret')
     self.assertEqual(oauth2.storage, storage_mock)
Ejemplo n.º 3
0
 def setUp(self):
     self.app = flask.Flask(__name__)
     self.app.testing = True
     self.app.config['SECRET_KEY'] = 'notasecert'
     self.app.logger.setLevel(logging.CRITICAL)
     self.oauth2 = flask_util.UserOAuth2(self.app,
                                         client_id='client_idz',
                                         client_secret='client_secretz')
Ejemplo n.º 4
0
    def test_bad_client_secrets(self):
        return_val = ('other', {'client_id': 'id', 'client_secret': 'secret'})

        with mock.patch('oauth2client.clientsecrets.loadfile',
                        return_value=return_val):
            with self.assertRaises(ValueError):
                flask_util.UserOAuth2(flask.Flask(__name__),
                                      client_secrets_file='file.json')
Ejemplo n.º 5
0
    def test_explicit_configuration(self):
        oauth2 = flask_util.UserOAuth2(
            flask.Flask(__name__), client_id='id', client_secret='secret')

        self.assertEqual(oauth2.client_id, 'id')
        self.assertEqual(oauth2.client_secret, 'secret')

        return_val = (
            clientsecrets.TYPE_WEB,
            {'client_id': 'id', 'client_secret': 'secret'})

        with mock.patch('oauth2client.clientsecrets.loadfile',
                        return_value=return_val):

            oauth2 = flask_util.UserOAuth2(
                flask.Flask(__name__), client_secrets_file='file.json')

            self.assertEqual(oauth2.client_id, 'id')
            self.assertEqual(oauth2.client_secret, 'secret')
Ejemplo n.º 6
0
    def test_app_configuration(self):
        app = flask.Flask(__name__)
        app.config['GOOGLE_OAUTH2_CLIENT_ID'] = 'id'
        app.config['GOOGLE_OAUTH2_CLIENT_SECRET'] = 'secret'

        oauth2 = flask_util.UserOAuth2(app)

        self.assertEqual(oauth2.client_id, 'id')
        self.assertEqual(oauth2.client_secret, 'secret')

        return_val = (
            clientsecrets.TYPE_WEB,
            {'client_id': 'id2', 'client_secret': 'secret2'})

        with mock.patch('oauth2client.clientsecrets.loadfile',
                        return_value=return_val):

            app = flask.Flask(__name__)
            app.config['GOOGLE_OAUTH2_CLIENT_SECRETS_FILE'] = 'file.json'
            oauth2 = flask_util.UserOAuth2(app)

            self.assertEqual(oauth2.client_id, 'id2')
            self.assertEqual(oauth2.client_secret, 'secret2')
Ejemplo n.º 7
0
    def _create_incremental_auth_app(self):
        self.app = flask.Flask(__name__)
        self.app.testing = True
        self.app.config['SECRET_KEY'] = 'notasecert'
        self.oauth2 = flask_util.UserOAuth2(self.app,
                                            client_id='client_idz',
                                            client_secret='client_secretz',
                                            include_granted_scopes=True)

        @self.app.route('/one')
        @self.oauth2.required(scopes=['one'])
        def one():
            return 'Hello'

        @self.app.route('/two')
        @self.oauth2.required(scopes=['two', 'three'])
        def two():
            return 'Hello'
Ejemplo n.º 8
0
        os.environ['GOOGLE_OAUTH2_' +
                   'client_id'.upper()] = secrets['web']['client_id']
        os.environ['GOOGLE_OAUTH2_' +
                   'client_secret'.upper()] = secrets['web']['client_secret']
except FileNotFoundError:
    pass

app = Flask(__name__)

app.config['SECRET_KEY'] = os.urandom(24)
app.config['GOOGLE_OAUTH2_CLIENT_ID'] = os.environ.get(
    'GOOGLE_OAUTH2_CLIENT_ID', None)
app.config['GOOGLE_OAUTH2_CLIENT_SECRET'] = os.environ.get(
    'GOOGLE_OAUTH2_CLIENT_SECRET', None)

g_oauth = flask_util.UserOAuth2(app)


def get_savedata(post_data):
    data = {}
    data['mask'] = {}
    if post_data['safebooru']:
        img = requests.get(BASE_URL + '&id=' + post_data['name'])
        elem = ET.fromstring(img.text)
        src_url = elem[0].get('file_url')
        data['url'] = src_url
    masks = post_data['imgs']
    colors = post_data['colors']
    for (key, img) in masks.items():
        mask_img = Image.open(BytesIO(base64.b64decode(
            img.split(',')[-1]))).convert('RGB')
Ejemplo n.º 9
0
 def test_delayed_configuration(self):
     app = flask.Flask(__name__)
     oauth2 = flask_util.UserOAuth2()
     oauth2.init_app(app, client_id='id', client_secret='secret')
     self.assertEqual(oauth2.app, app)
Ejemplo n.º 10
0
 def test_no_configuration(self):
     with self.assertRaises(ValueError):
         flask_util.UserOAuth2(flask.Flask(__name__))
Ejemplo n.º 11
0
 def test_explicit_scopes(self):
     oauth2 = flask_util.UserOAuth2(
         flask.Flask(__name__), scopes=['1', '2'], client_id='id',
         client_secret='secret')
     self.assertEqual(oauth2.scopes, ['1', '2'])