<s> import logging <EOL> import hashlib <EOL> import json <EOL> from base64 import b64encode <EOL> from django . conf import settings <EOL> from django . http import HttpRequest <EOL> from django . test . client import ( Client , FakePayload , encode_multipart , <EOL> BOUNDARY , CONTENT_TYPE_RE , MULTIPART_CONTENT ) <EOL> from django . contrib . auth . models import User <EOL> from nose . tools import assert_equal , with_setup , assert_false , eq_ , ok_ <EOL> from nose . plugins . attrib import attr <EOL> from django . template . defaultfilters import slugify <EOL> try : <EOL> from funfactory . urlresolvers import ( get_url_prefix , Prefixer , reverse , <EOL> set_url_prefix ) <EOL> from tower import activate <EOL> except ImportError , e : <EOL> from django . core . urlresolvers import reverse <EOL> get_url_prefix = None <EOL> from valet_keys . models import Key <EOL> from badger . tests import BadgerTestCase <EOL> from badger . models import ( Badge , Award , Nomination , Progress , DeferredAward , <EOL> NominationApproveNotAllowedException , <EOL> NominationAcceptNotAllowedException , <EOL> BadgeAwardNotAllowedException ) <EOL> from badger . utils import get_badge , award_badge <EOL> class BadgerApiViewsTests ( BadgerTestCase ) : <EOL> def setUp ( self ) : <EOL> self . client = Client ( ) <EOL> self . testuser = self . _get_user ( ) <EOL> self . badge = Badge ( creator = self . testuser , title = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , unique = True ) <EOL> self . badge . save ( ) <EOL> self . awards_url = reverse ( '<STR_LIT>' , <EOL> args = ( self . badge . slug , ) ) <EOL> key = Key ( ) <EOL> key . user = self . testuser <EOL> self . password = key . generate_secret ( ) <EOL> self . username = key . key <EOL> key . save ( ) <EOL> self . key = key <EOL> auth_ct = '<STR_LIT>' % ( self . username , self . password ) <EOL> self . basic_auth = '<STR_LIT>' % b64encode ( auth_ct ) <EOL> self . headers = { '<STR_LIT>' : self . basic_auth } <EOL> Award . objects . all ( ) . delete ( ) <EOL> def tearDown ( self ) : <EOL> self . key . delete ( ) <EOL> Award . objects . all ( ) . delete ( ) <EOL> Badge . objects . all ( ) . delete ( ) <EOL> def test_forbidden_without_key ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . get ( self . awards_url ) <EOL> ok_ ( <NUM_LIT:200> , resp . status_code ) <EOL> resp = self . client . post ( self . awards_url ) <EOL> ok_ ( <NUM_LIT> , resp . status_code ) <EOL> resp = self . client . post ( self . awards_url , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ok_ ( <NUM_LIT> , resp . status_code ) <EOL> resp = self . client . post ( self . awards_url , self . headers ) <EOL> ok_ ( <NUM_LIT:200> , resp . status_code ) <EOL> resp = self . client . get ( self . awards_url , self . headers ) <EOL> ok_ ( <NUM_LIT:200> , resp . status_code ) <EOL> def test_bad_data ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . post ( self . awards_url , "<STR_LIT>" , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = self . basic_auth ) <EOL> ok_ ( <NUM_LIT> , resp . status_code ) <EOL> def test_badge_award ( self ) : <EOL> """<STR_LIT>""" <EOL> invite_email = '<STR_LIT>' <EOL> invalid_email = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> award_user = self . _get_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" ) <EOL> data = dict ( <EOL> description = description , <EOL> emails = [ <EOL> award_user . email , <EOL> invite_email , <EOL> invalid_email , <EOL> ] , <EOL> ) <EOL> resp = self . client . post ( self . awards_url , json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = self . basic_auth ) <EOL> ok_ ( self . badge . is_awarded_to ( award_user ) ) <EOL> award = Award . objects . get ( user = award_user , badge = self . badge ) <EOL> eq_ ( description , award . description ) <EOL> eq_ ( '<STR_LIT:application/json>' , resp [ '<STR_LIT:Content-Type>' ] ) <EOL> data = json . loads ( resp . content ) <EOL> ok_ ( '<STR_LIT>' in data ) <EOL> ok_ ( award_user . email in data [ '<STR_LIT>' ] ) <EOL> eq_ ( '<STR_LIT>' , data [ '<STR_LIT>' ] [ award_user . email ] ) <EOL> ok_ ( invite_email in data [ '<STR_LIT>' ] ) <EOL> eq_ ( '<STR_LIT>' , data [ '<STR_LIT>' ] [ invite_email ] ) <EOL> ok_ ( '<STR_LIT>' in data ) <EOL> ok_ ( invalid_email in data [ '<STR_LIT>' ] ) <EOL> eq_ ( '<STR_LIT>' , data [ '<STR_LIT>' ] [ invalid_email ] ) <EOL> def test_no_description ( self ) : <EOL> """<STR_LIT>""" <EOL> award_user = self . _get_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" ) <EOL> data = { "<STR_LIT>" : [ award_user . email ] } <EOL> resp = self . client . post ( self . awards_url , json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = self . basic_auth ) <EOL> eq_ ( '<STR_LIT:application/json>' , resp [ '<STR_LIT:Content-Type>' ] ) <EOL> data = json . loads ( resp . content ) <EOL> ok_ ( award_user . email in data [ '<STR_LIT>' ] ) <EOL> ok_ ( award_user . email not in data [ '<STR_LIT>' ] ) <EOL> ok_ ( self . badge . is_awarded_to ( award_user ) ) <EOL> def test_already_awarded ( self ) : <EOL> """<STR_LIT>""" <EOL> description = "<STR_LIT>" <EOL> invite_email = '<STR_LIT>' <EOL> award_user = self . _get_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" ) <EOL> award = self . badge . award_to ( email = award_user . email ) <EOL> deferred_award = self . badge . award_to ( email = invite_email ) <EOL> data = dict ( <EOL> description = description , <EOL> emails = [ <EOL> invite_email , <EOL> award_user . email , <EOL> ] , <EOL> ) <EOL> resp = self . client . post ( self . awards_url , json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = self . basic_auth ) <EOL> eq_ ( '<STR_LIT:application/json>' , resp [ '<STR_LIT:Content-Type>' ] ) <EOL> data = json . loads ( resp . content ) <EOL> print "<STR_LIT>" % data <EOL> ok_ ( '<STR_LIT>' in data ) <EOL> ok_ ( award_user . email in data [ '<STR_LIT>' ] ) <EOL> eq_ ( '<STR_LIT>' , data [ '<STR_LIT>' ] [ award_user . email ] ) <EOL> ok_ ( invite_email in data [ '<STR_LIT>' ] ) <EOL> eq_ ( '<STR_LIT>' , data [ '<STR_LIT>' ] [ invite_email ] ) <EOL> def test_disallowed_badge_award ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . _get_user ( username = "<STR_LIT>" , <EOL> email = "<STR_LIT>" ) <EOL> badge = Badge ( creator = user , title = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , unique = True ) <EOL> badge . save ( ) <EOL> awards_url = reverse ( '<STR_LIT>' , <EOL> args = ( self . badge . slug , ) ) <EOL> data = dict ( emails = [ '<STR_LIT>' , ] ) <EOL> resp = self . client . post ( awards_url , json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_AUTHORIZATION = self . basic_auth ) <EOL> ok_ ( <NUM_LIT> , resp . status_code ) </s>
