Example #1
0
def test_user_registration_post(client, app):
    response = client.post('/auth/registration',
                           json={
                               'username': '******',
                               'password': '******'
                           })
    data = response.get_json()
    with app.app_context():
        user = User.find_by_username('test_user')
        assert user
        assert user.username == 'test_user'
        assert User.verify_hash(user.password, 'test_password')

    assert data['message'] == 'user was created'
    assert data['access_token']
    assert data['refresh_token']
Example #2
0
 def get(self, entry_id):
     current_user = User.find_by_username(get_jwt_identity())
     entry = Entry.query.get(entry_id)
     if entry.user_id == current_user.id:
         return entry_schema.jsonify(entry)
     else:
         return jsonify({'message': 'That\'s not yours!'})
Example #3
0
 def put(self, entry_id):
     current_user = User.find_by_username(get_jwt_identity())
     entry = Entry.query.get(entry_id)
     if entry.user_id == current_user.id:
         new_data = entry_schema.load(request.json).data
         entry.status = new_data['status']
         entry.entry_day = new_data['entry_day']
         db.session.commit()
         return entry_schema.jsonify(entry)
     else:
         return jsonify({'message': 'That\'s not yours to change!'})
Example #4
0
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = create_app({'TESTING': True, 'DATABASE': db_path})

    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_path
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    with app.app_context():
        db.create_all()
        user_1 = User(username='******', password=User.generate_hash('test'))
        user_2 = User(username='******', password=User.generate_hash('test'))
        db.session.add(user_1)
        db.session.add(user_2)
        # have to commit the users first so that their id's can
        # be used to create habits properly
        db.session.commit()

        test_habit1 = Habit(name='test',
                            description='this is a test',
                            start_date=datetime.now(),
                            user_id=user_1.id)
        test_habit2 = Habit(name='test2',
                            description='also a test',
                            start_date=datetime.now(),
                            user_id=user_1.id)
        test_habit3 = Habit(name='test3',
                            description='test for user_2',
                            start_date=datetime.now(),
                            user_id=user_2.id)
        db.session.add(test_habit1)
        db.session.add(test_habit2)
        db.session.add(test_habit3)
        db.session.commit()
        test_habit1.create_entries(datetime.now(), user_1)
        test_habit2.create_entries(datetime.now(), user_1)
        test_habit3.create_entries(datetime.now(), user_2)

    yield app
    os.close(db_fd)
    os.unlink(db_path)
Example #5
0
    def delete(self, habit_id):
        current_user = User.find_by_username(get_jwt_identity())
        # need error handling for when id doesn't exist
        habit = Habit.query.get(habit_id)
        if habit.user_id == current_user.id:
            # only have to delete habit and sqlalchemy will delete
            # it's entries automatically because of a cascade
            db.session.delete(habit)
            db.session.commit()
        else:
            return jsonify({'message': 'That\'s not yours to delete!'})

        return habit_schema.jsonify(habit)
Example #6
0
    def put(self, habit_id):
        current_user = User.find_by_username(get_jwt_identity())
        habit = Habit.query.get(habit_id)
        if habit.user_id == current_user.id:
            new_data = habit_schema.load(request.json).data
            habit.name = new_data['name']
            habit.description = new_data['description']
            # probably shouldn't be able to edit the start date
            # habit.start_date = new_data['start_date']
            db.session.commit()
        else:
            return jsonify({'message': 'That\'s not yours to change!'})

        return habit_schema.jsonify(habit)
Example #7
0
    def post(self):
        current_user = User.find_by_username(get_jwt_identity())
        new_data = habit_schema.load(request.json).data
        # i don't know why habit_schema sometimes doesn't load the date
        # so lets just load it from the request for now
        start_date = request.json['start_date']
        start_date = datetime.strptime(start_date, '%m/%d/%Y')
        new_habit = Habit(name=new_data['name'],
                          description=new_data['description'],
                          start_date=start_date,
                          user_id=current_user.id)
        db.session.add(new_habit)
        # have to commit once here so that Habit exists for Entry to reference later
        db.session.commit()

        new_habit.create_entries(start_date, current_user)

        return habit_schema.jsonify(new_habit)
Example #8
0
    def get(self, habit_id=None):
        """
        Manages GET request for this view. By default habit_id is None
        unless one is provided.
        """
        current_user = User.find_by_username(get_jwt_identity())
        # if no habit_id is specified, just return all the habits
        if habit_id is None:
            query = Habit.query.filter(Habit.user_id == current_user.id).all()
            return habits_schema.jsonify(query)

        # if a habit_id is provided in the url
        elif habit_id:
            # need to add some sort of error handling for when habit_id is out of range
            query = Habit.query.get(habit_id)
            if query is None:
                return jsonify({'message': 'Habit does not exist'})
            elif query.user_id == current_user.id:
                return habit_schema.jsonify(query)
            else:
                return jsonify({'message': 'That\'s not yours!'})