def test_put_subcategory_deactivate(self, mock_auth): subcategory = create_subcategory(self.category.id) data = {'active': False} response = self.client.put( f'/subcategories/{subcategory.id}', json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() subcategory = Subcategory.query.first() self.assertFalse(subcategory.active) self.assertTrue(isinstance(subcategory.deactivated_at, datetime.datetime)) # Subcategory cannot be reactivated deactivated_at = subcategory.deactivated_at data = {'active': True} response = self.client.put( f'/subcategories/{subcategory.id}', json=data, headers=auth_headers(), ) subcategory = Subcategory.query.first() self.assertFalse(subcategory.active) self.assertEqual(subcategory.deactivated_at, deactivated_at)
def test_put_criterion_deactivate(self, mock_auth): criterion = create_criterion(self.subcategory.id) data = { 'active': False, } response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() criterion = Criterion.query.first() self.assertFalse(criterion.active) self.assertTrue(isinstance(criterion.deactivated_at, datetime.datetime)) # Criterion cannot be reactivated deactivated_at = criterion.deactivated_at data = { 'active': True, } response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) criterion = Criterion.query.first() self.assertFalse(criterion.active) self.assertEqual(criterion.deactivated_at, deactivated_at)
def test_put_subcategory_doesnt_exist(self, mock_auth): response = self.client.put('/subcategories/1', json={}, headers=auth_headers()) self.assertEqual(response.status_code, 404) mock_auth.assert_called_once() json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.subcategory_not_found)
def test_post_category(self, mock_auth): data = { 'title': 'Definition of Domestic Violence', 'help_text': "This is how a state legally defines the term 'domestic violence'", } response = self.client.post('/categories', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 201) mock_auth.assert_called_once() new_category = Category.query.first() self.assertEqual(new_category.title, 'Definition of Domestic Violence') self.assertEqual( new_category.help_text, "This is how a state legally defines the term 'domestic violence'", ) json_response = json.loads(response.data.decode('utf-8')) self.assertEqual( json_response, { 'id': new_category.id, 'title': 'Definition of Domestic Violence', 'help_text': "This is how a state legally defines the term 'domestic violence'", 'active': True, 'deactivated_at': None, })
def test_put_category(self, mock_auth): category = create_category() data = { 'title': 'A New Title', 'help_text': 'Some new help text', } response = self.client.put( '/categories/%i' % category.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() # Refresh category object category = Category.query.first() self.assertEqual(category.title, 'A New Title') self.assertEqual(category.help_text, 'Some new help text') json_response = json.loads(response.data.decode('utf-8')) self.assertEqual( json_response, { 'id': category.id, 'title': 'A New Title', 'help_text': 'Some new help text', 'active': True, 'deactivated_at': None, })
def test_post_score(self, mock_auth): criterion_id = self.criterion.id state_code = self.state.code data = { 'criterion_id': criterion_id, 'state': state_code, 'meets_criterion': True, } response = self.client.post('/scores', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 201) mock_auth.assert_called_once() score = Score.query.one() self.assertEqual(score.criterion_id, criterion_id) self.assertEqual(score.state, state_code) self.assertTrue(score.meets_criterion) self.assertTrue(isinstance(score.created_at, datetime.datetime)) json_response = json.loads(response.data) self.assertEqual( json_response, { 'id': score.id, 'criterion_id': criterion_id, 'state': state_code, 'meets_criterion': True, })
def test_post_subcategory_category_doesnt_exist(self, mock_auth): data = {'category_id': 0} response = self.client.post('/subcategories', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 400) json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.category_not_found)
def test_put_link(self, mock_auth): link = Link(state=self.state1_code, subcategory_id=self.subcategory.id).save() data = { 'text': 'Section 20 of Statute 39-B', 'url': 'ny.gov/link/to/statute', } response = self.client.put('/links/%i' % link.id, json=data, headers=auth_headers()) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() # Refresh link object link = Link.query.first() subcategory = Subcategory.query.first() self.assertEqual(link.text, 'Section 20 of Statute 39-B') self.assertEqual(link.url, 'ny.gov/link/to/statute') json_response = json.loads(response.data.decode('utf-8')) self.assertEqual( json_response, { 'id': link.id, 'subcategory_id': subcategory.id, 'state': self.state1_code, 'text': 'Section 20 of Statute 39-B', 'url': 'ny.gov/link/to/statute', 'active': True, 'deactivated_at': None, })
def test_put_subcategory(self, mock_auth): category_id = self.category.id subcategory = create_subcategory(category_id) data = { 'title': 'A New Title', 'help_text': 'Some new help text', } response = self.client.put( f'/subcategories/{subcategory.id}', json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() subcategory = Subcategory.query.get(subcategory.id) self.assertEqual(subcategory.category_id, category_id) self.assertEqual(subcategory.title, 'A New Title') self.assertEqual(subcategory.help_text, 'Some new help text') self.assertTrue(subcategory.active) self.assertIsNone(subcategory.deactivated_at) json_response = json.loads(response.data) self.assertEqual(json_response, subcategory.serialize())
def test_post_subcategory_no_category(self, mock_auth): with warnings.catch_warnings(): warnings.simplefilter('ignore', category=SAWarning) response = self.client.post('/subcategories', json={}, headers=auth_headers()) self.assertEqual(response.status_code, 400) json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.category_not_found)
def test_put_category_deactivate(self, mock_auth): category = create_category() data = { 'active': False, } response = self.client.put( '/categories/%i' % category.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() # Refresh category object category = Category.query.first() self.assertFalse(category.active) self.assertTrue(isinstance(category.deactivated_at, datetime.datetime)) json_response = json.loads(response.data.decode('utf-8')) self.assertTrue(isinstance(json_response['deactivated_at'], str)) # Category cannot be reactivated deactivated_at = category.deactivated_at data = { 'active': True, } response = self.client.put( '/categories/%i' % category.id, json=data, headers=auth_headers(), ) category = Category.query.first() self.assertFalse(category.active) self.assertEqual(category.deactivated_at, deactivated_at)
def test_put_criterion(self, mock_auth): subcategory_id = self.subcategory.id criterion = create_criterion(subcategory_id) data = { 'title': 'A New Title', 'help_text': 'Some new help text', } response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() criterion = Criterion.query.first() self.assertEqual(criterion.subcategory_id, subcategory_id) self.assertEqual(criterion.title, 'A New Title') self.assertEqual( criterion.recommendation_text, "The state's definition of domestic violence should include a framework of economic " 'abuse', ) self.assertEqual(criterion.help_text, 'Some new help text') self.assertFalse(criterion.adverse) self.assertTrue(criterion.active) self.assertIsNone(criterion.deactivated_at) json_response = json.loads(response.data) self.assertEqual( json_response, { 'id': criterion.id, 'subcategory_id': subcategory_id, 'title': 'A New Title', 'recommendation_text': "The state's definition of domestic violence should include a framework of " 'economic abuse', 'help_text': 'Some new help text', 'adverse': False, 'active': True, 'deactivated_at': None, })
def test_post_score_no_state(self, mock_auth): data = { 'criterion_id': self.criterion.id, } with warnings.catch_warnings(): warnings.simplefilter('ignore', category=SAWarning) response = self.client.post('/scores', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 400) json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.invalid_state)
def test_put_subcategory_cannot_change_category(self, mock_auth): category_id = self.category.id subcategory = create_subcategory(category_id) data = {'category_id': subcategory.category_id + 1} response = self.client.put( f'/subcategories/{subcategory.id}', json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 400) mock_auth.assert_called_once() json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.cannot_change_category)
def test_post_score_criterion_doesnt_exist(self, mock_auth): criterion_id = self.criterion.id + 1 data = { 'criterion_id': criterion_id, } response = self.client.post('/scores', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 400) mock_auth.assert_called_once() json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.criterion_not_found)
def test_put_link_cannot_change_state(self, mock_auth): link = Link(state=self.state1_code, subcategory_id=self.subcategory.id).save() data = { 'state': self.state2_code, } response = self.client.put('/links/%i' % link.id, json=data, headers=auth_headers()) self.assertEqual(response.status_code, 400) json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.cannot_change_state)
def test_post_link_no_state(self, mock_auth): data = { 'subcategory_id': self.subcategory.id, 'text': 'Section 20 of Statute 39-B', 'url': 'ny.gov/link/to/statute', } with warnings.catch_warnings(): warnings.simplefilter('ignore', category=SAWarning) response = self.client.post('/links', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 400) json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.invalid_state)
def test_put_criterion_cannot_change_subcategory(self, mock_auth): subcategory_id = self.subcategory.id criterion = create_criterion(subcategory_id) data = {'subcategory_id': subcategory_id + 1} response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 400) mock_auth.assert_called_once() json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.cannot_change_subcategory)
def test_post_subcategory(self, mock_auth): category_id = self.category.id data = { 'category_id': category_id, 'title': 'Safe Work Environment', 'help_text': 'Some help text', } response = self.client.post('/subcategories', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 201) mock_auth.assert_called_once() subcategory = Subcategory.query.one() self.assertEqual(subcategory.category_id, category_id) self.assertEqual(subcategory.title, 'Safe Work Environment') self.assertEqual(subcategory.help_text, 'Some help text') self.assertTrue(subcategory.active) self.assertIsNone(subcategory.deactivated_at) json_response = json.loads(response.data) self.assertEqual(json_response, subcategory.serialize())
def test_put_link_deactivate(self, mock_auth): link = Link(state=self.state1_code, subcategory_id=self.subcategory.id).save() data = { 'active': False, } response = self.client.put('/links/%i' % link.id, json=data, headers=auth_headers()) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() # Refresh link object link = Link.query.first() self.assertFalse(link.active) self.assertTrue(isinstance(link.deactivated_at, datetime.datetime)) json_response = json.loads(response.data.decode('utf-8')) self.assertTrue(isinstance(json_response['deactivated_at'], str))
def test_post_criterion(self, mock_auth): subcategory_id = self.subcategory.id data = { 'subcategory_id': subcategory_id, 'title': 'Includes economic abuse framework', 'recommendation_text': "The state's definition of domestic violence should include a framework of " 'economic abuse', 'help_text': 'This means that the state acknowledges the role that economic control and abuse ' 'can play in domestic violence', 'adverse': False, } response = self.client.post('/criteria', json=data, headers=auth_headers()) self.assertEqual(response.status_code, 201) mock_auth.assert_called_once() criterion = Criterion.query.one() self.assertEqual(criterion.subcategory_id, subcategory_id) self.assertEqual(criterion.title, 'Includes economic abuse framework') self.assertEqual( criterion.recommendation_text, "The state's definition of domestic violence should include a framework of economic " 'abuse', ) self.assertEqual( criterion.help_text, 'This means that the state acknowledges the role that economic control and abuse can ' 'play in domestic violence', ) self.assertFalse(criterion.adverse) self.assertTrue(criterion.active) self.assertIsNone(criterion.deactivated_at) json_response = json.loads(response.data) self.assertEqual( json_response, { 'id': criterion.id, 'subcategory_id': subcategory_id, 'title': 'Includes economic abuse framework', 'recommendation_text': "The state's definition of domestic violence should include a framework of " 'economic abuse', 'help_text': 'This means that the state acknowledges the role that economic control and abuse ' 'can play in domestic violence', 'adverse': False, 'active': True, 'deactivated_at': None, })