@@ -5,8 +5,20 @@ const Blog = require("../models/blog");
55const { initialBlogs, blogsInDb } = require ( "./test_helper" ) ;
66
77const api = supertest ( app ) ;
8+ let token ;
89
9- beforeEach ( async ( ) => {
10+ beforeAll ( async ( ) => {
11+ await api
12+ . post ( "/api/users" )
13+ . send ( { username : "test" , name : "test" , password : "test" } ) ;
14+
15+ const response = await api
16+ . post ( "/api/login" )
17+ . send ( { username : "test" , password : "test" } ) ;
18+ token = response . body . token ;
19+ } ) ;
20+
21+ beforeEach ( async ( ) => {
1022 await Blog . deleteMany ( { } ) ;
1123 await Blog . insertMany ( initialBlogs ) ;
1224} ) ;
@@ -17,7 +29,21 @@ test("get blogs", async () => {
1729 . expect ( 200 )
1830 . expect ( "Content-Type" , / a p p l i c a t i o n \/ j s o n / ) ;
1931
20- expect ( response . body ) . toHaveLength ( 0 ) ;
32+ expect ( response . body ) . toHaveLength ( initialBlogs . length ) ;
33+ } ) ;
34+
35+ test ( "should require authentication to save a blog" , async ( ) => {
36+ const newBlog = {
37+ title : "Type wars" ,
38+ author : "Unknown" ,
39+ url : "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html" ,
40+ likes : 2 ,
41+ } ;
42+
43+ await api . post ( "/api/blogs" ) . send ( newBlog ) . expect ( 401 ) . expect ( "Unauthorized" ) ;
44+
45+ const res = await api . get ( "/api/blogs" ) ;
46+ expect ( res . body ) . toHaveLength ( initialBlogs . length ) ;
2147} ) ;
2248
2349test ( "save a blog" , async ( ) => {
@@ -26,10 +52,12 @@ test("save a blog", async () => {
2652 author : "Unknown" ,
2753 url : "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html" ,
2854 likes : 2 ,
55+ user : token . id ,
2956 } ;
3057
3158 await api
3259 . post ( "/api/blogs" )
60+ . set ( "Authorization" , `Bearer ${ token } ` )
3361 . send ( newBlog )
3462 . expect ( 201 )
3563 . expect ( "Content-Type" , / a p p l i c a t i o n \/ j s o n / ) ;
@@ -51,10 +79,12 @@ test("if the likes property is missing, default value to 0", async () => {
5179 title : "Type wars" ,
5280 author : "Unknown" ,
5381 url : "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html" ,
82+ user : token . id ,
5483 } ;
5584
5685 const { body : savedBlog } = await api
5786 . post ( "/api/blogs" )
87+ . set ( "Authorization" , `Bearer ${ token } ` )
5888 . send ( newBlog )
5989 . expect ( 201 )
6090 . expect ( "Content-Type" , / a p p l i c a t i o n \/ j s o n / ) ;
@@ -67,37 +97,58 @@ test("return bad request if title and url is missing", async () => {
6797 author : "John Doe" ,
6898 } ;
6999
70- const response = await api . post ( "/api/blogs" ) . send ( newBlog ) ;
100+ const response = await api
101+ . post ( "/api/blogs" )
102+ . set ( "Authorization" , `Bearer ${ token } ` )
103+ . send ( newBlog ) ;
71104 expect ( response . statusCode ) . toBe ( 400 ) ;
72105
73- const { body :blogs } = await api . get ( "/api/blogs" )
74- expect ( blogs ) . toHaveLength ( initialBlogs . length )
106+ const { body : blogs } = await api . get ( "/api/blogs" ) ;
107+ expect ( blogs ) . toHaveLength ( initialBlogs . length ) ;
75108} ) ;
76109
77- test ( "confirm deletion of a blog" , async ( ) => {
78- const blogsAtStart = await blogsInDb ( )
79- const blogToDelete = blogsAtStart [ 0 ]
110+ test ( "confirm deletion of a blog" , async ( ) => {
111+ const blogsAtStart = await blogsInDb ( ) ;
112+ const newBlog = {
113+ title : "Type wars" ,
114+ author : "Unknown" ,
115+ url : "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html" ,
116+ user : token . id ,
117+ } ;
80118
81- const response = await api . delete ( `/api/blogs/${ blogToDelete . id } ` )
82- expect ( response . status ) . toBe ( 204 )
83-
84- const { body :blogsAtEnd } = await api . get ( "/api/blogs" )
85- expect ( blogsAtEnd ) . toHaveLength ( initialBlogs . length - 1 )
86- const contents = blogsAtEnd . map ( blog => blog . title )
119+ const { body : savedBlog } = await api
120+ . post ( "/api/blogs" )
121+ . set ( "Authorization" , `Bearer ${ token } ` )
122+ . send ( newBlog )
123+ . expect ( 201 )
124+ . expect ( "Content-Type" , / a p p l i c a t i o n \/ j s o n / ) ;
125+
126+ const response = await api
127+ . delete ( `/api/blogs/${ savedBlog . id } ` )
128+ . set ( "Authorization" , `Bearer ${ token } ` ) ;
129+ expect ( response . status ) . toBe ( 204 ) ;
130+
131+ const { body : blogsAtEnd } = await api . get ( "/api/blogs" ) ;
132+ expect ( blogsAtEnd ) . toHaveLength ( blogsAtStart . length ) ;
133+ const contents = blogsAtEnd . map ( ( blog ) => blog . title ) ;
87134
88- expect ( contents ) . not . toContain ( blogToDelete . title )
89- } )
135+ expect ( contents ) . not . toContain ( savedBlog . title ) ;
136+ } ) ;
90137
91- test ( "update a blog" , async ( ) => {
138+ test ( "update a blog" , async ( ) => {
92139 const blogsAtStart = await blogsInDb ( ) ;
93- const { title, author, url, id} = blogsAtStart [ 0 ]
140+ const { title, author, url, id } = blogsAtStart [ 0 ] ;
94141
95- const blog = { title, url, author, likes : 30 }
142+ const blog = { title, url, author, likes : 30 } ;
96143
97- const { body :updatedBlog } = await api . put ( `/api/blogs/${ id } ` ) . send ( blog ) . expect ( 200 )
144+ const { body : updatedBlog } = await api
145+ . put ( `/api/blogs/${ id } ` )
146+ . set ( "Authorization" , `Bearer ${ token } ` )
147+ . send ( blog )
148+ . expect ( 200 ) ;
98149
99- expect ( updatedBlog . likes ) . toBe ( 30 )
100- } )
150+ expect ( updatedBlog . likes ) . toBe ( 30 ) ;
151+ } ) ;
101152
102153afterAll ( async ( done ) => {
103154 await mongoose . connection . close ( ) ;
0 commit comments