1import pytest
2
3from flexget.components.managed_lists.lists.radarr_list import RadarrAPIService
4
5RADARR_API_KEY = '65e246ce581a426781e1a8645f0a1f2c'
6RADARR_BASE_URL = 'http://127.0.0.1'
7RADARR_PORT = 7878
8
9# Load up a radarr container and put VCR in record mode to record.
10# NOTE: You'll need to reset radarr during runs otherwise the tags generated will have a different id. You'll also need to setup a root folder
11# docker run -d --name=radarr-tmp -p 7878:7878 linuxserver/radarr:nightly
12@pytest.mark.online
13class TestRadarrListActions:
14    config = """
15        templates:
16          global:
17            disable: [seen]
18        tasks:
19          clear_and_add_to_radarr_list:
20            list_clear:
21              what:
22                - radarr_list:
23                    base_url: %(RADARR_BASE_URL)s
24                    api_key: %(RADARR_API_KEY)s
25                    port: %(RADARR_PORT)s
26            mock:
27              - { title: 'Despicable Me 2 (2013)', imdb_id: 'tt1690953', tmdb_id: 93456 }
28              - { title: 'Sinister 2 (2015)', imdb_id: 'tt2752772', tmdb_id: 283445 }
29              - { title: 'Crimson Peak (2015)', imdb_id: 'tt2554274', tmdb_id: 201085 }
30              - { title: 'Deadpool (2016)', imdb_id: 'tt1431045', tmdb_id: 293660 }
31            accept_all: yes
32            list_add:
33              - radarr_list:
34                  base_url: %(RADARR_BASE_URL)s
35                  api_key: %(RADARR_API_KEY)s
36                  port: %(RADARR_PORT)s
37
38          clear_and_add_to_radarr_with_tags:
39            list_clear:
40              what:
41                - radarr_list:
42                    base_url: %(RADARR_BASE_URL)s
43                    api_key: %(RADARR_API_KEY)s
44                    port: %(RADARR_PORT)s
45            mock:
46              - { title: 'Deadpool (2016)', imdb_id: 'tt1431045', tmdb_id: 293660 }
47            accept_all: yes
48            list_add:
49              - radarr_list:
50                  base_url: %(RADARR_BASE_URL)s
51                  api_key: %(RADARR_API_KEY)s
52                  port: %(RADARR_PORT)s
53                  tags: ["movies", "othertag"]
54
55          radarr_list_as_input_plugin:
56            radarr_list:
57              base_url: %(RADARR_BASE_URL)s
58              api_key: %(RADARR_API_KEY)s
59              port: %(RADARR_PORT)s
60              include_data: True
61            accept_all: yes
62
63          remove_from_radarr_list:
64            mock:
65              - { title: "Ocean\'s Twelve (2004)", imdb_id: 'tt0349903', tmdb_id: 163 }
66              - { title: 'Sinister 2 (2015)', imdb_id: 'tt2752772', tmdb_id: 283445 }
67            accept_all: yes
68            list_remove:
69              - radarr_list:
70                  base_url: %(RADARR_BASE_URL)s
71                  api_key: %(RADARR_API_KEY)s
72                  port: %(RADARR_PORT)s
73
74          match_radarr_list:
75            mock:
76              - { title: 'Despicable.Me.2.2013.1080p.BluRay.x264-FlexGet', imdb_id: 'tt1690953', tmdb_id: 93456 }
77              - { title: 'Sinister.2.2015.720p.BluRay.x264-FlexGet', imdb_id: 'tt2752772', tmdb_id: 283445 }
78              - { title: 'Crimson.Peak.2015.720p.BluRay.x264-FlexGet', imdb_id: 'tt2554274', tmdb_id: 201085 }
79              - { title: 'Deadpool.2016.1080p.BluRay.x264-FlexGet', imdb_id: 'tt1431045', tmdb_id: 293660 }
80              - { title: 'Kung.Fu.Panda.3.2016.720p.BluRay.x264-FlexGet', imdb_id: 'tt2267968', tmdb_id: 140300 }
81            list_match:
82              from:
83                - radarr_list:
84                    base_url: %(RADARR_BASE_URL)s
85                    api_key: %(RADARR_API_KEY)s
86                    port: %(RADARR_PORT)s
87    """ % {
88        'RADARR_API_KEY': RADARR_API_KEY,
89        'RADARR_BASE_URL': RADARR_BASE_URL,
90        'RADARR_PORT': RADARR_PORT,
91    }
92
93    def test_radarr_list_tags(self, execute_task, manager):
94        radarr = RadarrAPIService(RADARR_API_KEY, RADARR_BASE_URL, RADARR_PORT)
95        tag_by_id = radarr.add_tag('tag_by_id')["id"]
96        manager.config['tasks']['clear_and_add_to_radarr_with_tags']['list_add'][0]['radarr_list'][
97            'tags'
98        ].append(tag_by_id)
99
100        execute_task('clear_and_add_to_radarr_with_tags')
101        tags = {t["label"].lower(): t["id"] for t in radarr.get_tags()}
102        for movie in radarr.get_movies():
103            assert sorted(movie['tags']) == sorted(
104                [tag_by_id, tags.get("movies"), tags.get("othertag")]
105            )
106
107    # TODO: each action should be own test case
108    def test_radarr_list_actions(self, execute_task):
109        # Begin by clearing and then adding a bunch of movies
110        task = execute_task('clear_and_add_to_radarr_list')
111
112        # By using the list as the input we verify that the
113        # movies added above is returned to us
114        task = execute_task('radarr_list_as_input_plugin')
115        assert task.find_entry(
116            movie_name='Despicable Me 2'
117        ), "movie should have been present in the list but it wasn't"
118        assert task.find_entry(
119            movie_name='Crimson Peak'
120        ), "movie should have been present in the list but it wasn't"
121        assert task.find_entry(
122            movie_name='Deadpool'
123        ), "movie should have been present in the list but it wasn't"
124        assert task.find_entry(
125            movie_name='Sinister 2'
126        ), "movie should have been present in the list but it wasn't"
127
128        # Now we will attempt to remove one existing (Sinister 2) and one
129        # non-existing movie which should not affect anything at all
130        task = execute_task('remove_from_radarr_list')
131
132        # And to verify the list we fetch the list again
133        # Sinister 2 should now be missing
134        task = execute_task('radarr_list_as_input_plugin')
135        assert task.find_entry(
136            movie_name='Despicable Me 2'
137        ), "movie should have been present in the list but it wasn't"
138        assert task.find_entry(
139            movie_name='Crimson Peak'
140        ), "movie should have been present in the list but it wasn't"
141        assert task.find_entry(
142            movie_name='Deadpool'
143        ), "movie should have been present in the list but it wasn't"
144        assert not task.find_entry(
145            movie_name='Sinister 2'
146        ), "movie should not be present in the list but it was"
147
148        # Now we will try to match a bunch of input entries with
149        # the list. Two of the movies should not have been matched.
150        task = execute_task('match_radarr_list')
151        assert task.find_entry(
152            'accepted', title='Despicable.Me.2.2013.1080p.BluRay.x264-FlexGet'
153        ), "movie should have been matched but it wasn't"
154        assert task.find_entry(
155            'accepted', title='Crimson.Peak.2015.720p.BluRay.x264-FlexGet'
156        ), "movie should have been matched but it wasn't"
157        assert task.find_entry(
158            'accepted', title='Deadpool.2016.1080p.BluRay.x264-FlexGet'
159        ), "movie should have been matched but it wasn't"
160        assert task.find_entry(
161            'undecided', title='Sinister.2.2015.720p.BluRay.x264-FlexGet'
162        ), "movie should not have been matched but it was"
163        assert task.find_entry(
164            'undecided', title='Kung.Fu.Panda.3.2016.720p.BluRay.x264-FlexGet'
165        ), "movie should not have been matched but it was"
166
167        # list_match should have removed all the matched movies
168        # so no movies should remain
169        task = execute_task('radarr_list_as_input_plugin')
170        assert len(task.all_entries) == 0, "there should be no movies left in the list"
171