Skip to content

Commit 88280b0

Browse files
author
Bob Corsaro
committed
Adds freshen tests
1 parent a6e03eb commit 88280b0

File tree

3 files changed

+137
-7
lines changed

3 files changed

+137
-7
lines changed

embedly/client.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
class Embedly(object):
1818
"""
1919
Client
20-
20+
2121
"""
22-
def __init__(self, user_agent=USER_AGENT, domain='api.embed.ly', key=None):
22+
def __init__(self, user_agent=USER_AGENT, domain=None, key=None):
2323
"""
2424
Initialize the Embedly client
25-
25+
2626
:param user_agent: User Agent passed to Embedly
2727
:type user_agent: str
2828
:param domain: Domain you want the client to use '(api|pro).embed.ly'
@@ -33,13 +33,19 @@ def __init__(self, user_agent=USER_AGENT, domain='api.embed.ly', key=None):
3333
:returns: None
3434
:raises: ValueError
3535
"""
36+
if not domain:
37+
if key:
38+
domain = 'pro.embed.ly'
39+
else:
40+
domain = 'api.embed.ly'
41+
3642
if not domain_re.match(domain):
3743
raise ValueError(
3844
'Invalid Domain: %s. api.embed.ly or pro.embed.ly' % domain)
39-
45+
4046
if domain == 'pro.embed.ly' and key is None:
4147
raise ValueError('domain: pro.embed.ly requires a key.')
42-
48+
4349
self.user_agent = user_agent
4450
self.domain = domain
4551
self.key = key
@@ -71,7 +77,8 @@ def _get(self, version, method, url_or_urls, **kwargs):
7177
if resp['status'] == '200':
7278
data = json.loads(content)
7379
else:
74-
data = {'error' : True,
80+
data = {'type' : 'error',
81+
'error' : True,
7582
'error_code' : int(resp['status'])}
7683

7784
print data
@@ -98,4 +105,4 @@ def objectify(self, url_or_urls, **kwargs):
98105
"""
99106
oembed
100107
"""
101-
return self._get(2, 'objectify', url_or_urls, **kwargs)
108+
return self._get(2, 'objectify', url_or_urls, **kwargs)

features/oembed.feature

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Feature: OEmbed
2+
3+
As an embedly user
4+
I want to call the the embedly api
5+
Because I want and oembed for a specific url
6+
7+
Scenario Outline: Get the provider_url
8+
Given an embedly endpoint
9+
When oembed is called with the <url> URL
10+
Then the provider_url should be <provider_url>
11+
12+
Examples:
13+
| url | provider_url |
14+
| http://www.scribd.com/doc/13994900/Easter | http://www.scribd.com/ |
15+
| http://www.scribd.com/doc/28452730/Easter-Cards | http://www.scribd.com/ |
16+
| http://www.youtube.com/watch?v=Zk7dDekYej0 | http://www.youtube.com/ |
17+
| http://tweetphoto.com/14784358 | http://plixi.com |
18+
19+
20+
Scenario Outline: Get the provider_url with force flag
21+
Given an embedly endpoint
22+
When oembed is called with the <url> URL and force flag
23+
Then the provider_url should be <provider_url>
24+
25+
Examples:
26+
| url | provider_url |
27+
| http://www.youtube.com/watch?v=Zk7dDekYej0 | http://www.youtube.com/ |
28+
29+
30+
Scenario Outline: Get multiple provider_urls
31+
Given an embedly endpoint
32+
When oembed is called with the <urls> URLs
33+
Then provider_url should be <provider_urls>
34+
35+
Examples:
36+
| urls | provider_urls |
37+
| http://www.scribd.com/doc/13994900/Easter,http://www.scribd.com/doc/28452730/Easter-Cards | http://www.scribd.com/,http://www.scribd.com/ |
38+
| http://www.youtube.com/watch?v=Zk7dDekYej0,http://plixi.com/p/16044847 | http://www.youtube.com/,http://plixi.com |
39+
40+
41+
Scenario Outline: Get the provider_url with pro
42+
Given an embedly endpoint with key
43+
When oembed is called with the <url> URL
44+
Then the provider_url should be <provider_url>
45+
46+
Examples:
47+
| url | provider_url |
48+
| http://blog.embed.ly/bob | http://posterous.com |
49+
| http://blog.doki-pen.org/cassandra-rules | http://posterous.com |
50+
| http://www.guardian.co.uk/media/2011/jan/21/andy-coulson-phone-hacking-statement | http://www.guardian.co.uk/ |
51+
52+
53+
Scenario Outline: Attempt to get 404 URL
54+
Given an embedly endpoint
55+
When oembed is called with the <url> URL
56+
Then type should be error
57+
And error_code should be 404
58+
And type should be error
59+
60+
Examples:
61+
| url |
62+
| http://www.youtube.com/this/is/a/bad/url |
63+
| http://blog.embed.ly/lsbsdlfldsf/asdfkljlas/klajsdlfkasdf |
64+
| http://tweetphoto.com/alsdfldsf/asdfkljlas/klajsdlfkasdf |
65+
66+
67+
Scenario Outline: Attempt multi get 404 URLs
68+
Given an embedly endpoint
69+
When oembed is called with the <urls> URLs
70+
Then error_code should be <errcode>
71+
And type should be <types>
72+
73+
Examples:
74+
| urls | errcode | types |
75+
| http://www.youtube.com/this/is/a/bad/url,http://blog.embed.ly/alsd/slsdlf/asdlfj | 404,404 | error,error |
76+
| http://blog.embed.ly/lsbsdlfldsf/asdf/kl,http://tweetphoto.com/asdf/asdf/asdfl | 404,404 | error,error |
77+
| http://blog.embed.ly/lsbsdlfldsf/asdf/kl,http://tweetphoto.com/14784358 | 404,None | error,photo |
78+
| http://tweetphoto.com/14784358,http://www.scribd.com/asdf/asdf/asdfasdf | None,404 | photo,error |
79+

features/steps.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from os import environ
2+
3+
from freshen import *
4+
5+
from embedly.client import Embedly
6+
7+
@Given("an embedly endpoint( [^\s]+)?( with key)?$")
8+
def init_api(domain, key_enabled):
9+
opts = {}
10+
11+
if domain:
12+
opts['domain'] = domain
13+
14+
if key_enabled:
15+
if not environ.get('EMBEDLY_KEY'):
16+
raise RuntimeError('Please set env variable $EMBEDLY_KEY')
17+
opts['key'] = environ["EMBEDLY_KEY"]
18+
19+
scc.api = Embedly(**opts)
20+
21+
@When("(\w+) is called with the (.*) URLs?( and ([^\s]+) flag)?$")
22+
def call_urls(method, urls, _, flag):
23+
urls = urls.split(',')
24+
opts = {}
25+
if len(urls) == 1:
26+
opts['url_or_urls'] = urls[0]
27+
else:
28+
opts['url_or_urls'] = urls
29+
30+
if flag:
31+
opts[flag] = 'true'
32+
33+
scc.result = getattr(scc.api, method)(**opts)
34+
35+
@Then("(the )?([^\s]+) should be ([^\s]+)")
36+
def check_value(_, key, value):
37+
if type(scc.result) == list:
38+
r = []
39+
for o in scc.result:
40+
r.append(str(o.get(key)))
41+
assert_equal(','.join(r), value)
42+
43+
else:
44+
assert_equal(str(scc.result.get(key)), value)

0 commit comments

Comments
 (0)