Skip to content

Commit

Permalink
Channel listing
Browse files Browse the repository at this point in the history
  • Loading branch information
ineentho committed Apr 24, 2015
1 parent 50b46eb commit 1c96942
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
2 changes: 1 addition & 1 deletion runtests
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
nosetests --with-coverage --cover-package server --cover-branches --with-xunit
nosetests --with-coverage --cover-package server --cover-branches --with-xunit --nocapture
4 changes: 2 additions & 2 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def load():

# Register all APIs
import server.auth
import server.public_api
import server.channel_api
import server.api_public
import server.api_channel

db.create_all()

Expand Down
6 changes: 5 additions & 1 deletion server/channel_api.py → server/api_channel.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""
This file contains all API requests that are meant for channel
owners, such as registering channels and adding videos to them
"""

import re
from flask import request, jsonify
from server import app
from server.channel import create_channel, ChannelExistsError, authenticate_channel, post_video, ChannelNotFoundError, \
IncorrectPasswordError, SlugExistsError
from server.util import invalid_parameter, invalid_request


@app.route('/channel/newvideo', methods=['POST'])
def add_new_video():
"""
Expand Down
35 changes: 35 additions & 0 deletions server/api_public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
This file contains all requests that are to be used by
the end user. Examples include searching for channels and
videos
"""

from flask import jsonify
from server import app
from server.channel import Video, Channel


@app.route('/api/channels/')
@app.route('/api/channels/<int:page>')
def list_channels(page=1):
pagination = Channel.query.paginate(page, error_out=False)

# Base response will always be used as the base, even if
# the request fails
base_resp = {
'page': page,
'total-pages': pagination.pages,
'total-channels': pagination.total
}

if len(pagination.items) == 0:
return jsonify(dict(base_resp, error='Page not found'))

channel_list = []
for channel in pagination.items:
channel_list.append({
'channel': channel.name,
'slug': channel.slug,
'url': channel.url
})
return jsonify(dict(base_resp, channels=channel_list))
7 changes: 0 additions & 7 deletions server/public_api.py

This file was deleted.

34 changes: 31 additions & 3 deletions tests/test_channel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest


class ChannelRegisterTestCase(unittest.TestCase):
class ChannelRegistrationTestCase(unittest.TestCase):
def setUp(self):
server.app.config['TESTING'] = True
server.load()
Expand All @@ -15,7 +15,8 @@ def post_json(self, url, data):
headers={'content-type': 'application/json'})

def tearDown(self):
pass
# Not that it should matter, but leaves the db in a clean state
server.recreate_db()

def test_register_no_data(self):
"""
Expand Down Expand Up @@ -100,11 +101,38 @@ def test_valid_parameters(self):
"""
Test if a channel is created when supplied valid parameters
"""

# 0 Channels before
rv = self.app.get('/api/channels/')
self.assertEqual(json.loads(rv.data)['total-channels'], 0)

# Add channel
rv = self.post_json('/channel/register', {
'name': 'test-channel',
'slug': 'test-channel',
'url': 'http://test-channel-servrer.opid.io/test-channel',
'password': 'test123'
})
self.assertIn(b'Channel created', rv.data)
self.assertEqual(rv.status_code, 200)
self.assertEqual(rv.status_code, 200)

# Should be 1 channel now
rv = self.app.get('/api/channels/')
self.assertEqual(json.loads(rv.data)['total-channels'], 1)

def test_no_duplicate_slug(self):
"""
Make sure there cannot be any duplicate slugs
"""

rv = None
for i in range(2):
rv = self.post_json('/channel/register', {
'name': 'test-channel',
'slug': 'test-channel',
'url': 'http://test-channel-servrer.opid.io/test-channel',
'password': 'test123'
})

self.assertIn(b'slug is already in use', rv.data)
self.assertEqual(rv.status_code, 400)

0 comments on commit 1c96942

Please sign in to comment.