Exemplo n.º 1
0
 def test_get_by_name(self):
     ## Fails on bogus name:
     with self.assertRaisesRegexp(AssertionError, "Frequency must be one of"):
         Frequency.get_id_by_name("Bogus")
     
     id = Frequency.get_id_by_name('weekly')
     self.assertEqual(Frequency.get_name_by_id(id), 'weekly')
Exemplo n.º 2
0
    def test_get_by_name(self):
        ## Fails on bogus name:
        with self.assertRaisesRegexp(AssertionError,
                                     "Frequency must be one of"):
            Frequency.get_id_by_name("Bogus")

        id = Frequency.get_id_by_name('weekly')
        self.assertEqual(Frequency.get_name_by_id(id), 'weekly')
Exemplo n.º 3
0
    def test_create(self):
        # Requires login:
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        setup.assertRequiresLogin(self, res)

        self.login()

        ## Test that it rejects bad inputs
        bad_goal = self.new_goal.copy()
        bad_goal['name'] = None
        res = self.test_client.post("/goals/create/", data=bad_goal)
        setup.assertInvalid(self, res, "name")

        bad_goal['name'] = self.new_goal['name']
        bad_goal['prompt'] = ""
        res = self.test_client.post("/goals/create/", data=bad_goal)

        setup.assertBadData(self, res,
                            "Prompt must be between 0 and 255 characters")

        ## Test that it actually registers at all :-)
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn('id', data)
        goal = Goal.pull_by_id(data['id'])
        self.assertIsNotNone(goal)
        for key, val in self.new_goal.iteritems():
            self.assertEqual(val, data[key])
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(val,
                                 Frequency.get_name_by_id(getattr(goal, key)))
            else:
                self.assertEqual(val, getattr(goal, key))

        ## Shouldn't be able to register something that already exists
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        self.assertEqual(res.status_code, 409)
        data = json.loads(res.data)
        self.assertIn('detail', data)
        self.assertIn('Test goal already exists', data['detail'])
Exemplo n.º 4
0
    def test_create(self):
        # Requires login:
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        setup.assertRequiresLogin(self, res)

        self.login()

        ## Test that it rejects bad inputs
        bad_goal = self.new_goal.copy()
        bad_goal['name'] = None
        res = self.test_client.post("/goals/create/", data=bad_goal)
        setup.assertInvalid(self, res, "name")

        bad_goal['name'] = self.new_goal['name']
        bad_goal['prompt'] = "" 
        res = self.test_client.post("/goals/create/", data=bad_goal)

        setup.assertBadData(self, res, "Prompt must be between 0 and 255 characters")

        ## Test that it actually registers at all :-)
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn('id', data)
        goal = Goal.pull_by_id(data['id'])
        self.assertIsNotNone(goal)
        for key, val in self.new_goal.iteritems():
            self.assertEqual(val, data[key])
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(val, Frequency.get_name_by_id(getattr(goal, key)))
            else:
                self.assertEqual(val, getattr(goal, key))

        ## Shouldn't be able to register something that already exists
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        self.assertEqual(res.status_code, 409)
        data = json.loads(res.data)
        self.assertIn('detail', data)
        self.assertIn('Test goal already exists', data['detail'])
Exemplo n.º 5
0
 def reconstruct(self):
     self.frequency_name = Frequency.get_name_by_id(self.frequency)
     self.check_in_frequency_name = Frequency.get_name_by_id(self.check_in_frequency)
Exemplo n.º 6
0
    def test_update(self):
        ## Requires login
        res = self.test_client.post("/goals/update/", data={'id': 0})
        setup.assertRequiresLogin(self, res)

        ## Validates form
        self.login()
        res = self.test_client.post("/goals/update/", data={'favorite_muppet': 'kermit'})
        setup.assertInvalid(self, res, 'id')        
        
        ## 404's
        res = self.test_client.post("/goals/update/", data={'id': 0, 'name': 'test goal 2.0'})
        setup.assert404(self, res)

        ## Actually works
        goal = self.create_test_goal()
        data = {'id': goal['id']}
        data['name'] = "Test Goal 2.0"
        data['prompt'] = "Guess how many eggs I had for breakfast today!"
        data['frequency'] = "monthly"
        data['target'] = 100
        data['input_type'] = "binary"
        data['active'] = False
        data['public'] = True
        data['check_in_frequency'] = "daily"
        
        res = self.test_client.post("/goals/update/", data=data)
        setup.assertOk(self, res, 200)

        updated_goal = Goal.pull_by_id(goal['id'])
        for key, val in data.iteritems():
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(Frequency.get_name_by_id(getattr(updated_goal, key)), val)
            else:
                self.assertEqual(getattr(updated_goal, key), val)

        ## If any part is invalid, no part should go through
        bad_data = {'id': goal['id']}
        bad_data['name'] = "Test Goal 3.0"
        bad_data['prompt'] = "How many jars of sand did you collect?"
        bad_data['frequency'] = "daily"
        bad_data['target'] = 1000
        bad_data['input_type'] = "numeric"
        bad_data['active'] = True
        bad_data['public'] = "banana"
        bad_data['check_in_frequency'] = "daily"

        res = self.test_client.post("/goals/update/", data=bad_data)
        setup.assertBadData(self, res, "Public must be a boolean")
        updated_goal = Goal.pull_by_id(goal['id'])
        for key, val in data.iteritems():
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(Frequency.get_name_by_id(getattr(updated_goal, key)), val)
            else:
                self.assertEqual(getattr(updated_goal, key), val)

        
        ## Can't update someone else's goal
        self.logout()
        self.login_other_user()
        res = self.test_client.post("/goals/update/", data=bad_data) ## using bad_data here ensures we don't update
        setup.assertInvalidCredentials(self, res)
