4
4
from clean_links .unshorten import unshorten_url
5
5
6
6
7
+ def test_missing_schema ():
8
+ url = "Ceci n'est pas une URL"
9
+ with pytest .raises (requests .exceptions .MissingSchema ):
10
+ unshorten_url (url )
11
+
12
+
13
+ def test_not_an_address ():
14
+ url = "https://www.definitely-not-a-website.boogety"
15
+ with pytest .raises (requests .exceptions .ConnectionError ):
16
+ unshorten_url (url )
17
+
18
+
7
19
# cassettes/{module_name}/test_single.yaml will be used
8
20
@pytest .mark .vcr
9
21
def test_unchanged ():
@@ -14,65 +26,98 @@ def test_unchanged():
14
26
"resolved" : "https://example.com/" ,
15
27
"status" : 200 ,
16
28
"exception" : None ,
29
+ "request_history" : ["https://example.com/" ],
30
+ "response_history" : [200 ],
17
31
}
18
32
19
33
20
34
@pytest .mark .vcr
21
35
def test_single_redirect ():
22
36
url = "https://trib.al/5m7fAg3"
23
37
result = unshorten_url (url )
38
+ resolved = "https://www.bloomberg.com/news/articles/2024-01-24/cryptocurrency-ai-electricity-demand-seen-doubling-in-three-years?cmpid%3D=socialflow-twitter-tech&utm_content=tech&utm_medium=social&utm_campaign=socialflow-organic&utm_source=twitter"
24
39
assert result == {
25
40
"url" : url ,
26
- "resolved" : "https://www.bloomberg.com/news/articles/2024-01-24/cryptocurrency-ai-electricity-demand-seen-doubling-in-three-years?cmpid%3D=socialflow-twitter-tech&utm_content=tech&utm_medium=social&utm_campaign=socialflow-organic&utm_source=twitter" ,
41
+ "resolved" : resolved ,
27
42
"status" : 200 ,
28
43
"exception" : None ,
44
+ "request_history" : [url , resolved ],
45
+ "response_history" : [301 , 200 ],
29
46
}
30
47
31
48
32
49
@pytest .mark .vcr
33
50
def test_multiple_redirect ():
34
51
url = "https://hubs.la/Q01HRjhm0"
35
52
result = unshorten_url (url )
53
+ resolved = "https://app.east.mentorspaces.com/#!/orgs/6aab4989-2bd1-7ec9-6e3f-56f3128815c8/?utm_content=242450506&utm_medium=social&utm_source=twitter&hss_channel=tw-18419094&_branch_match_id=1280732352261121106&utm_campaign=fall-recruiting&_branch_referrer=H4sIAAAAAAAAAx3KUQqEIBAA0Nv0V1rYsgXSUcJmB5R0FGek61f7%2B3hepPCqVEKSXLk4QB5cKUMMdCriA7cmaYdM8gw7mcnMetaf7tWEv9CS5QzBxb9wbhXQyhVEsHaeeQfviDA%2B1o9fMy56MTdcnGABdQAAAA%3D%3D"
36
54
assert result == {
37
55
"url" : url ,
38
- "resolved" : "https://app.east.mentorspaces.com/#!/orgs/6aab4989-2bd1-7ec9-6e3f-56f3128815c8/?utm_content=242450506&utm_medium=social&utm_source=twitter&hss_channel=tw-18419094&_branch_match_id=1280732352261121106&utm_campaign=fall-recruiting&_branch_referrer=H4sIAAAAAAAAAx3KUQqEIBAA0Nv0V1rYsgXSUcJmB5R0FGek61f7%2B3hepPCqVEKSXLk4QB5cKUMMdCriA7cmaYdM8gw7mcnMetaf7tWEv9CS5QzBxb9wbhXQyhVEsHaeeQfviDA%2B1o9fMy56MTdcnGABdQAAAA%3D%3D" ,
56
+ "resolved" : resolved ,
39
57
"status" : 200 ,
40
58
"exception" : None ,
59
+ "request_history" : [
60
+ url ,
61
+ "https://mentorspaces.app.link/nsbe?utm_content=242450506&utm_medium=social&utm_source=twitter&hss_channel=tw-18419094" ,
62
+ "https://app.east.mentorspaces.com/orgs/6aab4989-2bd1-7ec9-6e3f-56f3128815c8/?utm_content=242450506&utm_medium=social&utm_source=twitter&hss_channel=tw-18419094&_branch_match_id=1280732352261121106&utm_campaign=fall-recruiting&_branch_referrer=H4sIAAAAAAAAAx3KUQqEIBAA0Nv0V1rYsgXSUcJmB5R0FGek61f7%2B3hepPCqVEKSXLk4QB5cKUMMdCriA7cmaYdM8gw7mcnMetaf7tWEv9CS5QzBxb9wbhXQyhVEsHaeeQfviDA%2B1o9fMy56MTdcnGABdQAAAA%3D%3D" ,
63
+ "http://app.east.mentorspaces.com/#!/orgs/6aab4989-2bd1-7ec9-6e3f-56f3128815c8/?utm_content=242450506&utm_medium=social&utm_source=twitter&hss_channel=tw-18419094&_branch_match_id=1280732352261121106&utm_campaign=fall-recruiting&_branch_referrer=H4sIAAAAAAAAAx3KUQqEIBAA0Nv0V1rYsgXSUcJmB5R0FGek61f7%2B3hepPCqVEKSXLk4QB5cKUMMdCriA7cmaYdM8gw7mcnMetaf7tWEv9CS5QzBxb9wbhXQyhVEsHaeeQfviDA%2B1o9fMy56MTdcnGABdQAAAA%3D%3D" ,
64
+ resolved ,
65
+ ],
66
+ "response_history" : [301 , 307 , 301 , 301 , 200 ],
41
67
}
42
68
43
69
44
70
@pytest .mark .vcr
45
71
def test_expired_certificate_ignore ():
46
72
url = "https://expired.badssl.com/"
47
73
result = unshorten_url (url , verify = False )
74
+ resolved = "https://expired.badssl.com/"
48
75
assert result == {
49
76
"url" : url ,
50
- "resolved" : "https://expired.badssl.com/" ,
77
+ "resolved" : resolved ,
51
78
"status" : 200 ,
52
79
"exception" : None ,
80
+ "request_history" : [url ],
81
+ "response_history" : [200 ],
53
82
}
54
83
55
84
56
85
@pytest .mark .vcr
57
86
def test_resolve_to_mailto ():
58
87
url = "https://tinyurl.com/NewwAlemAndKibrom"
59
88
result = unshorten_url (url )
89
+ resolved = "https://tinyurl.com/NewwAlemAndKibrom"
60
90
assert result ["url" ] == url
61
- assert (
62
- result ["resolved" ]
63
- == "mailto:[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ?subject=URGENT%20CALL%20TO%20ACTION%3A%20Save%20Alem%20and%20Kibrom's%20lives&body=To%20Whom%20it%20May%20Concern%3A%0A%0AAlem%20Tesfay%20Abraham%20and%20Kibrom%20Adhanom%20Okbazghi%20are%20two%20Eritrean%20asylum-seekers%20who%20have%20been%20detained%20without%20charge%20in%20Egypt%20since%202012%20and%202014%2C%20respectively.%20They%20now%20are%20facing%20deportation%20to%20Eritrea%20without%20ever%20receiving%20the%20opportunity%20to%20register%20as%20refugees%20with%20UNHCR%20in%20Egypt.%20On%209%20September%2C%20they%20were%20taken%20from%20prison%20to%20a%20hospital%20in%20Cairo%20to%20take%20PCR%20tests%20and%20were%20informed%20by%20a%20prison%20official%20that%20they%20would%20be%20deported%20to%20Eritrea%20on%20the%20oncoming%20days.%0A%0AForcibly%20returning%20Alem%20and%20Kibrom%20to%20Eritrea%2C%20where%20they%20fled%20indefinite%20military%20conscription%20and%20where%20they%20would%20face%20persecution%2C%20is%20a%20grave%20breach%20of%20international%20law.%20Eritrean%20asylum-seekers%20who%20are%20forcibly%20returned%20to%20Eritrea%20risk%20arbitrary%20arrest%2C%20forced%20disappearance%20and%20indefinite%20detention%20without%20charges.%20As%20widely%20documented%20by%20many%20NGOs%20as%20well%20as%20the%20UN%20Human%20Rights%20Council%2C%20citizens%20in%20Eritrea%20are%20held%20in%20prisons%20incommunicado%2C%20in%20unsanitary%20living%20conditions%2C%20where%20torture%20and%20other%20ill%20treatments%20are%20taking%20place%20to%20present.%0A%0AForcing%20Alem%20and%20Kibrom%20back%20to%20the%20nation%20they%20are%20seeking%20asylum%20from%20violates%20the%201951%20Convention%20and%201967%20Protocol%2C%20two%20International%20Laws%20Egypt%20has%20agreed%20to.%20They%20deserve%20the%20right%20to%20be%20resettled%20by%20will%2C%20to%20a%20country%20willing%20to%20accept%20them.%20We%20urge%20you%2C%20the%20Egyptian%20authorities%2C%20and%20all%20other%20relevant%20bodies%2C%20to%20help%20stop%20the%20forced%20repatriation%20of%20Alem%20and%20Kibrom%20and%20protect%20them%20from%20persecution%20and%20grant%20them%20their%20long-awaited%20freedom.%20%0A%0A%23JusticeforAlemAndKibrom%0A%0ASincerely%2C"
64
- )
65
- assert result ["status" ] is None
66
- assert (
67
- result ["exception" ]
68
- == 'InvalidSchema: No connection adapters were found for "mailto:[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ,%[email protected] ?subject=URGENT%20CALL%20TO%20ACTION%3A%20Save%20Alem%20and%20Kibrom\' s%20lives&body=To%20Whom%20it%20May%20Concern%3A%0A%0AAlem%20Tesfay%20Abraham%20and%20Kibrom%20Adhanom%20Okbazghi%20are%20two%20Eritrean%20asylum-seekers%20who%20have%20been%20detained%20without%20charge%20in%20Egypt%20since%202012%20and%202014%2C%20respectively.%20They%20now%20are%20facing%20deportation%20to%20Eritrea%20without%20ever%20receiving%20the%20opportunity%20to%20register%20as%20refugees%20with%20UNHCR%20in%20Egypt.%20On%209%20September%2C%20they%20were%20taken%20from%20prison%20to%20a%20hospital%20in%20Cairo%20to%20take%20PCR%20tests%20and%20were%20informed%20by%20a%20prison%20official%20that%20they%20would%20be%20deported%20to%20Eritrea%20on%20the%20oncoming%20days.%0A%0AForcibly%20returning%20Alem%20and%20Kibrom%20to%20Eritrea%2C%20where%20they%20fled%20indefinite%20military%20conscription%20and%20where%20they%20would%20face%20persecution%2C%20is%20a%20grave%20breach%20of%20international%20law.%20Eritrean%20asylum-seekers%20who%20are%20forcibly%20returned%20to%20Eritrea%20risk%20arbitrary%20arrest%2C%20forced%20disappearance%20and%20indefinite%20detention%20without%20charges.%20As%20widely%20documented%20by%20many%20NGOs%20as%20well%20as%20the%20UN%20Human%20Rights%20Council%2C%20citizens%20in%20Eritrea%20are%20held%20in%20prisons%20incommunicado%2C%20in%20unsanitary%20living%20conditions%2C%20where%20torture%20and%20other%20ill%20treatments%20are%20taking%20place%20to%20present.%0A%0AForcing%20Alem%20and%20Kibrom%20back%20to%20the%20nation%20they%20are%20seeking%20asylum%20from%20violates%20the%201951%20Convention%20and%201967%20Protocol%2C%20two%20International%20Laws%20Egypt%20has%20agreed%20to.%20They%20deserve%20the%20right%20to%20be%20resettled%20by%20will%2C%20to%20a%20country%20willing%20to%20accept%20them.%20We%20urge%20you%2C%20the%20Egyptian%20authorities%2C%20and%20all%20other%20relevant%20bodies%2C%20to%20help%20stop%20the%20forced%20repatriation%20of%20Alem%20and%20Kibrom%20and%20protect%20them%20from%20persecution%20and%20grant%20them%20their%20long-awaited%20freedom.%20%0A%0A%23JusticeforAlemAndKibrom%0A%0ASincerely%2C"'
69
- )
91
+ assert result ["resolved" ] == resolved
92
+ assert result ["status" ] == 301
93
+ assert result ["exception" ].startswith ("InvalidSchema: No connection adap" )
94
+ assert result ["request_history" ][0 ] == url
95
+ assert result [
"request_history" ][
1 ].
startswith (
"mailto:[email protected] " )
96
+ assert result ["response_history" ] == [301 ]
70
97
71
98
72
- def test_missing_schema ():
73
- url = "I AM NOT AN URL"
74
- with pytest .raises (requests .exceptions .MissingSchema ):
75
- unshorten_url (url )
99
+ @pytest .mark .vcr
100
+ def test_invalid_url_in_redirect_chain ():
101
+ """What should this actually do?
102
+
103
+ Throw error like if it was an invalid URL to begin with?
104
+
105
+ Or should it return the last valid URL in the redirect chain?
106
+
107
+ I think the last URL in the chain..
108
+
109
+ """
110
+ url = "https://ctt.ec/5kum7+"
111
+ result = unshorten_url (url )
112
+ resolved = "https://clicktotweet.com/5kum7+"
113
+ assert result == {
114
+ "url" : url ,
115
+ "resolved" : resolved ,
116
+ "status" : 302 ,
117
+ "exception" : "InvalidURL: No host specified." ,
118
+ "request_history" : [url , resolved , "http://" ],
119
+ "response_history" : [301 , 302 ],
120
+ }
76
121
77
122
78
123
# def test_expired_certificate_verify():
0 commit comments