Skip to content

Commit

Permalink
Merge pull request #168 from sdp-tech/reformer-api
Browse files Browse the repository at this point in the history
Reformer api 관련 테스트코드 #158
  • Loading branch information
Scanf-s authored Nov 30, 2024
2 parents 5ec31a1 + fa41c0b commit dcc3607
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 98 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 클라우드 아키텍쳐



![image](https://github.com/user-attachments/assets/910f79f5-80f7-4420-9897-088adaacec95)

# 프로젝트 아키텍쳐
Expand Down
1 change: 1 addition & 0 deletions market/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def test_update_market_info(self):
self.assertEqual(market.market_introduce, update_data["market_introduce"])

@patch("market.views.market_view.market_crud_view.client")

def test_delete_market_info(self, mock_boto3_client: MagicMock):
# 마켓 삭제 시도 테스트

Expand Down
245 changes: 154 additions & 91 deletions users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_user_login(self):

self.assertEqual(response.status_code, 200)
self.assertIn("access", response.data)

self.assertIn("refresh", response.data)

def test_user_logout(self):
Expand Down Expand Up @@ -188,111 +189,173 @@ class ReformerTestCase(APITestCase):
def setUp(self):
self.client = APIClient()

# Reformer 관련 API 처리는 반드시 Access token이 필요하므로 로그인 먼저 수행
# 테스트 사용자 생성
self.test_user = User.objects.create_user(
email="[email protected]",
password="123123",
phone="01012341234",
full_name="hello",
full_name="Test User",
nickname="nickname",
introduce="hello, django",
)
self.login_request_data = {"email": "[email protected]", "password": "123123"}

def test_reformer_create(self):
# 리포머 생성에 사용할 데이터
self.reformer_data = {
"reformer_link": "https://test.com",
"reformer_area": "seoul",
"education": [
{"school": "Test School", "major": "Design", "academic_status": "Graduated"},
],
"certification": [
{"name": "Certification 1", "issuing_authority": "Authority 1"},
],
"awards": [
{"competition": "Competition 1", "prize": "First Prize"},
],
"career": [
{"company_name": "Company 1", "department": "IT", "period": "2 years"},
],
"freelancer": [
{"project_name": "Project 1", "description": "Freelance work description"},
],
}

# 1. 일반 로그인 상태
response = self.client.post(
path="/api/user/login", data=self.login_request_data, format="json"
)
# 로그인 및 인증 헤더 설정
self.login_and_set_token()

def login_and_set_token(self):
# 로그인 수행 및 인증 헤더 설정
login_data = {"email": "[email protected]", "password": "123123"}
response = self.client.post(path="/api/user/login", data=login_data, format="json")
self.assertEqual(response.status_code, 200, "로그인 실패")
self.access_token = response.data["access"]
self.refresh_token = response.data["refresh"]
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + self.access_token)

# 2. 요청 전, 사용자의 권한이 customer 레벨이고, 요청 후 사용자의 권한이 Reformer 레벨로 변경되는지 확인
user = User.objects.get_user_by_email(
self.login_request_data.get("email")
).first()
self.assertEqual(user.role, "customer")

response = self.client.post(
path="/api/user/reformer",
data={
"reformer_link": "https://www.naver.com",
"reformer_area": "seoul yongsan",
"education": [
{
"school": "Test school",
"major": "Fashion Design",
"academic_status": "Graduated",
},
{
"school": "Another University",
"major": "Art",
"academic_status": "Graduated",
},
],
"certification": [
{"name": "test cert", "issuing_authority": "asdfasdfasdf"},
{"name": "test cert 2", "issuing_authority": "gjdofisjgfdg"},
],
"awards": [
{"competition": "어쩌구저쩌구대회", "prize": "멍때리기대회1등"},
{"competition": "어쩌구저쩌구대회2", "prize": "웃음참기대회1등"},
],
"career": [
{
"company_name": "sdiojfgoijfsgf",
"department": "디자인",
"period": "3년",
},
{
"company_name": "pjgfhjfghjgoijfsgfthgdrthgdrth",
"department": "디자인",
"period": "6개월",
},
],
"freelancer": [
{
"project_name": "oubj89todjirng",
"description": "이런저런 일을 했습니다",
},
{
"project_name": "ㅁㄴㅇㄻㄴㅇㄻㄴㅇㄹ",
"description": "이런저런 일을 했습니다 2",
},
],
},
format="json",
)
def test_reformer_create(self):
before_user = User.objects.get(email="[email protected]")
self.assertEqual(before_user.role, "customer")
# 리포머 생성 테스트, 리포머를 생성하는 과정에서 사용자의 역할이 "customer"에서 "reformer"로 변경되는지 확인
response = self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")
self.assertEqual(response.status_code, 201)
user = User.objects.get_user_by_email(
self.login_request_data.get("email")
).first()
self.assertEqual(
user.role, "reformer"
) # 사용자의 권한이 reformer로 바뀌었는지 확인

# 3. Reformer 객체와 관련 객체가 생성되었는지 확인
reformer = Reformer.objects.filter(user=user)
self.assertEqual(reformer.count(), 1)
self.assertEqual(reformer.first().reformer_education.count(), 2)
self.assertEqual(reformer.first().reformer_certification.count(), 2)
self.assertEqual(reformer.first().reformer_awards.count(), 2)
self.assertEqual(reformer.first().reformer_career.count(), 2)
self.assertEqual(reformer.first().reformer_freelancer.count(), 2)

def test_reformer_get_list(self):
# 1. 로그인 상태
response = self.client.post(
path="/api/user/login", data=self.login_request_data, format="json"
)
self.access_token = response.data["access"]
self.refresh_token = response.data["refresh"]
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + self.access_token)
self.assertEqual(response.data["message"], "successfully created")

