-
Notifications
You must be signed in to change notification settings - Fork 80
Bugfix: Add duplicate organization name check #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NenadPantelic
wants to merge
1
commit into
anitab-org:develop
Choose a base branch
from
NenadPantelic:110-duplicate-organization
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from tests.base_test_case import BaseTestCase | ||
from app.api.request_api_utils import post_request, get_request, BASE_MS_API_URL, AUTH_COOKIE | ||
from app.api.models.organization import get_organization_response_model, update_organization_request_model | ||
from tests.test_data import user1 | ||
from tests.test_data import user1, user2 | ||
from app.database.models.ms_schema.user import UserModel | ||
from app.database.models.bit_schema.user_extension import UserExtensionModel | ||
from app.database.models.bit_schema.organization import OrganizationModel | ||
|
@@ -25,7 +25,7 @@ class TestUpdateOrganizationApi(BaseTestCase): | |
def setUp(self, mock_login, mock_get_user): | ||
super(TestUpdateOrganizationApi, self).setUp() | ||
# set access expiry 4 weeks from today's date (sc*min*hrrs*days) | ||
access_expiry = time.time() + 60*60*24*28 | ||
access_expiry = time.time() + 60 * 60 * 24 * 28 | ||
success_message = {"access_token": "this is fake token", "access_expiry": access_expiry} | ||
success_code = HTTPStatus.OK | ||
|
||
|
@@ -36,41 +36,38 @@ def setUp(self, mock_login, mock_get_user): | |
mock_login.raise_for_status = json.dumps(success_code) | ||
|
||
expected_user = marshal(user1, full_user_api_model) | ||
|
||
mock_get_response = Mock() | ||
mock_get_response.json.return_value = expected_user | ||
mock_get_response.status_code = success_code | ||
|
||
mock_get_user.return_value = mock_get_response | ||
mock_get_user.raise_for_status = json.dumps(success_code) | ||
|
||
user_login_success = { | ||
"username": user1.get("username"), | ||
"password": user1.get("password") | ||
} | ||
|
||
with self.client: | ||
login_response = self.client.post( | ||
"/login", | ||
data=json.dumps(user_login_success), | ||
follow_redirects=True, | ||
content_type="application/json", | ||
) | ||
|
||
test_user1 = UserModel( | ||
name=user1["name"], | ||
username=user1["username"], | ||
password=user1["password"], | ||
email=user1["email"], | ||
password=user1["password"], | ||
email=user1["email"], | ||
terms_and_conditions_checked=user1["terms_and_conditions_checked"] | ||
) | ||
test_user1.need_mentoring = user1["need_mentoring"] | ||
test_user1.available_to_mentor = user1["available_to_mentor"] | ||
|
||
test_user1.save_to_db() | ||
self.test_user1_data = UserModel.find_by_email(test_user1.email) | ||
AUTH_COOKIE["user"] = marshal(self.test_user1_data, full_user_api_model) | ||
|
||
self.correct_payload_organization = { | ||
"representative_department": "H&R Department", | ||
"name": "Company ABC", | ||
|
@@ -83,31 +80,77 @@ def setUp(self, mock_login, mock_get_user): | |
"status": "Draft", | ||
} | ||
|
||
|
||
def test_api_dao_create_organization_successfully(self): | ||
test_user_extension = UserExtensionModel( | ||
user_id=self.test_user1_data.id, | ||
timezone="AUSTRALIA_MELBOURNE" | ||
) | ||
test_user_extension.is_organization_rep = True | ||
test_user_extension.save_to_db() | ||
|
||
response = self.client.put( | ||
"/organization", | ||
headers={"Authorization": AUTH_COOKIE["Authorization"].value}, | ||
data=json.dumps( | ||
dict(self.correct_payload_organization) | ||
), | ||
dict(self.correct_payload_organization) | ||
), | ||
follow_redirects=True, | ||
content_type="application/json", | ||
) | ||
self.assertEqual(HTTPStatus.CREATED, response.status_code) | ||
self.assertEqual(messages.ORGANIZATION_SUCCESSFULLY_CREATED, json.loads(response.data)) | ||
|
||
def test_api_dao_create_organization_when_name_not_unique(self): | ||
# new user | ||
test_user = UserModel( | ||
name=user2["name"], | ||
username=user2["username"], | ||
password=user2["password"], | ||
email=user2["email"], | ||
terms_and_conditions_checked=user2["terms_and_conditions_checked"] | ||
) | ||
test_user.need_mentoring = user2["need_mentoring"] | ||
test_user.available_to_mentor = user2["available_to_mentor"] | ||
|
||
test_user.save_to_db() | ||
test_user_extension = UserExtensionModel( | ||
user_id=self.test_user1_data.id, | ||
timezone="AUSTRALIA_MELBOURNE" | ||
) | ||
test_user_extension.is_organization_rep = True | ||
test_user_extension.save_to_db() | ||
organization = OrganizationModel( | ||
rep_id=test_user.id, | ||
name="Company ABC", | ||
email="[email protected]", | ||
address="506 Elizabeth St, Melbourne VIC 3000, Australia", | ||
website="https://www.ames.net.au", | ||
timezone="AUSTRALIA_MELBOURNE", | ||
) | ||
organization.rep_department = "H&R Department" | ||
organization.about = "This is about ABC" | ||
organization.phone = "321-456-789" | ||
organization.status = "DRAFT" | ||
# joined one month prior to access date | ||
organization.join_date = time.time() - 60 * 60 * 24 * 7 | ||
organization.save_to_db() | ||
|
||
# test creating the org with the same name (with another user) | ||
response = self.client.put( | ||
"/organization", | ||
headers={"Authorization": AUTH_COOKIE["Authorization"].value}, | ||
data=json.dumps( | ||
dict(self.correct_payload_organization) | ||
), | ||
follow_redirects=True, | ||
content_type="application/json", | ||
) | ||
self.assertEqual(HTTPStatus.CONFLICT, response.status_code) | ||
self.assertEqual(messages.ORGANIZATION_NAME_ALREADY_USED, json.loads(response.data)) | ||
|
||
def test_api_dao_update_organization_successfully(self): | ||
# prepare existing organization | ||
organization = OrganizationModel( | ||
rep_id=self.test_user1_data.id, | ||
rep_id=self.test_user1_data.id, | ||
name="Company ABC", | ||
email="[email protected]", | ||
address="506 Elizabeth St, Melbourne VIC 3000, Australia", | ||
|
@@ -119,18 +162,17 @@ def test_api_dao_update_organization_successfully(self): | |
organization.phone = "321-456-789" | ||
organization.status = "DRAFT" | ||
# joined one month prior to access date | ||
organization.join_date = time.time() - 60*60*24*7 | ||
organization.join_date = time.time() - 60 * 60 * 24 * 7 | ||
|
||
db.session.add(organization) | ||
db.session.commit() | ||
|
||
test_user_extension = UserExtensionModel( | ||
user_id=self.test_user1_data.id, | ||
timezone="AUSTRALIA_MELBOURNE" | ||
) | ||
test_user_extension.is_organization_rep = True | ||
test_user_extension.save_to_db() | ||
|
||
update_payload_organization = { | ||
"representative_department": "H&R Department", | ||
"name": "Company ABC", | ||
|
@@ -142,13 +184,12 @@ def test_api_dao_update_organization_successfully(self): | |
"phone": "321-456-789", | ||
"status": "Publish", | ||
} | ||
|
||
response = self.client.put( | ||
"/organization", | ||
headers={"Authorization": AUTH_COOKIE["Authorization"].value}, | ||
data=json.dumps( | ||
dict(update_payload_organization) | ||
), | ||
dict(update_payload_organization) | ||
), | ||
follow_redirects=True, | ||
content_type="application/json", | ||
) | ||
|
@@ -162,15 +203,14 @@ def test_api_dao_get_organization_not_representative(self): | |
) | ||
test_user_extension.is_organization_rep = False | ||
test_user_extension.save_to_db() | ||
|
||
response = self.client.put( | ||
"/organization", | ||
headers={"Authorization": AUTH_COOKIE["Authorization"].value}, | ||
data=json.dumps( | ||
dict(self.correct_payload_organization) | ||
), | ||
dict(self.correct_payload_organization) | ||
), | ||
follow_redirects=True, | ||
content_type="application/json", | ||
) | ||
self.assertEqual(HTTPStatus.FORBIDDEN, response.status_code) | ||
self.assertEqual(messages.NOT_ORGANIZATION_REPRESENTATIVE, response.json) | ||
self.assertEqual(messages.NOT_ORGANIZATION_REPRESENTATIVE, response.json) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NenadPantelic perhaps rename this to "...._when_name_already_exist" to avoid ambiguity coz organization name is unique.