Beispiel #1
0
    def test_01_Wines(self):
        try:
            os.remove('db.sqlite')
        except OSError:
            pass
        db.create_all()
        # Create
        wine_1 = {
           'name': "lambrusco",
           'grade': 9,
           'price': 5.99,
           'bottle': 1
           }
        response = self.app.post('/wines',
                                 data=json.dumps(wine_1),
                                 content_type='application/json')
        self.assertEqual(response.status_code, 201)
        global id_lambrusco
        id_lambrusco = json.loads(response.data)['created']
        wine_2 = {
           'name': "mosto",
           'grade': 7,
           'price': 3.5
           }
        response = self.app.post('/wines',
                                 data=json.dumps(wine_2),
                                 content_type='application/json')
        self.assertEqual(response.status_code, 201)
        global id_mosto
        id_mosto = json.loads(response.data)['created']

        # Update
        wine_1['price'] = 10
        response = self.app.put('/wines' + '/' + id_lambrusco,
                                data=json.dumps(wine_1),
                                content_type='application/json')
        self.assertEqual(response.status_code, 200)
        data = json.loads(self.app.get('/wines' + '/' + id_lambrusco).data)
        self.assertEqual(data['price'], wine_1['price'])
        id_lambrusco = data['id']

        # Show by type
        response = self.app.get('/wines/byType/azul')
        self.assertEqual(response.status_code, 404)
        response = self.app.get('/wines/byType/tinto')
        data = json.loads(response.data)
        self.assertEqual(data['red'], ["lambrusco"])
        response = self.app.get('/wines/byType/blanco')
        data = json.loads(response.data)
        self.assertEqual(data['white'], ["mosto"])

        # Delete
        response = self.app.delete('/wines/' + id_mosto)
        self.assertEqual(response.status_code, 200)

        # Get
        response = self.app.get('/wines/' + id_lambrusco)
        data = json.loads(response.data)
        self.assertEqual(data['id'], id_lambrusco)

        # Get unlisted
        response = self.app.get('/wines/jk35')
        self.assertEqual(response.status_code, 404)

        # Get All
        response = self.app.get('/wines')
        data = json.loads(response.data)
        self.assertEqual(data['wines'][0]['name'], "lambrusco")

        # Delete All
        response = self.app.delete('/wines')
        self.assertEqual(response.status_code, 200)

        # Put new
        response = self.app.put('/wines' + '/' + id_lambrusco,
                                data=json.dumps(wine_1),
                                content_type='application/json')
        self.assertEqual(response.status_code, 201)
        id_lambrusco = json.loads(response.data)['created']

        # Bad one
        response = self.app.post('/wines',
                                 data=json.dumps({'price': 2}),
                                 content_type='application/json')
        self.assertEqual(response.status_code, 400)
Beispiel #2
0
@app.route('/logout', methods=['GET'])
@oauth.require_oauth()
def logOut():
    user = request.oauth.user
    session.pop('id', None)
    return make_response(jsonify({"LogedOut": user.username}), 200)


@app.route('/me')
@oauth.require_oauth()
def me():
    return jsonify(username=request.oauth.user.username)


if __name__ == '__main__':
    db.create_all()
    # Got root?
    if not getUserbyName('root'):
        root = User(username='******', passwd='root', id=1)
        client = Client(
            client_id='SjHWna6K40tQdqyVAxEDjR5EGpbLM741oOWJRSz0',
            client_secret='n7T1j51NOFn3ARfrsFOnCrblDUBnPz2yIKcQ5VhSMye3W1YK01',
            _redirect_uris='http://127.0.0.1:8000/authorized',
            _default_scopes='email',
            user_id=1,
        )
        db.session.add(root)
        db.session.add(client)
        db.session.commit()
    app.run(debug=True)
@app.route('/logout', methods=['GET'])
@oauth.require_oauth()
def logOut():
    user = request.oauth.user
    session.pop('id', None)
    return make_response(jsonify({"LogedOut": user.username}), 200)


@app.route('/me')
@oauth.require_oauth()
def me():
    return jsonify(username=request.oauth.user.username)


if __name__ == '__main__':
    db.create_all()
    # Got root?
    if not getUserbyName('root'):
        root = User(username='******', passwd='root', id=1)
        client = Client(
            client_id='SjHWna6K40tQdqyVAxEDjR5EGpbLM741oOWJRSz0',
            client_secret='n7T1j51NOFn3ARfrsFOnCrblDUBnPz2yIKcQ5VhSMye3W1YK01',
            _redirect_uris='http://127.0.0.1:8000/authorized',
            _default_scopes='email',
            user_id=1,
        )
        db.session.add(root)
        db.session.add(client)
        db.session.commit()
    app.run(debug=True)