Repository: oppia/oppia
File: core/controllers/blog_dashboard_test.py (Python)

Code before:
        )
        self.assertEqual(
            change_dict['title'],
            json_response['published_blog_post_summary_dicts'][0]['title'],
        )
        self.assertEqual(json_response['draft_blog_post_summary_dicts'], [])

    def test_get_dashboard_with_missing_author_model(self) -> None:
        """Tests that dashboard gracefully handles missing author model."""
        self.signup('neweditor@example.com', 'NewEditor')
        new_editor_id = self.get_user_id_from_email('neweditor@example.com')
        self.add_user_role('NewEditor', feconf.ROLE_ID_BLOG_POST_EDITOR)
        self.login('neweditor@example.com')
        # Create a blog post to trigger author model creation.
        csrf_token = self.get_new_csrf_token()
        self.post_json(
            '%s' % feconf.BLOG_DASHBOARD_DATA_URL,
            {},
            csrf_token=csrf_token,
        )
        # Now delete the author model to simulate it being missing.
        author_model = blog_models.BlogAuthorDetailsModel.get_by_author(
            new_editor_id
        )
        self.assertIsNotNone(author_model)
        assert author_model is not None
        author_model.delete()
        # Verify dashboard still works with missing author model.
        json_response = self.get_json(
            '%s' % (feconf.BLOG_DASHBOARD_DATA_URL),
        )
        self.assertEqual(
            json_response['author_details']['displayed_author_name'], ''
        )
        self.assertEqual(json_response['author_details']['author_bio'], '')
        self.logout()

    def test_create_new_blog_post(self) -> None:
        # Checks blog editor can create a new blog post.
        self.login(self.BLOG_EDITOR_EMAIL)
        csrf_token = self.get_new_csrf_token()
        json_response = self.post_json(
            '%s' % (feconf.BLOG_DASHBOARD_DATA_URL), {}, csrf_token=csrf_token
        )
        blog_post_id = json_response['blog_post_id']
        blog_post_rights = blog_services.get_blog_post_rights(blog_post_id)
        self.assertEqual(blog_post_rights.editor_ids, [self.blog_editor_id])
        self.logout()

        # Checks non blog-admins and non editors cannot create a new blog post.
        self.login(self.user_email)

Review comment (bug):
Is this to satisfy the type system? If so, consider using an if statement and reassignment instead:

```python

if (maybe_author_model := get_by_author(...)) is None:
    self.fail('no such model')
else:
    author_model = maybe_author_model  # <- Now this won't have an optional type
```

Code after:
        )
        self.assertEqual(
            change_dict['title'],
            json_response['published_blog_post_summary_dicts'][0]['title'],
        )
        self.assertEqual(json_response['draft_blog_post_summary_dicts'], [])

    def test_get_dashboard_with_missing_author_model(self) -> None:
        """Tests that dashboard returns default author details when
        the author model is missing.
        """
        self.signup('neweditor@example.com', 'NewEditor')
        new_editor_id = self.get_user_id_from_email('neweditor@example.com')
        self.add_user_role('NewEditor', feconf.ROLE_ID_BLOG_POST_EDITOR)
        self.login('neweditor@example.com')
        # Create a blog post to trigger author model creation.
        csrf_token = self.get_new_csrf_token()
        self.post_json(
            '%s' % feconf.BLOG_DASHBOARD_DATA_URL,
            {},
            csrf_token=csrf_token,
        )
        # Now delete the author model to simulate it being missing.
        author_model = blog_models.BlogAuthorDetailsModel.get_by_author(
            new_editor_id
        )
        if author_model is None:
            self.fail(
                'Expected BlogAuthorDetailsModel to exist for user_id=%s'
                % new_editor_id
            )
        author_model.delete()
        # Verify dashboard returns default author details.
        json_response = self.get_json(
            '%s' % (feconf.BLOG_DASHBOARD_DATA_URL),
        )
        self.assertEqual(
            json_response['author_details']['displayed_author_name'],
            'Deleted User',
        )
        self.assertEqual(json_response['author_details']['author_bio'], '')
        self.logout()

    def test_create_new_blog_post(self) -> None:
        # Checks blog editor can create a new blog post.
        self.login(self.BLOG_EDITOR_EMAIL)
        csrf_token = self.get_new_csrf_token()
        json_response = self.post_json(
            '%s' % (feconf.BLOG_DASHBOARD_DATA_URL), {}, csrf_token=csrf_token
        )
        blog_post_id = json_response['blog_post_id']