def test_retrieve_board_list(self): """Test retrieving a list of board""" board = sample_board(title='Test board', description='Test board description') sample_topic(self.user.profile, board) sample_board(title='Another test board', description='Another test board description') res = self.client.get(BOARDS_URL) boards = Board.objects.all().order_by('id') serializer = BoardSerializer(boards, many=True, context={'request': RequestFactory().get(BOARDS_URL)}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(len(res.data), 2) self.assertAllIn(('id', 'title', 'description', 'topics', 'created_at'), res.data[0].keys()) self.assertAllIn(('href', 'title'), res.data[0]['topics'][0].keys())
def setUp(self): self.board = sample_board() self.user = sample_user() self.topic = sample_topic(starter=self.user.profile, board=self.board) self.post = Post.objects.create(author=self.user.profile, message='Test post message', topic=self.topic)
def test_retrieve_topic_list(self): """Test retrieving a topic list from a board""" sample_topic(starter=self.user.profile, board=self.board, title='Other testing topic') url = get_topic_url(self.board) res = self.client.get(url) self.assertEqual(res.status_code, status.HTTP_200_OK) fields_desired = ('id', 'board', 'title', 'starter', 'post_count', 'first_post', 'created_at') serializer = TopicListSerializer( self.board.topics, many=True, context={'request': RequestFactory().get(url)}) self.assertEqual(res.data, serializer.data) self.assertAllIn(fields_desired, res.data[0].keys()) self.assertAllIn(('username', 'href'), res.data[0]['starter'])
def test_retrieve_board_details(self): """Test retrieving a board's details""" board = sample_board() topic = sample_topic(self.user.profile, board) url = detail_board_url(board) res = self.client.get(url) serializer = BoardSerializer(board, context={'request': RequestFactory().get(url)}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(res.data['topics'][0]['title'], topic.title) self.assertAllIn(('id', 'title', 'description', 'topics', 'created_at'), res.data.keys())
def test_update_topic_requires_ownership(self): """Test that updating a topic requires the ownership""" new_user = sample_user(email='*****@*****.**') new_topic = sample_topic(board=self.board, starter=new_user.profile, title='Topic from another user') url = get_topic_url(self.board, new_topic) payload = {'title': 'Modified topic title'} res = self.client.patch(url, payload) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) new_topic.refresh_from_db() self.assertNotEqual(new_topic.title, payload['title'])
def setUp(self): self.user = sample_user() self.board = sample_board() self.topic = sample_topic(self.user.profile, self.board)