after_user = User.objects.get(email="[email protected]")
self.assertEqual(after_user.role, "reformer")

# 데이터 검증
reformer = Reformer.objects.filter(user=self.test_user).first()
self.assertIsNotNone(reformer)
self.assertEqual(reformer.reformer_area, self.reformer_data["reformer_area"])

def test_reformer_duplicate_creation(self):
# 첫 번째 생성
self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")

# 중복 생성 시도를 하면 에러가 발생함
response = self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")
self.assertEqual(response.status_code, 400)
self.assertIn("message", response.data)

def test_reformer_create_missing_fields(self):
# 필요한 필드가 누락된 경우 에러가 발생함
incomplete_data = {
"reformer_link": "https://test.com", # 'reformer_area' 필드 누락
# "reformer_area": "seoul",
}
response = self.client.post(path="/api/user/reformer", data=incomplete_data, format="json")
self.assertEqual(response.status_code, 400)
self.assertIn("message", response.data) # 에러 메시지 확인

def test_reformer_get(self):
# 리포머 생성
self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")

# 리포머 데이터 조회
response = self.client.get(path="/api/user/reformer", format="json")
self.assertEqual(response.status_code, 200)

# 데이터 검증
response_data = response.data
self.assertEqual(response_data["reformer_link"], self.reformer_data["reformer_link"])
self.assertEqual(response_data["reformer_area"], self.reformer_data["reformer_area"])

def test_reformer_update(self):
pass
# 리포머 생성
self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")

# 리포머 데이터 업데이트
update_data = {
"reformer_link": "https://updated-link.com",
"reformer_area": "seoul gangnam",
}
response = self.client.put(path="/api/user/reformer", data=update_data, format="json")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["message"], "successfully updated")

# 업데이트된 데이터 검증
reformer = Reformer.objects.get(user=self.test_user)
self.assertEqual(reformer.reformer_link, update_data["reformer_link"])
self.assertEqual(reformer.reformer_area, update_data["reformer_area"])

def test_reformer_update_without_permissions(self):
# 리포머 역할이 아닌 사용자가 리포머 정보를 업데이트 하려고 할 때 403 오류 발생
non_reformer_user = User.objects.create_user(
email="[email protected]", password="123123"
)
self.client.credentials(HTTP_AUTHORIZATION="Bearer " + self.get_access_token(non_reformer_user))

update_data = {"reformer_link": "https://new-link.com", "reformer_area": "busan"}
response = self.client.put(path="/api/user/reformer", data=update_data, format="json")

self.assertEqual(response.status_code, 403)
self.assertIn("detail", response.data) # 권한 오류 메시지 확인

def test_reformer_delete(self):
pass
# 리포머 생성
self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")

# 리포머 삭제
response = self.client.delete(path="/api/user/reformer", format="json")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["message"], "successfully deleted")

# 데이터 삭제 확인
reformer_count = Reformer.objects.filter(user=self.test_user).count()
self.assertEqual(reformer_count, 0)

def test_reformer_delete_nonexistent(self):
# 존재하지 않는 리포머 삭제를 시도하면 에러
response = self.client.delete(path="/api/user/reformer", format="json")
self.assertEqual(response.status_code, 404)
self.assertIn("message", response.data)

def test_reformer_delete_after_update(self):
# 리포머 생성
self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")

# 리포머 삭제
self.client.delete(path="/api/user/reformer", format="json")

# 삭제된 리포머가 데이터베이스에서 완전히 삭제되었는지 확인
reformer_count = Reformer.objects.filter(user=self.test_user).count()
self.assertEqual(reformer_count, 0)

# 리포머 재생성 시도
response = self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")
self.assertEqual(response.status_code, 201)

# 다시 생성된 리포머가 존재하는지 확인
reformer = Reformer.objects.filter(user=self.test_user).first()
self.assertIsNotNone(reformer)
self.assertEqual(reformer.reformer_link, self.reformer_data["reformer_link"])

def test_reformer_role_check(self):
# 리포머 생성 후 사용자 역할이 reformer가 되는 것을 다시 확인
self.client.post(path="/api/user/reformer", data=self.reformer_data, format="json")
user = User.objects.get(email="[email protected]")
self.assertEqual(user.role, "reformer")

def get_access_token(self, user: User):
login_data = {"email": user.email, "password": "123123"}
response = self.client.post(path="/api/user/login", data=login_data, format="json")
return response.data["access"]
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ def delete(self, request, **kwargs):
raise ReformerEducation.DoesNotExist

with transaction.atomic():
s3 = client("s3")
s3.delete_object(
Bucket=os.getenv("AWS_STORAGE_BUCKET_NAME"),
Key=reformer_education.proof_document.name,
)
if (
reformer_education.proof_document
): # 증명 서류가 존재한다면, 삭제
s3 = client("s3")
s3.delete_object(
Bucket=os.getenv("AWS_STORAGE_BUCKET_NAME"),
Key=reformer_education.proof_document.name,
)

reformer_education.delete()
return Response(
data={"message": "successfully deleted"}, status=status.HTTP_200_OK
Expand All @@ -99,7 +103,7 @@ def delete(self, request, **kwargs):
except ReformerEducation.DoesNotExist:
return Response(
data={
"message": "해당 UUID에 해당하는 리포머 학력 정보가 존재하지 않습니다."
"message": "해당 UUID에 해당하는 리포머 학력 내역 정보가 존재하지 않습니다."
},
status=status.HTTP_404_NOT_FOUND,
)
)

0 comments on commit dcc3607

Please sign in to comment.