<s> import os <EOL> from django . contrib . auth . models import User <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import ValidationError <EOL> from django . db import IntegrityError <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from friendship . exceptions import AlreadyExistsError , AlreadyFriendsError <EOL> from friendship . models import Friend , Follow , FriendshipRequest <EOL> TEST_TEMPLATES = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> class login ( object ) : <EOL> def __init__ ( self , testcase , user , password ) : <EOL> self . testcase = testcase <EOL> success = testcase . client . login ( username = user , password = password ) <EOL> self . testcase . assertTrue ( <EOL> success , <EOL> "<STR_LIT>" % ( user , password ) <EOL> ) <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , * args ) : <EOL> self . testcase . client . logout ( ) <EOL> class BaseTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . user_pw = '<STR_LIT:test>' <EOL> self . user_bob = self . create_user ( '<STR_LIT>' , '<STR_LIT>' , self . user_pw ) <EOL> self . user_steve = self . create_user ( '<STR_LIT>' , '<STR_LIT>' , self . user_pw ) <EOL> self . user_susan = self . create_user ( '<STR_LIT>' , '<STR_LIT>' , self . user_pw ) <EOL> self . user_amy = self . create_user ( '<STR_LIT>' , '<STR_LIT>' , self . user_pw ) <EOL> cache . clear ( ) <EOL> def tearDown ( self ) : <EOL> cache . clear ( ) <EOL> self . client . logout ( ) <EOL> def login ( self , user , password ) : <EOL> return login ( self , user , password ) <EOL> def create_user ( self , username , password , email_address ) : <EOL> user = User . objects . create_user ( username , password , email_address ) <EOL> return user <EOL> def assertResponse200 ( self , response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def assertResponse302 ( self , response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def assertResponse403 ( self , response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def assertResponse404 ( self , response ) : <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> class FriendshipModelTests ( BaseTestCase ) : <EOL> def test_friendship_request ( self ) : <EOL> req1 = Friend . objects . add_friend ( self . user_bob , self . user_steve ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_bob ) , [ ] ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_steve ) , [ ] ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( from_user = self . user_bob ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( to_user = self . user_steve ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Friend . objects . unread_request_count ( self . user_steve ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( Friend . objects . requests ( self . user_bob ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( Friend . objects . requests ( self . user_steve ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( Friend . objects . sent_requests ( self . user_bob ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( Friend . objects . sent_requests ( self . user_steve ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( Friend . objects . unread_requests ( self . user_steve ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Friend . objects . unread_request_count ( self . user_steve ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( Friend . objects . rejected_requests ( self . user_steve ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( Friend . objects . unrejected_requests ( self . user_steve ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Friend . objects . unrejected_request_count ( self . user_steve ) , <NUM_LIT:1> ) <EOL> self . assertFalse ( Friend . objects . are_friends ( self . user_bob , self . user_steve ) ) <EOL> req1 . accept ( ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( from_user = self . user_bob ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( to_user = self . user_steve ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_bob ) , [ self . user_steve ] ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_steve ) , [ self . user_bob ] ) <EOL> self . assertTrue ( Friend . objects . are_friends ( self . user_bob , self . user_steve ) ) <EOL> self . assertTrue ( Friend . objects . remove_friend ( self . user_bob , self . user_steve ) ) <EOL> self . assertFalse ( Friend . objects . are_friends ( self . user_bob , self . user_steve ) ) <EOL> self . assertFalse ( Friend . objects . remove_friend ( self . user_bob , self . user_steve ) ) <EOL> req2 = Friend . objects . add_friend ( self . user_susan , self . user_amy ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_susan ) , [ ] ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_amy ) , [ ] ) <EOL> req2 . cancel ( ) <EOL> self . assertEqual ( Friend . objects . requests ( self . user_susan ) , [ ] ) <EOL> self . assertEqual ( Friend . objects . requests ( self . user_amy ) , [ ] ) <EOL> req3 = Friend . objects . add_friend ( self . user_susan , self . user_amy ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_susan ) , [ ] ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_amy ) , [ ] ) <EOL> req3 . reject ( ) <EOL> with self . assertRaises ( AlreadyExistsError ) : <EOL> Friend . objects . add_friend ( self . user_susan , self . user_amy ) <EOL> self . assertFalse ( Friend . objects . are_friends ( self . user_susan , self . user_amy ) ) <EOL> self . assertEqual ( len ( Friend . objects . rejected_requests ( self . user_amy ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( Friend . objects . rejected_requests ( self . user_amy ) ) , <NUM_LIT:1> ) <EOL> req3 . delete ( ) <EOL> req4 = Friend . objects . add_friend ( self . user_susan , self . user_amy ) <EOL> req4 . mark_viewed ( ) <EOL> self . assertFalse ( Friend . objects . are_friends ( self . user_susan , self . user_amy ) ) <EOL> self . assertEqual ( len ( Friend . objects . read_requests ( self . user_amy ) ) , <NUM_LIT:1> ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> Friend . objects . add_friend ( self . user_bob , self . user_bob ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> Friend . objects . create ( to_user = self . user_bob , from_user = self . user_bob ) <EOL> def test_already_friends_with_request ( self ) : <EOL> req = Friend . objects . add_friend ( self . user_bob , self . user_steve ) <EOL> req . accept ( ) <EOL> with self . assertRaises ( AlreadyFriendsError ) : <EOL> req2 = Friend . objects . add_friend ( self . user_bob , self . user_steve ) <EOL> def test_multiple_friendship_requests ( self ) : <EOL> """<STR_LIT>""" <EOL> req1 = Friend . objects . add_friend ( self . user_bob , self . user_steve ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_bob ) , [ ] ) <EOL> self . assertEqual ( Friend . objects . friends ( self . user_steve ) , [ ] ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( from_user = self . user_bob ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( to_user = self . user_steve ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Friend . objects . unread_request_count ( self . user_steve ) , <NUM_LIT:1> ) <EOL> req2 = Friend . objects . add_friend ( self . user_steve , self . user_bob ) <EOL> self . assertFalse ( Friend . objects . are_friends ( self . user_bob , self . user_steve ) ) <EOL> req1 . accept ( ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( from_user = self . user_bob ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( to_user = self . user_steve ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( from_user = self . user_steve ) . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( FriendshipRequest . objects . filter ( to_user = self . user_bob ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_multiple_calls_add_friend ( self ) : <EOL> """<STR_LIT>""" <EOL> req1 = Friend . objects . add_friend ( self . user_bob , self . user_steve , message = '<STR_LIT>' ) <EOL> with self . assertRaises ( AlreadyExistsError ) : <EOL> req2 = Friend . objects . add_friend ( self . user_bob , self . user_steve , message = '<STR_LIT>' ) <EOL> def test_following ( self ) : <EOL> req1 = Follow . objects . add_follower ( self . user_bob , self . user_steve ) <EOL> self . assertEqual ( len ( Follow . objects . followers ( self . user_steve ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( Follow . objects . following ( self . user_bob ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Follow . objects . followers ( self . user_steve ) , [ self . user_bob ] ) <EOL> self . assertEqual ( Follow . objects . following ( self . user_bob ) , [ self . user_steve ] ) <EOL> self . assertTrue ( Follow . objects . follows ( self . user_bob , self . user_steve ) ) <EOL> self . assertFalse ( Follow . objects . follows ( self . user_steve , self . user_bob ) ) <EOL> with self . assertRaises ( IntegrityError ) : <EOL> Follow . objects . add_follower ( self . user_bob , self . user_steve ) <EOL> with self . assertRaises ( AlreadyExistsError ) : <EOL> Follow . objects . add_follower ( self . user_bob , self . user_steve ) <EOL> self . assertTrue ( Follow . objects . remove_follower ( self . user_bob , self . user_steve ) ) <EOL> self . assertEqual ( len ( Follow . objects . followers ( self . user_steve ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( Follow . objects . following ( self . user_bob ) ) , <NUM_LIT:0> ) <EOL> self . assertFalse ( Follow . objects . follows ( self . user_bob , self . user_steve ) ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> Follow . objects . add_follower ( self . user_bob , self . user_bob ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> Follow . objects . create ( follower = self . user_bob , followee = self . user_bob ) <EOL> class FriendshipViewTests ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( FriendshipViewTests , self ) . setUp ( ) <EOL> self . friendship_request = Friend . objects . add_friend ( self . user_steve , self . user_bob ) <EOL> def test_friendship_view_users ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> with self . settings ( FRIENDSHIP_CONTEXT_OBJECT_LIST_NAME = '<STR_LIT>' , TEMPLATE_DIRS = ( TEST_TEMPLATES , ) ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT>' in response . context ) <EOL> def test_friendship_view_friends ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : self . user_bob . username } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT:user>' in response . context ) <EOL> with self . settings ( FRIENDSHIP_CONTEXT_OBJECT_NAME = '<STR_LIT:object>' , TEMPLATE_DIRS = ( TEST_TEMPLATES , ) ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT:object>' in response . context ) <EOL> def test_friendship_add_friend ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_amy . username } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> def test_friendship_add_friend_dupe ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_amy . username } ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT>' in response . context ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def test_friendship_requests ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> def test_friendship_requests_rejected ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> def test_friendship_accept ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : self . user_bob . username } ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> with self . login ( self . user_steve . username , self . user_pw ) : <EOL> response = self . client . post ( url ) <EOL> self . assertResponse404 ( response ) <EOL> def test_friendship_reject ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> with self . login ( self . user_steve . username , self . user_pw ) : <EOL> response = self . client . post ( url ) <EOL> self . assertResponse404 ( response ) <EOL> def test_friendship_cancel ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse404 ( response ) <EOL> with self . login ( self . user_steve . username , self . user_pw ) : <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> def test_friendship_requests_detail ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . friendship_request . pk } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> def test_friendship_followers ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> with self . settings ( FRIENDSHIP_CONTEXT_OBJECT_NAME = '<STR_LIT:object>' , TEMPLATE_DIRS = ( TEST_TEMPLATES , ) ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT:object>' in response . context ) <EOL> def test_friendship_following ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> with self . settings ( FRIENDSHIP_CONTEXT_OBJECT_NAME = '<STR_LIT:object>' , TEMPLATE_DIRS = ( TEST_TEMPLATES , ) ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT:object>' in response . context ) <EOL> def test_follower_add ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_amy . username } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : self . user_bob . username } ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse200 ( response ) <EOL> self . assertTrue ( '<STR_LIT>' in response . context ) <EOL> self . assertEqual ( response . context [ '<STR_LIT>' ] , [ "<STR_LIT>" ] ) <EOL> def test_follower_remove ( self ) : <EOL> follow = Follow . objects . add_follower ( self . user_bob , self . user_amy ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . user_amy . username } ) <EOL> response = self . client . get ( url ) <EOL> self . assertResponse302 ( response ) <EOL> with self . login ( self . user_bob . username , self . user_pw ) : <EOL> response = self . client . get ( url ) <EOL> self . assertResponse200 ( response ) <EOL> response = self . client . post ( url ) <EOL> self . assertResponse302 ( response ) <EOL> redirect_url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : self . user_bob . username } ) <EOL> self . assertTrue ( redirect_url in response [ '<STR_LIT>' ] ) </s>
