Пример #1
0
 def test_administrator_role(self):
     """Test CRUD permissions of administrator"""
     role = Role.query.filter_by(name='Administrator').first()
     usr = User(email='*****@*****.**', password='******')
     usr.role = role
     self.assertTrue(usr.can(Permission.CRUD_OWNED))
     self.assertTrue(usr.can(Permission.CRUD_USERS))
     self.assertTrue(usr.can(Permission.ADMIN))
Пример #2
0
    def process_callback():

        # Get information from authorisation request response.
        authorization_response_url = request.url
        authorization_response_code = request.args.get('code')

        # Construct token request.
        client_secret = os.environ.get('GITHUB_CLIENT_SECRET')
        token_request_base_url = "https://github.com/login/oauth/access_token"
        token_request_url, token_request_headers, token_request_body = client.prepare_token_request(
            token_request_base_url,
            authorization_response=authorization_response_url,
            code=authorization_response_code,
            client_secret=client_secret)

        # Get an access token.
        token_request_response = requests.post(token_request_url,
                                               headers=token_request_headers,
                                               data=token_request_body)
        client.parse_request_body_response(token_request_response.text)

        # Get user details.
        user_request_base_uri = "https://api.github.com/user"
        user_request_uri, user_request_headers, user_request_body = client.add_token(
            user_request_base_uri)
        user_info = requests.get(user_request_uri,
                                 headers=user_request_headers,
                                 data=user_request_body).json()

        # Create user object and log user in.
        user_id = user_info['id']
        user = User(user_id, "reader")
        users = mongo_client.users
        # Assign the first user to be an admin.
        if len(users) == 0:
            user.role = "admin"
        mongo_client.add_user(user_id, user.role)
        login_user(user)

        # Return to the main page.
        return redirect('/')