Exemplo n.º 7
0
    def test_update(self):
        ## Requires login
        res = self.test_client.post("/goals/update/", data={'id': 0})
        setup.assertRequiresLogin(self, res)

        ## Validates form
        self.login()
        res = self.test_client.post("/goals/update/",
                                    data={'favorite_muppet': 'kermit'})
        setup.assertInvalid(self, res, 'id')

        ## 404's
        res = self.test_client.post("/goals/update/",
                                    data={
                                        'id': 0,
                                        'name': 'test goal 2.0'
                                    })
        setup.assert404(self, res)

        ## Actually works
        goal = self.create_test_goal()
        data = {'id': goal['id']}
        data['name'] = "Test Goal 2.0"
        data['prompt'] = "Guess how many eggs I had for breakfast today!"
        data['frequency'] = "monthly"
        data['target'] = 100
        data['input_type'] = "binary"
        data['active'] = False
        data['public'] = True
        data['check_in_frequency'] = "daily"

        res = self.test_client.post("/goals/update/", data=data)
        setup.assertOk(self, res, 200)

        updated_goal = Goal.pull_by_id(goal['id'])
        for key, val in data.iteritems():
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(
                    Frequency.get_name_by_id(getattr(updated_goal, key)), val)
            else:
                self.assertEqual(getattr(updated_goal, key), val)

        ## If any part is invalid, no part should go through
        bad_data = {'id': goal['id']}
        bad_data['name'] = "Test Goal 3.0"
        bad_data['prompt'] = "How many jars of sand did you collect?"
        bad_data['frequency'] = "daily"
        bad_data['target'] = 1000
        bad_data['input_type'] = "numeric"
        bad_data['active'] = True
        bad_data['public'] = "banana"
        bad_data['check_in_frequency'] = "daily"

        res = self.test_client.post("/goals/update/", data=bad_data)
        setup.assertBadData(self, res, "Public must be a boolean")
        updated_goal = Goal.pull_by_id(goal['id'])
        for key, val in data.iteritems():
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(
                    Frequency.get_name_by_id(getattr(updated_goal, key)), val)
            else:
                self.assertEqual(getattr(updated_goal, key), val)

        ## Can't update someone else's goal
        self.logout()
        self.login_other_user()
        res = self.test_client.post(
            "/goals/update/",
            data=bad_data)  ## using bad_data here ensures we don't update
        setup.assertInvalidCredentials(self, res)
Exemplo n.º 8
0
    def test_update(self):
        goal = self.create_test_goal()

        with self.assertRaisesRegexp(goly.errors.ResourceAlreadyExistsError,
                                     "Unable to create goal"):
            goal.update({"name": goal.name})

        data = {
            "name": "New test name",
            "prompt": "New test prompt",
            "frequency": "yearly",
            "check_in_frequency": "quarterly",
            "target": 100,
            "input_type": "numeric",
            "active": False,
            "public": True
        }

        goal.update(data)

        for attr, name in data.iteritems():
            if (attr in ['check_in_frequency', 'frequency']):
                self.assertEqual(Frequency.get_name_by_id(getattr(goal, attr)),
                                 data[attr])
            else:
                self.assertEqual(getattr(goal, attr), data[attr])

        check = Goal.pull_by_id(goal.get_id())  ## make sure these persisted

        for attr, name in data.iteritems():
            if (attr in ['check_in_frequency', 'frequency']):
                self.assertEqual(Frequency.get_name_by_id(getattr(goal, attr)),
                                 data[attr])
            else:
                self.assertEqual(getattr(goal, attr), data[attr])

        with self.assertRaisesRegexp(AssertionError,
                                     "Name must be between 0 and 50"):
            goal.update({"name": " "})

        with self.assertRaisesRegexp(AssertionError,
                                     "Name must be between 0 and 50"):
            goal.update({"name": "a" * 51})

        with self.assertRaisesRegexp(AssertionError,
                                     "Prompt must be between 0 and 255"):
            goal.update({"prompt": " "})

        with self.assertRaisesRegexp(AssertionError,
                                     "Prompt must be between 0 and 255"):
            goal.update({"prompt": "a" * 256})

        with self.assertRaisesRegexp(AssertionError,
                                     "frequency must be one of "):
            goal.update({"frequency": "not-an-option"})

        with self.assertRaisesRegexp(AssertionError,
                                     "Target must be an integer"):
            goal.update({"target": "banana"})

        with self.assertRaisesRegexp(AssertionError,
                                     "Input type must be binary or numeric"):
            goal.update({"input_type": "pineapple"})

        with self.assertRaisesRegexp(AssertionError,
                                     "Active must be a boolean"):
            goal.update({'active': "fish"})

        with self.assertRaisesRegexp(AssertionError,
                                     "Public must be a boolean"):
            goal.update({"public": "filet"})

        with self.assertRaisesRegexp(AssertionError,
                                     "check_in_frequency must be one of"):
            goal.update(
                {"check_in_frequency": "whenever I feel like it, gosh"})

        with self.assertRaisesRegexp(AssertionError,
                                     "Check-in frequency must conform"):
            goal.update({"check_in_frequency": "weekly"})