Example #1
0
    def test_get(self):
        tc = self.test_client
        ## Login required
        setup.assertRequiresLogin(self, tc.get("/goals/0/check-ins/0/"))

        ## Goal exists
        self.login()
        setup.assert404(self, tc.get("/goals/0/check-ins/0/"))
        
        ## user is goal owner
        goal = self.create_test_numeric_goal()
        goal_id = str(goal.get_id())
        self.logout()
        self.login_other_user()
        setup.assertInvalidCredentials(self, tc.get("/goals/" + goal_id + "/check-ins/0/"))
        
        ## Check in must exist
        self.logout()
        self.login()
        setup.assert404(self, tc.get("/goals/" + goal_id + "/check-ins/0/"))

        ## Returns correctly
        ci = CheckIn(goal, Timeframe.get_current_timeframe(goal.check_in_frequency_name), 1)
        ci.persist()
        tfid = str(ci.timeframe)
        res = tc.get("/goals/" + goal_id + "/check-ins/" + tfid + "/")
        setup.assertOk(self, res, 200)
        self.assertEqual(json.loads(res.data), ci.to_dict())
Example #2
0
    def test_get(self):
        tc = self.test_client
        ## Login required
        setup.assertRequiresLogin(self, tc.get("/goals/0/check-ins/0/"))

        ## Goal exists
        self.login()
        setup.assert404(self, tc.get("/goals/0/check-ins/0/"))

        ## user is goal owner
        goal = self.create_test_numeric_goal()
        goal_id = str(goal.get_id())
        self.logout()
        self.login_other_user()
        setup.assertInvalidCredentials(
            self, tc.get("/goals/" + goal_id + "/check-ins/0/"))

        ## Check in must exist
        self.logout()
        self.login()
        setup.assert404(self, tc.get("/goals/" + goal_id + "/check-ins/0/"))

        ## Returns correctly
        ci = CheckIn(
            goal,
            Timeframe.get_current_timeframe(goal.check_in_frequency_name), 1)
        ci.persist()
        tfid = str(ci.timeframe)
        res = tc.get("/goals/" + goal_id + "/check-ins/" + tfid + "/")
        setup.assertOk(self, res, 200)
        self.assertEqual(json.loads(res.data), ci.to_dict())
Example #3
0
def get_current_check_in(id):
    """Get the current check-in for a given goal.  Returns 404 if the current check-in or goal does not exist"""
    goal = Goal.pull_by_id(id)
    if (not goal):
        raise NotFoundError()
    
    if goal.user != current_user.get_id():
        raise UnauthorizedError
    
    check_in = CheckIn.pull_by_goal_timeframe(id, Timeframe.get_current_timeframe(goal.check_in_frequency_name).get_id())
    if (not check_in):
        raise NotFoundError()

    return check_in.to_json(), 200
Example #4
0
def check_in(id):
    validate_form(request.form, ["value"])
    goal = Goal.pull_by_id(id)
    if (not goal):
        raise NotFoundError()
    
    if goal.user != current_user.get_id():
        raise UnauthorizedError
    
    if ('timeframe' in request.form):
        timeframe = Timeframe.pull_by_id(request.form['timeframe'])
    else:
        timeframe = Timeframe.get_current_timeframe(goal.check_in_frequency_name)

    check_in = CheckIn(goal, timeframe, request.form['value'])
    return_code = 200 if check_in.exists() else 201
    check_in.persist()

    return check_in.to_json(), return_code
Example #5
0
    def test_check_in(self):
        tc = self.test_client

        # Requires Login:
        res = tc.post("/goals/1/check-ins/", data={"value": 1})
        setup.assertRequiresLogin(self, res)

        ## Requires "value"
        self.login()
        res = tc.post("/goals/1/check-ins/", data={"not-value": 1})
        setup.assertInvalid(self, res, "value")

        ## Must be a goal that exists
        res = tc.post("/goals/0/check-ins/", data={"value": 1})
        setup.assert404(self, res)        
        
        ## User must own the goal in question
        numeric_goal = self.create_test_numeric_goal()
        numeric_goal_id = str(numeric_goal.get_id()) ## You have to do this before you log out, for some reason?
        binary_goal = self.create_test_binary_goal()
        binary_goal_id = str(binary_goal.get_id())
        
        self.logout()
        self.login_other_user()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1})
        setup.assertInvalidCredentials(self, res)
        
        ## Check-in must conform (true / false vs. numeric)
        self.logout()
        self.login()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": "true"})
        setup.assertBadData(self, res, "Value must be numeric")

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/", data={"value": 10})
        setup.assertBadData(self, res, "Value must be a boolean")        

        ## Check-in is returned with 201 if no timeframe is given, returning current timeframe (both numeric & binary)
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/", data={"value": True})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), binary_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## An updated check-in is returned with 200 if no timeframe is given, returning current timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 3})
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## Check-in is returned with 201 if timeframe ID is given, returning correct timeframe
        tf = Timeframe.get_timeframe("daily", datetime.datetime(2016, 1, 1))
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1, "timeframe": tf.get_id()})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)
        
        ## Updated check-in is returned with 200 if Timeframe ID is given, returning correct timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 3, "timeframe": tf.get_id()})
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)
Example #6
0
    def test_check_in(self):
        tc = self.test_client

        # Requires Login:
        res = tc.post("/goals/1/check-ins/", data={"value": 1})
        setup.assertRequiresLogin(self, res)

        ## Requires "value"
        self.login()
        res = tc.post("/goals/1/check-ins/", data={"not-value": 1})
        setup.assertInvalid(self, res, "value")

        ## Must be a goal that exists
        res = tc.post("/goals/0/check-ins/", data={"value": 1})
        setup.assert404(self, res)

        ## User must own the goal in question
        numeric_goal = self.create_test_numeric_goal()
        numeric_goal_id = str(numeric_goal.get_id(
        ))  ## You have to do this before you log out, for some reason?
        binary_goal = self.create_test_binary_goal()
        binary_goal_id = str(binary_goal.get_id())

        self.logout()
        self.login_other_user()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": 1})
        setup.assertInvalidCredentials(self, res)

        ## Check-in must conform (true / false vs. numeric)
        self.logout()
        self.login()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": "true"})
        setup.assertBadData(self, res, "Value must be numeric")

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/",
                      data={"value": 10})
        setup.assertBadData(self, res, "Value must be a boolean")

        ## Check-in is returned with 201 if no timeframe is given, returning current timeframe (both numeric & binary)
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": 1})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'],
                         Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/",
                      data={"value": True})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), binary_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'],
                         Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## An updated check-in is returned with 200 if no timeframe is given, returning current timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": 3})
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'],
                         Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## Check-in is returned with 201 if timeframe ID is given, returning correct timeframe
        tf = Timeframe.get_timeframe("daily", datetime.datetime(2016, 1, 1))
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={
                          "value": 1,
                          "timeframe": tf.get_id()
                      })
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## Updated check-in is returned with 200 if Timeframe ID is given, returning correct timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={
                          "value": 3,
                          "timeframe": tf.get_id()
                      })
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)