dankventskalender migrate to flask
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6KB

  1. import pytest
  2. from calender.db import get_db
  3. def test_index(client, auth):
  4. response = client.get('/')
  5. assert b"Log In" in response.data
  6. assert b"Register" in response.data
  7. auth.login()
  8. response = client.get('/')
  9. assert b'Log Out' in response.data
  10. assert b'test title' in response.data
  11. assert b'by test on 2018-01-01' in response.data
  12. assert b'test\nbody' in response.data
  13. assert b'href="/1/update"' in response.data
  14. @pytest.mark.parametrize('path', (
  15. '/create',
  16. '/1/update',
  17. '/1/delete',
  18. ))
  19. def test_login_required(client, path):
  20. response = client.post(path)
  21. assert response.headers['Location'] == 'http://localhost/auth/login'
  22. def test_author_required(app, client, auth):
  23. # change the post author to another user
  24. with app.app_context():
  25. db = get_db()
  26. db.execute('UPDATE post SET author_id = 2 WHERE id = 1')
  27. db.commit()
  28. auth.login()
  29. # current user can't modify other user's post
  30. assert client.post('/1/update').status_code == 403
  31. assert client.post('/1/delete').status_code == 403
  32. # current user doesn't see edit link
  33. assert b'href="/1/update"' not in client.get('/').data
  34. @pytest.mark.parametrize('path', (
  35. '/2/update',
  36. '/2/delete',
  37. ))
  38. def test_exists_required(client, auth, path):
  39. auth.login()
  40. assert client.post(path).status_code == 404
  41. def test_create(client, auth, app):
  42. auth.login()
  43. assert client.get('/create').status_code == 200
  44. client.post('/create', data={'title': 'created', 'body': ''})
  45. with app.app_context():
  46. db = get_db()
  47. count = db.execute('SELECT COUNT(id) FROM post').fetchone()[0]
  48. assert count == 2
  49. def test_update(client, auth, app):
  50. auth.login()
  51. assert client.get('/1/update').status_code == 200
  52. client.post('/1/update', data={'title': 'updated', 'body': ''})
  53. with app.app_context():
  54. db = get_db()
  55. post = db.execute('SELECT * FROM post WHERE id = 1').fetchone()
  56. assert post['title'] == 'updated'
  57. @pytest.mark.parametrize('path', (
  58. '/create',
  59. '/1/update',
  60. ))
  61. def test_create_update_validate(client, auth, path):
  62. auth.login()
  63. response = client.post(path, data={'title': '', 'body': ''})
  64. assert b'Title is required.' in response.data
  65. def test_delete(client, auth, app):
  66. auth.login()
  67. response = client.post('/1/delete')
  68. assert response.headers['Location'] == 'http://localhost/'
  69. with app.app_context():
  70. db = get_db()
  71. post = db.execute('SELECT * FROM post WHERE id = 1').fetchone()
  72. assert post is None