Skip to content

yeshan333/flask-bearer-token-demo-in-restful-apis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flask-bearer-token-demo-in-restful-apis

pip install virtualenv
virtualenv env
# actviate virtual environment
source env/Scripts/activate
# install dependencies
pip install -r requirements.txt
# start test
nose2
# test_auth_flask.py
import unittest

from app import app

class Flask_APP_Test(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

        self.app.testing = True

    # /oauth/token
    # Httpie: http --form :5000/oauth/token grant_type=password username=shansan password=123456
    def test_token(self):
        result = self.app.post('/oauth/token', \
            data={"grant_type": "password", "username": "shansan", "password": "123456"}, \
            content_type='multipart/form-data')
        data = result.get_json()
        self.assertEqual(data['token_type'], "Bearer")

    # Httpie: http :5000/protect Authorization:"Bearer eyJhbGciOiJIUzUxMiIsImlhdCI6MTU4NDMyNDgzOSwiZXhwIjoxNTg0MzI4NDM5fQ.eyJpZCI6IjEyMzQ1NiJ9.pOFYk4-e-LX3tbpvDOp9wXs1JE5mbPfJQ7y7xm06evEx8Su-hsmYSRH8p_P-RLO48GTG43sBDcBvUZJAXZGy-A"
    def test_visit_by_token(self):
        get_token_response  = self.app.post('/oauth/token', \
            data={"grant_type": "password", "username": "shansan", "password": "123456"}, \
            content_type='multipart/form-data')

        rep_json = get_token_response.get_json()
        token = rep_json['access_token']

        result = self.app.get('/protect', \
            headers={'Authorization': 'Bearer {}'.format(token)})

        self.assertEqual(result.status_code, 200)

other: test by Httpie

http --form :5000/oauth/token grant_type=password username=shansan password=123456

http :5000/protect Authorization:"Bearer eyJhbGciOiJIUzUxMiIsImlhdCI6MTU4NDMyNDgzOSwiZXhwIjoxNTg0MzI4NDM5fQ.eyJpZCI6IjEyMzQ1NiJ9.pOFYk4-e-LX3tbpvDOp9wXs1JE5mbPfJQ7y7xm06evEx8Su-hsmYSRH8p_P-RLO48GTG43sBDcBvUZJAXZGy-A"

About

flask-bearer-token-demo-in-restful-apis

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages