@ -9,6 +9,33 @@ def create_test_image():
img_byte_arr . seek ( 0 )
return img_byte_arr
def ensure_friendship ( user_id , user_id_second ) :
""" Crée une amitié entre deux utilisateurs si elle n ' existe pas déjà. """
friend = db [ " friends " ] . find_one ( {
" $or " : [
{ " user_id " : user_id , " friend_user_id " : user_id_second } ,
{ " user_id " : user_id_second , " friend_user_id " : user_id }
]
} )
if not friend :
db [ " friends " ] . insert_one ( {
" user_id " : user_id ,
" friend_user_id " : user_id_second ,
" status " : " accepted "
} )
def remove_friendship ( user_id , user_id_second ) :
""" Supprime l ' amitié entre deux utilisateurs si elle existe. """
friend = db [ " friends " ] . find_one ( {
" $or " : [
{ " user_id " : user_id , " friend_user_id " : user_id_second } ,
{ " user_id " : user_id_second , " friend_user_id " : user_id }
]
} )
if friend :
db [ " friends " ] . delete_one ( { " _id " : friend [ " _id " ] } )
def add_test_image ( token ) :
test_image = create_test_image ( )
response = client . post (
@ -27,6 +54,7 @@ def create_test_pin(token, user_id, image_id=None, with_date=False):
" title " : " Test Pin " ,
" description " : " Test Description " ,
" location " : [ 0 , 0 ] ,
" complete_address " : " Paris, Île-de-France, France métropolitaine, France " ,
" files " : [ image_id ] ,
" user_id " : user_id
}
@ -58,6 +86,7 @@ def test_add_pin_invalid_files_format(token, user_id):
" title " : " Test Pin " ,
" description " : " Test Description " ,
" location " : [ 0 , 0 ] ,
" complete_address " : " Marseille, Bouches-du-Rhône, Provence-Alpes-Côte d ' Azur, France métropolitaine, France " ,
" files " : [ " abc " ] ,
" user_id " : user_id
} ,
@ -118,6 +147,7 @@ def test_update_pin(token, user_id):
" title " : " Updated Pin " ,
" description " : " Updated Description " ,
" location " : [ 1 , 1 ] ,
" complete_address " : " Mont Saint-Michel, Grande Terrasse de l ' Ouest, Le Mont-Saint-Michel, Avranches, Manche, Normandie, France métropolitaine, 50170, France " ,
" files " : [ image_id ] ,
" user_id " : user_id
} ,
@ -133,6 +163,7 @@ def test_update_pin(token, user_id):
assert data [ " title " ] == " Updated Pin "
assert data [ " description " ] == " Updated Description "
assert data [ " location " ] == [ 1 , 1 ]
assert data [ " complete_address " ] == " Mont Saint-Michel, Grande Terrasse de l ' Ouest, Le Mont-Saint-Michel, Avranches, Manche, Normandie, France métropolitaine, 50170, France "
assert image_id in data [ " files " ]
assert data [ " date " ] is None
@ -143,6 +174,7 @@ def test_update_pin(token, user_id):
" title " : " Updated Pin With Date " ,
" description " : " Updated Description " ,
" location " : [ 1 , 1 ] ,
" complete_address " : " Bordeaux, Gironde, Nouvelle-Aquitaine, France métropolitaine, France " ,
" files " : [ image_id ] ,
" user_id " : user_id ,
" date " : " 2024-03-21T12:00:00 "
@ -157,6 +189,7 @@ def test_update_pin(token, user_id):
)
data = get_response . json ( )
assert data [ " title " ] == " Updated Pin With Date "
assert data [ " complete_address " ] == " Bordeaux, Gironde, Nouvelle-Aquitaine, France métropolitaine, France "
assert data [ " date " ] == " 2024-03-21T12:00:00 "
def test_update_wrong_format ( token ) :
@ -181,4 +214,152 @@ def test_delete_pin(token, user_id):
f " /pin/ { pin_id } " ,
headers = { " Authorization " : f " Bearer { token } " }
)
assert get_response . status_code == 404
assert get_response . status_code == 404
def test_share_pin ( token , user_id , token_second , user_id_second ) :
# Créer un pin
image_id = add_test_image ( token )
pin_id = create_test_pin ( token , user_id , image_id )
# S'assurer que les utilisateurs sont amis
ensure_friendship ( user_id , user_id_second )
# Partager le pin
response = client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id_second } ,
headers = { " Authorization " : f " Bearer { token } " }
)
assert response . status_code == 200
# Vérifier que l'autre utilisateur peut voir le pin
response = client . get (
f " /pin/ { pin_id } " ,
headers = { " Authorization " : f " Bearer { token_second } " }
)
assert response . status_code == 200
def test_share_pin_not_owner ( token , user_id , token_second , user_id_second ) :
# Créer un pin avec le deuxième utilisateur
image_id = add_test_image ( token_second )
pin_id = create_test_pin ( token_second , user_id_second , image_id )
# Essayer de partager le pin (ne devrait pas fonctionner car on n'est pas le propriétaire)
response = client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id } ,
headers = { " Authorization " : f " Bearer { token } " }
)
assert response . status_code == 403
assert " You can only share your own pins " in response . json ( ) [ " detail " ]
def test_share_pin_already_shared ( token , user_id , token_second , user_id_second ) :
# Créer un pin
image_id = add_test_image ( token )
pin_id = create_test_pin ( token , user_id , image_id )
# S'assurer que les utilisateurs sont amis
ensure_friendship ( user_id , user_id_second )
# Partager le pin une première fois
response = client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id_second } ,
headers = { " Authorization " : f " Bearer { token } " }
)
assert response . status_code == 200
# Essayer de partager à nouveau (ne devrait pas fonctionner)
response = client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id_second } ,
headers = { " Authorization " : f " Bearer { token } " }
)
assert response . status_code == 409
assert " Pin is already shared with this user " in response . json ( ) [ " detail " ]
def test_update_shared_pin ( token , user_id , token_second , user_id_second ) :
# Créer un pin
image_id = add_test_image ( token )
pin_id = create_test_pin ( token , user_id , image_id )
# S'assurer que les utilisateurs sont amis
ensure_friendship ( user_id , user_id_second )
# Partager le pin
client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id_second } ,
headers = { " Authorization " : f " Bearer { token } " }
)
# L'autre utilisateur essaie de modifier le pin
response = client . patch (
f " /pin/ { pin_id } " ,
json = {
" title " : " Updated by friend " ,
" description " : " Updated Description " ,
" location " : [ 1 , 1 ] ,
" complete_address " : " Marseille, Bouches-du-Rhône, Provence-Alpes-Côte d ' Azur, France métropolitaine, France " ,
" files " : [ image_id ] ,
" user_id " : user_id_second
} ,
headers = { " Authorization " : f " Bearer { token_second } " }
)
assert response . status_code == 200
# Vérifier que les modifications ont été appliquées
response = client . get (
f " /pin/ { pin_id } " ,
headers = { " Authorization " : f " Bearer { token_second } " }
)
data = response . json ( )
assert data [ " title " ] == " Updated by friend "
assert data [ " complete_address " ] == " Marseille, Bouches-du-Rhône, Provence-Alpes-Côte d ' Azur, France métropolitaine, France "
def test_delete_shared_pin_access ( token , user_id , token_second , user_id_second ) :
# Créer un pin
image_id = add_test_image ( token )
pin_id = create_test_pin ( token , user_id , image_id )
# S'assurer que les utilisateurs sont amis
ensure_friendship ( user_id , user_id_second )
# Partager le pin
client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id_second } ,
headers = { " Authorization " : f " Bearer { token } " }
)
# L'autre utilisateur supprime son accès au pin
response = client . delete (
f " /pin/ { pin_id } " ,
headers = { " Authorization " : f " Bearer { token_second } " }
)
assert response . status_code == 200
assert " Pin access removed " in response . json ( ) [ " message " ]
# Vérifier que l'autre utilisateur n'a plus accès au pin
response = client . get (
f " /pin/ { pin_id } " ,
headers = { " Authorization " : f " Bearer { token_second } " }
)
assert response . status_code == 403
def test_share_pin_not_friend ( token , user_id , token_second , user_id_second ) :
# Créer un pin
image_id = add_test_image ( token )
pin_id = create_test_pin ( token , user_id , image_id )
# S'assurer que les utilisateurs ne sont pas amis
remove_friendship ( user_id , user_id_second )
# Essayer de partager avec quelqu'un qui n'est pas ami
response = client . post (
f " /pin/ { pin_id } /share " ,
json = { " friend_id " : user_id_second } ,
headers = { " Authorization " : f " Bearer { token } " }
)
assert response . status_code == 403
assert " You can only share pins with your friends " in response . json ( ) [ " detail " ]