morepath / more.jwtauth

JWT Authentication integration for Morepath
BSD 3-Clause "New" or "Revised" License
3 stars 3 forks source link

Tests doesn't work for Morepath 0.13 #2

Closed henri-hulski closed 8 years ago

henri-hulski commented 8 years ago

I have problems to get some tests to work.

lookup = App().registry.lookup

does not work and I don't know, how to get that right.

def test_encode_decode():
    morepath.scan(more.jwtauth)

    identity_policy = JWTIdentityPolicy(master_secret='secret')

    morepath.autocommit()
    claims_set = {
        'sub': 'user'
    }
    token = identity_policy.encode_jwt(claims_set)
    claims_set_decoded = identity_policy.decode_jwt(token)

    assert claims_set_decoded == claims_set

This does work. If I use morepath.commit() it also works. But I'm not sure which one is right if any.

@faassen Any ideas?

faassen commented 8 years ago

Try App.lookup, though where in the test do you use it?

faassen commented 8 years ago

You cannot use autocommit in tests, just commit.

henri-hulski commented 8 years ago

So it's okay to use morepath.commit() without arguments?

App.lookup doesn't work.

The test is:

def test_jwt_custom_settings():
    morepath.scan(more.jwtauth)

    class App(morepath.App):
        pass

    @App.setting_section(section="jwtauth")
    def get_jwtauth_settings():
        return {
            'public_key': 'MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBWcJwPEAnS/k4kFgUhxNF7J0SQQhZG+nNgy+'
                          '/mXwhQ5PZIUmId1a1TjkNXiKzv6DpttBqduHbz/V0EtH+QfWy0B4BhZ5MnTyDGjcz1DQqKd'
                          'exebhzobbhSIZjpYd5aU48o9rXp/OnAnrajddpGsJ0bNf4rtMLBqFYJN6LOslAB7xTBRg=',
            'algorithm': "ES256",
            'leeway': 20
        }

    morepath.commit(App)
    lookup = App().registry.lookup

    assert settings(lookup=lookup).jwtauth.algorithm == "ES256"
    assert settings(lookup=lookup).jwtauth.leeway == 20
    assert settings(
        lookup=lookup).jwtauth.public_key == 'MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBWcJwPEAnS/k4kFgUhxNF7J0SQQhZG+nNgy+' \
                                             '/mXwhQ5PZIUmId1a1TjkNXiKzv6DpttBqduHbz/V0EtH+QfWy0B4BhZ5MnTyDGjcz1DQqKd' \
                                             'exebhzobbhSIZjpYd5aU48o9rXp/OnAnrajddpGsJ0bNf4rtMLBqFYJN6LOslAB7xTBRg='
faassen commented 8 years ago

commit should get the App class as an argument. Sounds like the API docs need some improvements...

Try app.lookup, class instead of instance.

henri-hulski commented 8 years ago

I think for the second test I actually don't need Morepath. I just can do

def test_encode_decode():
    identity_policy = JWTIdentityPolicy(master_secret='secret')
    claims_set = {
        'sub': 'user'
    }
    token = identity_policy.encode_jwt(claims_set)
    claims_set_decoded = identity_policy.decode_jwt(token)

    assert claims_set_decoded == claims_set
henri-hulski commented 8 years ago

Ah and the first one is lookup = App().lookup.

henri-hulski commented 8 years ago

So it seems to be fine now. Thanks.

faassen commented 8 years ago

No problem!

faassen commented 8 years ago

I put a few clarifications in the CHANGES for 0.13 so that at least people in the future can read it.