/
songs_controller_test.rb
46 lines (37 loc) · 1.53 KB
/
songs_controller_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require "test_helper"
class Api::V1::FavoritePlaylist::SongsControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:visitor1)
@playlist = @user.favorite_playlist
@playlist.song_ids = [1, 2]
end
test "should add songs to playlist" do
post api_v1_favorite_playlist_songs_url, params: {song_id: 3}, as: :json, headers: api_token_header(@user)
response = @response.parsed_body
assert_response :success
assert_equal 3, response["id"]
assert response["is_favorited"]
assert_equal [3, 1, 2], @playlist.reload.song_ids
end
test "should remove songs from playlist" do
delete api_v1_favorite_playlist_song_url(id: 1), as: :json, headers: api_token_header(@user)
response = @response.parsed_body
assert_response :success
assert_equal 1, response["id"]
assert_not response["is_favorited"]
assert_equal [2], @playlist.reload.song_ids
delete api_v1_favorite_playlist_song_url(id: 2), as: :json, headers: api_token_header(@user)
response = @response.parsed_body
assert_response :success
assert_equal 2, response["id"]
assert_not response["is_favorited"]
assert_equal [], @playlist.reload.song_ids
end
test "should not add song to playlist if already in playlist" do
post api_v1_favorite_playlist_songs_url, params: {song_id: 1}, as: :json, headers: api_token_header(@user)
response = @response.parsed_body
assert_response :bad_request
assert_equal "DuplicatePlaylistSong", response["type"]
assert_not_empty response["message"]
end
end