예제 #1
0
def create_app(debug=False):
    app = Flask(__name__)
    app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY'
    app.config['SECRET_KEY'] = 'ANOTHER ONE'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///storytellers.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # DEBUGGING AND TESTING
    app.config['SQLALCHEMY_ECHO'] = False
    app.config['TESTING'] = debug
    app.config['LOGIN_DISABLED'] = True
    app.config['WTF_CSRF_ENABLED'] = False

    for bp in blueprints:
        app.register_blueprint(bp)
        bp.app = app

    db.init_app(app)
    login_manager.init_app(app)
    db.create_all(app=app)

    # create a first admin user
    with app.app_context():
        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        if user is None:
            example = User()
            example.firstname = 'Admin'
            example.lastname = 'Admin'
            example.email = '*****@*****.**'
            example.dateofbirth = datetime.datetime(2020, 10, 5)
            example.is_admin = True
            example.set_password('admin')
            db.session.add(example)
            db.session.commit()

        q = db.session.query(Story).filter(Story.id == 1)
        story = q.first()
        if story is None:
            example = Story()
            example.text = 'Trial story of example admin user :)'
            example.likes = 42
            example.author_id = 1
            example.dicenumber = 6
            example.roll = {
                'dice': ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain']
            }
            example.date = datetime.datetime(2019, 11, 5)
            db.session.add(example)
            db.session.commit()

    return app
예제 #2
0
    def test_json_wall(self):
        app = test_app.test_client()

        with app.session_transaction() as sess:
            db.drop_all()
            db.create_all()

        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        if user is None:
            example = User()
            example.firstname = 'userwall'
            example.lastname = 'theWall'
            example.email = '*****@*****.**'
            example.dateofbirth = datetime.datetime(2020, 10, 5)
            example.is_admin = True
            example.set_password('daddysflownacrosstheocean')
            db.session.add(example)
            db.session.commit()
            q = db.session.query(User).filter(User.email == '*****@*****.**')
            user = q.first()

        q = db.session.query(Story).filter(Story.author_id == user.id)
        story = q.first()
        if story is None:

            example = Story()
            example.text = 'We dont need no education We dont need no...All in all you re just another brick in the wall'
            example.likes = 42
            example.dislikes = 1
            example.dicenumber = 6
            example.author_id = user.id
            db.session.add(example)

            example = Story()
            example.text = 'Leaving just a memory...Snapshot in the family album...Daddy what else did you leave for me?'
            example.likes = 42
            example.dislikes = 0
            example.dicenumber = 4
            example.author_id = user.id
            db.session.add(example)

            db.session.commit()
            q = db.session.query(Story).filter(Story.author_id == user.id)

        stories = []
        thewalltest = Wall(user)
        for s in q:
            s: Story
            thewalltest.add_story(s)
            stories.append({
                'story_id': s.id,
                'text': s.text,
                'likes': s.likes,
                'dislikes': s.dislikes
            })

        reply = app.get('/thewall/' + str(user.id))
        body = json.loads(str(reply.data, 'utf8'))

        self.assertEqual(
            body,
            {
                "id": user.id,
                "firstname": user.firstname,
                "lastname": user.lastname,
                "email": user.email,
                "stories": stories  # thewalltest.stories
            })

        wall_repl = Wall()
        wall_repl.acquire_from_json(reply)
        self.assertEqual(thewalltest.id, wall_repl.id, "Json acquire fail")