Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged Blog comments backend into develop #50

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow executes several linters on changed files based on languages used in your code base whenever
# you push a code or open a pull request.
#
# You can adjust the behavior by modifying this file.
# For more information, see:
# https://github.com/github/super-linter
name: Lint Code Base

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
run-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0

- name: Lint Code Base
uses: github/super-linter@v4
env:
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: "master"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2 on 2024-02-15 20:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='blog',
old_name='data_posted',
new_name='date_posted',
),
migrations.AlterField(
model_name='blog',
name='id',
field=models.AutoField(primary_key=True, serialize=False),
),
]
18 changes: 18 additions & 0 deletions lvlgg_backend/blog/migrations/0003_alter_blog_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2 on 2024-02-15 23:06

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_rename_data_posted_blog_date_posted_alter_blog_id'),
]

operations = [
migrations.AlterField(
model_name='blog',
name='author',
field=models.TextField(default='EMPTY'),
),
]
21 changes: 21 additions & 0 deletions lvlgg_backend/blog/migrations/0004_alter_blog_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.2 on 2024-02-25 01:26

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0003_alter_blog_author'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name='blog',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
18 changes: 18 additions & 0 deletions lvlgg_backend/blog/migrations/0005_alter_blog_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.2 on 2024-02-25 02:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0004_alter_blog_author'),
]

operations = [
migrations.AlterField(
model_name='blog',
name='author',
field=models.TextField(default='None'),
),
]
21 changes: 21 additions & 0 deletions lvlgg_backend/blog/migrations/0006_alter_blog_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.2 on 2024-02-25 04:45

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0005_alter_blog_author'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name='blog',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
4 changes: 2 additions & 2 deletions lvlgg_backend/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

# Create your models here.
class Blog(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
content = models.TextField()
data_posted = models.DateTimeField(default=timezone.now)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(Client, on_delete=models.CASCADE, null=True)

def __str__(self):
return self.title
209 changes: 209 additions & 0 deletions lvlgg_backend/blog/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,212 @@
import json

from django.test import TestCase
from django.test.client import Client
from account.models import Client
from blog.models import Blog
from django.urls import reverse

# Create your tests here.

class BlogViewTestCase(TestCase):

def setUp(self):
# Create a Client instance
self.client_user = Client.objects.create(username="user1", email="user1@example.com", password="testpassword", firstname="Jerry", lastname="Tom")
# Verify that the Client instance is created
self.assertIsNotNone(self.client_user)
# Verify that the primary key is retrieved correctly
self.assertIsNotNone(self.client_user.pk)
# Save the primary key for later use
self.user_pk = self.client_user.pk

self.blog_post = Blog.objects.create(
title="Sample Blog Post",
content="This is a sample blog post content.",
author=self.client_user, # Use the previously created Client instance as the author
)
self.assertIsNotNone(self.blog_post)
self.assertIsNotNone(self.blog_post.pk)
self.blog_pk = self.blog_post.pk


def test_post_blog(self):
"""
Test creating a new blog
"""
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk
}

url = "/blog/create_blog/"

response = self.client.post(url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

payload['title'] = ""
response = self.client.post(url, payload, content_type="application/json")
self.assertEqual(response.status_code, 400)
payload['title'] = "My title"

payload['content'] = ""
response = self.client.post(url, payload, content_type="application/json")
self.assertEqual(response.status_code, 400)
payload['content'] = "Stuff for my bloggggg"

payload['author'] = 1000000
response = self.client.post(url, payload, content_type="application/json")
self.assertEqual(response.status_code, 404)

def test_get_blogs(self):
"""
Test getting all blogs
"""
# Create a blog first
payload = {
"content": "Stuff for a bloggggg",
"title": "My Title",
"author": self.user_pk
}
create_url = "/blog/create_blog/"
response = self.client.post(create_url, payload, content_type="application/json")
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = "/blog/getlist/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is not empty
self.assertTrue(blogs)

# Check the structure of each blog in the list
for blog in blogs:
self.assertIn('id', blog)
self.assertIn('title', blog)
self.assertIn('content', blog)
self.assertIn('date_posted', blog)
self.assertIn('author', blog)

first_blog = blogs[1]
self.assertEqual(first_blog['content'], payload['content'])
self.assertEqual(first_blog['title'], payload['title'])
self.assertEqual(first_blog['author'], payload['author'])

def test_get_blogs_empty(self):
"""
Test getting all blogs when there are no blogs
"""
url = f"/blog/delete_blog/{self.blog_pk}/"
response = self.client.delete(url)
self.assertEqual(response.status_code, 200)

# Retrieve list of blogs
get_url = "/blog/getlist/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

# Check if the response contains a 'blogs' key
self.assertIn('blogs', data)

# Get the list of blogs from the response
blogs = data['blogs']

# Check if the returned list is empty
self.assertFalse(blogs) # or self.assertEqual(len(blogs), 0) as an alternative

def test_update_blog(self):
"""
Test updating a blog
"""

update_url = f"/blog/update_blog/{self.blog_pk}/"

update = {
"content": "I updated the blog guys",
"title": "My Title V2",
}

response = self.client.put(update_url, update, content_type="application/json")
self.assertEqual(response.status_code, 200)

update['content'] = ''
response = self.client.put(update_url, update, content_type="application/json")
self.assertEqual(response.status_code, 400)
update['content'] = 'I updated the blog Guys'

update['title'] = ''
response = self.client.put(update_url, update, content_type="application/json")
self.assertEqual(response.status_code, 400)
update['title'] = 'My Title V2'

def test_update_blog_fail(self):
"""
Test updating a blog when using a bad pk
"""
key = 99
update_url = f"/blog/update_blog/{key}/"

update = {
"content": "I updated the blog guys",
"title": "My Title V2",
}

response = self.client.put(update_url, update, content_type="application/json")
self.assertEqual(response.status_code, 404)

def test_specific_get(self):
"""
Test getting a specific blog
"""
get_url = f"/blog/get_blog/{self.blog_pk}/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 200)

# Parse JSON response
data = json.loads(response.content.decode('utf-8'))

self.assertEqual(data['content'], "This is a sample blog post content.")
self.assertEqual(data['title'], "Sample Blog Post")

get_url = "/blog/get_blog/99/"
response = self.client.get(get_url)

# Check response status code
self.assertEqual(response.status_code, 500)

def test_delete(self):
"""
Test deleting a specific blog
"""
url = f"/blog/delete_blog/{self.blog_pk}/"
response = self.client.delete(url)
self.assertEqual(response.status_code, 200)

def test_delete_fail(self):
"""
Test Deleting a specific blog a bad pk
"""
key = 99
url = f"/blog/delete_blog/{key}/"
response = self.client.delete(url)
self.assertEqual(response.status_code, 404)
11 changes: 7 additions & 4 deletions lvlgg_backend/blog/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# accounts/urls.py
from django.urls import path
from .views import BlogDetailView
from .views import *

urlpatterns = [
path("blog/", BlogDetailView.as_view()),
]
path("create_blog/", Blogs.as_view()),
path("getlist/", Blogs.as_view()),
path("delete_blog/<int:pk>/", Blogs.as_view()),
path("get_blog/<int:pk>/", GetBlogs.as_view()),
path("update_blog/<int:pk>/", Blogs.as_view()),
]
Loading
Loading