예제 #1
0
    def test_get_attr_name(self, MockEntityModel, mock_decoded_token):

        self.config.entity_models = [MockEntityModel]

        entity = Entity(self.config)
        entity.get_entity_from_token_or_tablename(mock_decoded_token)

        result = entity.get_attr_name()

        assert result == "id"
예제 #2
0
    def test_get_attr_name(self, MockEntityModel, mock_decoded_token):

        self.ext.entity_models = [MockEntityModel]

        entity = Entity(self.ext)
        entity.get_entity_from_token(mock_decoded_token)

        result = entity.get_attr_name()

        assert result == "id"
예제 #3
0
    def test_get_entity_from_token(self, MockEntityModelThree,
                                   mock_decoded_token_three):

        self.ext.entity_models = [MockEntityModelThree]

        entity = Entity(self.ext)

        assert entity.get_entity_from_token(mock_decoded_token_three) == [
            (1, 'joe')
        ]
예제 #4
0
    def test_get_entity_from_token_multiple(self, MockEntityModel,
                                            MockEntityModelTwo,
                                            mock_decoded_token_two):

        self.ext.entity_models = [MockEntityModel, MockEntityModelTwo]

        entity = Entity(self.ext)

        assert entity.get_entity_from_token(mock_decoded_token_two) == [
            (1, 'joe')
        ]
예제 #5
0
    def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token):
        app = Flask(__name__)

        @app.route("/test", methods=["GET"])
        def test_one():
            return "/test"

        # Manually set the primary key
        entity = TestMockEntity(id=1, user_name="joe")

        ctx = app.test_request_context("/test")
        ctx.push()

        assert entity.user_name == "joe"
        assert entity.id == 1

        self.ext.entity_models = [TestMockEntity]
        entity = Entity(self.ext)
        routing = Routing(app, self.ext, entity)

        with ctx:
            # token from args
            monkeypatch.setattr("flask.request.args", MockArgs(mock_token))
            routing.before_middleware()
            assert ctx.g.test_entities == [(1, 'joe')]

        with ctx:
            # token from headers
            monkeypatch.setattr("flask.request.args", MockArgs())
            monkeypatch.setattr("flask.request.headers",
                                MockArgs(mock_token, True))
            routing.before_middleware()
            assert ctx.g.test_entities == [(1, 'joe')]
예제 #6
0
    def test_get_entity_from_token_or_tablename(self, MockEntityModelThree,
                                                mock_decoded_token_three,
                                                MockAOuthModel):

        self.config.entity_models = [MockEntityModelThree]
        entity = Entity(self.config)
        assert entity.get_entity_from_token_or_tablename(
            mock_decoded_token_three) == [(1, 'joe')]

        self.config.entity_models = [MockAOuthModel]
        self.config.google_oauth = self.oauth_options
        entity = Entity(self.config)
        entity.oauth_entity_key = "email"
        assert entity.get_entity_from_token_or_tablename(
            tablename="oauth_tablename") == [(1, 'joe')]
    def test_before_middleware(self, monkeypatch, TestMockEntity,
                               MockAOuthModel, mock_token, http_requests):
        app = Flask(__name__)

        @app.route("/test", methods=["GET"])
        def fc_one():
            return "/test"

        # Manually set the primary key
        entity = TestMockEntity(id=1, user_name="joe")
        oauth_entity = MockAOuthModel(id=1, email="*****@*****.**")
        after_oauth = TestMockEntity(id=1, user_name="terry")

        ctx = app.test_request_context("/test")
        ctx.push()

        assert entity.user_name == "joe"
        assert entity.id == 1
        assert oauth_entity.id == 1
        assert oauth_entity.email == "*****@*****.**"
        assert after_oauth.user_name == "terry"
        assert after_oauth.id == 1

        config = Config()
        config.init_config(self.app_config, google_oauth=self.oauth_options)

        config.entity_models = [TestMockEntity, MockAOuthModel]
        entity = Entity(config)
        google = Google(http_requests(oauth_urls))
        google.init(**config.google_oauth)
        routing = Routing()
        routing.init(app, config, entity, google)

        with ctx:
            # token from args
            monkeypatch.setattr("flask.request.args", MockArgs(mock_token))
            entity.clean_up()
            assert routing.entity.entity_key == None
            assert routing.entity.tablename == None
            routing.before_middleware()
            assert ctx.g.test_entities == [(1, 'joe')]

        with ctx:
            # token from OAuth headers - X-Auth-Token
            monkeypatch.setattr("flask.request.args", {})
            monkeypatch.setattr("flask.request.headers",
                                MockArgs(mock_token, True))
            entity.clean_up()
            assert routing.entity.oauth_entity_key == None
            assert routing.entity.tablename == None
        #     routing.before_middleware()
        #     assert ctx.g.oauth_tablename == [(1, '*****@*****.**')]

        # with ctx:
        #     # token from oauth headers
        #     monkeypatch.setattr("flask.request.args", {})
        #     monkeypatch.setattr("flask.request.headers", MockArgs("<access_token>", "X-Auth-Token"))
        #     entity.clean_up()
        #     assert routing.entity.entity_key == None
        #     assert routing.entity.oauth_entity_key == None
        #     assert routing.entity.tablename == None
        #     routing.before_middleware()
        #     assert ctx.g.oauth_tablename == [(1, "*****@*****.**")]

        # Fixes bug - "entity key state gets stale between requests #171"
        # https://github.com/joegasewicz/flask-jwt-router/issues/171
        with ctx:
            monkeypatch.setattr("flask.request.headers",
                                MockArgs("<after_token>", "Authorization"))
            entity.clean_up()
            assert routing.entity.entity_key == None
            assert routing.entity.oauth_entity_key == None
            assert routing.entity.tablename == None