1- import asyncio
2- import json
3-
41import pytest
52
63from galaxy .api .types import Authentication
74from galaxy .api .errors import (
85 UnknownError , InvalidCredentials , NetworkError , LoggedInElsewhere , ProtocolError ,
96 BackendNotAvailable , BackendTimeout , BackendError , TemporaryBlocked , Banned , AccessDenied
107)
8+ from galaxy .unittest .mock import async_return_value
9+
10+ from tests import create_message , get_messages
11+
1112
12- def test_success (plugin , read , write ):
13+ @pytest .mark .asyncio
14+ async def test_success (plugin , read , write ):
1315 request = {
1416 "jsonrpc" : "2.0" ,
1517 "id" : "3" ,
1618 "method" : "init_authentication"
1719 }
18-
19- read .side_effect = [json .dumps (request ).encode () + b"\n " , b"" ]
20- plugin .authenticate .coro .return_value = Authentication ("132" , "Zenek" )
21- asyncio .run (plugin .run ())
20+ read .side_effect = [async_return_value (create_message (request )), async_return_value (b"" )]
21+ plugin .authenticate .return_value = async_return_value (Authentication ("132" , "Zenek" ))
22+ await plugin .run ()
2223 plugin .authenticate .assert_called_with ()
23- response = json .loads (write .call_args [0 ][0 ])
2424
25- assert response == {
26- "jsonrpc" : "2.0" ,
27- "id" : "3" ,
28- "result" : {
29- "user_id" : "132" ,
30- "user_name" : "Zenek"
25+ assert get_messages (write ) == [
26+ {
27+ "jsonrpc" : "2.0" ,
28+ "id" : "3" ,
29+ "result" : {
30+ "user_id" : "132" ,
31+ "user_name" : "Zenek"
32+ }
3133 }
32- }
34+ ]
35+
3336
37+ @pytest .mark .asyncio
3438@pytest .mark .parametrize ("error,code,message" , [
3539 pytest .param (UnknownError , 0 , "Unknown error" , id = "unknown_error" ),
3640 pytest .param (BackendNotAvailable , 2 , "Backend not available" , id = "backend_not_available" ),
@@ -44,29 +48,32 @@ def test_success(plugin, read, write):
4448 pytest .param (Banned , 105 , "Banned" , id = "banned" ),
4549 pytest .param (AccessDenied , 106 , "Access denied" , id = "access_denied" ),
4650])
47- def test_failure (plugin , read , write , error , code , message ):
51+ async def test_failure (plugin , read , write , error , code , message ):
4852 request = {
4953 "jsonrpc" : "2.0" ,
5054 "id" : "3" ,
5155 "method" : "init_authentication"
5256 }
5357
54- read .side_effect = [json . dumps ( request ). encode () + b" \n " , b"" ]
55- plugin .authenticate .coro . side_effect = error ()
56- asyncio . run ( plugin .run () )
58+ read .side_effect = [async_return_value ( create_message ( request )), async_return_value ( b"" ) ]
59+ plugin .authenticate .side_effect = error ()
60+ await plugin .run ()
5761 plugin .authenticate .assert_called_with ()
58- response = json .loads (write .call_args [0 ][0 ])
5962
60- assert response == {
61- "jsonrpc" : "2.0" ,
62- "id" : "3" ,
63- "error" : {
64- "code" : code ,
65- "message" : message
63+ assert get_messages (write ) == [
64+ {
65+ "jsonrpc" : "2.0" ,
66+ "id" : "3" ,
67+ "error" : {
68+ "code" : code ,
69+ "message" : message
70+ }
6671 }
67- }
72+ ]
73+
6874
69- def test_stored_credentials (plugin , read , write ):
75+ @pytest .mark .asyncio
76+ async def test_stored_credentials (plugin , read , write ):
7077 request = {
7178 "jsonrpc" : "2.0" ,
7279 "id" : "3" ,
@@ -77,39 +84,37 @@ def test_stored_credentials(plugin, read, write):
7784 }
7885 }
7986 }
80- read .side_effect = [json . dumps ( request ). encode () + b" \n " , b"" ]
81- plugin .authenticate .coro . return_value = Authentication ("132" , "Zenek" )
82- asyncio . run ( plugin .run () )
87+ read .side_effect = [async_return_value ( create_message ( request )), async_return_value ( b"" ) ]
88+ plugin .authenticate .return_value = async_return_value ( Authentication ("132" , "Zenek" ) )
89+ await plugin .run ()
8390 plugin .authenticate .assert_called_with (stored_credentials = {"token" : "ABC" })
8491 write .assert_called ()
8592
86- def test_store_credentials (plugin , write ):
93+
94+ @pytest .mark .asyncio
95+ async def test_store_credentials (plugin , write ):
8796 credentials = {
8897 "token" : "ABC"
8998 }
99+ plugin .store_credentials (credentials )
90100
91- async def couritine ():
92- plugin .store_credentials (credentials )
93-
94- asyncio .run (couritine ())
95- response = json .loads (write .call_args [0 ][0 ])
96-
97- assert response == {
98- "jsonrpc" : "2.0" ,
99- "method" : "store_credentials" ,
100- "params" : credentials
101- }
102-
103- def test_lost_authentication (plugin , write ):
101+ assert get_messages (write ) == [
102+ {
103+ "jsonrpc" : "2.0" ,
104+ "method" : "store_credentials" ,
105+ "params" : credentials
106+ }
107+ ]
104108
105- async def couritine ():
106- plugin .lost_authentication ()
107109
108- asyncio .run (couritine ())
109- response = json .loads (write .call_args [0 ][0 ])
110+ @pytest .mark .asyncio
111+ async def test_lost_authentication (plugin , write ):
112+ plugin .lost_authentication ()
110113
111- assert response == {
112- "jsonrpc" : "2.0" ,
113- "method" : "authentication_lost" ,
114- "params" : None
115- }
114+ assert get_messages (write ) == [
115+ {
116+ "jsonrpc" : "2.0" ,
117+ "method" : "authentication_lost" ,
118+ "params" : None
119+ }
120+ ]
0 commit comments