@@ -2205,3 +2205,82 @@ def test_rest_service(self):
22052205 HTTP_AUTHORIZATION = f"Token { token } " ,
22062206 )
22072207 self .assertEqual (response .status_code , 204 )
2208+
2209+ @override_settings (PROMGEN = tests .SETTINGS )
2210+ def test_rest_shard (self ):
2211+ token = Token .objects .filter (user__username = "demo" ).first ().key
2212+
2213+ # Check retrieving shards without token returns 401 Unauthorized
2214+ response = self .client .get (reverse ("api-v2:shard-list" ))
2215+ self .assertEqual (response .status_code , 401 )
2216+
2217+ # Check retrieving all shards
2218+ expected = tests .Data ("examples" , "rest.shard.default.json" ).json ()
2219+ response = self .client .get (
2220+ reverse ("api-v2:shard-list" ),
2221+ HTTP_AUTHORIZATION = f"Token { token } " ,
2222+ )
2223+ self .assertEqual (response .status_code , 200 )
2224+ self .assertEqual (response .json (), expected )
2225+
2226+ # Check retrieving paginated shards
2227+ expected = tests .Data ("examples" , "rest.shard.paginated.json" ).json ()
2228+ response = self .client .get (
2229+ reverse ("api-v2:shard-list" ),
2230+ {"page_number" : 1 , "page_size" : 1 },
2231+ HTTP_AUTHORIZATION = f"Token { token } " ,
2232+ )
2233+ self .assertEqual (response .status_code , 200 )
2234+ self .assertEqual (response .json (), expected )
2235+
2236+ # Check retrieving shards whose "name" contains "test"
2237+ expected = tests .Data ("examples" , "rest.shard.filter_by_name.json" ).json ()
2238+ response = self .client .get (
2239+ reverse ("api-v2:shard-list" ),
2240+ {"name" : "test" },
2241+ HTTP_AUTHORIZATION = f"Token { token } " ,
2242+ )
2243+ self .assertEqual (response .status_code , 200 )
2244+ self .assertEqual (response .json (), expected )
2245+
2246+ # Check retrieving shards with a non-existent "name" returns an empty list
2247+ response = self .client .get (
2248+ reverse ("api-v2:shard-list" ),
2249+ {"name" : "non-existent" },
2250+ HTTP_AUTHORIZATION = f"Token { token } " ,
2251+ )
2252+ self .assertEqual (response .status_code , 200 )
2253+ self .assertEqual (response .data ["count" ], 0 )
2254+
2255+ # Check retrieving shards whose "id" is "1" without token returns 401 Unauthorized
2256+ response = self .client .get (reverse ("api-v2:shard-detail" , args = [1 ]))
2257+ self .assertEqual (response .status_code , 401 )
2258+
2259+ # Check retrieving shards whose "id" is "1"
2260+ expected = tests .Data ("examples" , "rest.shard.detail.json" ).json ()
2261+ response = self .client .get (
2262+ reverse ("api-v2:shard-detail" , args = [1 ]),
2263+ HTTP_AUTHORIZATION = f"Token { token } " ,
2264+ )
2265+ self .assertEqual (response .status_code , 200 )
2266+ self .assertEqual (response .json (), expected )
2267+
2268+ # Check retrieving shards with a non-existent "id" returns 404 Not Found
2269+ response = self .client .get (
2270+ reverse ("api-v2:shard-detail" , args = [- 1 ]),
2271+ HTTP_AUTHORIZATION = f"Token { token } " ,
2272+ )
2273+ self .assertEqual (response .status_code , 404 )
2274+
2275+ # Check retrieving list of projects for a shard without token returns 401 Unauthorized
2276+ response = self .client .get (reverse ("api-v2:shard-projects" , args = [1 ]))
2277+ self .assertEqual (response .status_code , 401 )
2278+
2279+ # Check retrieving list of projects for a shard
2280+ expected = tests .Data ("examples" , "rest.shard.projects.json" ).json ()
2281+ response = self .client .get (
2282+ reverse ("api-v2:shard-projects" , args = [1 ]),
2283+ HTTP_AUTHORIZATION = f"Token { token } " ,
2284+ )
2285+ self .assertEqual (response .status_code , 200 )
2286+ self .assertEqual (response .json (), expected )
0 commit comments