Skip to content
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

Features/3 add tests and coverage with GitHub actions #10

Merged
merged 17 commits into from
Feb 28, 2024

Conversation

nargis-sultani
Copy link
Contributor

[Short description explaining the high-level reason for the pull request]

Additions

-Added tests
-Added coverage.yml and tests.yml

Removals

Changes

Testing

Screenshots

Notes

Todos

Checklist

  • PR has an informative and human-readable title
  • Changes are limited to a single goal (no scope creep)
  • Code can be automatically merged (no conflicts)
  • Code follows the standards laid out in the development playbook
  • Passes all existing automated tests
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future todos are captured in comments
  • Visually tested in supported browsers and devices (see checklist below 👇)
  • Project documentation has been updated (including the "Unreleased" section of the CHANGELOG)
  • Reviewers requested with the Reviewers tool ➡️

Testing checklist

Browsers

  • Chrome
  • Firefox
  • Safari
  • Internet Explorer 8, 9, 10, and 11
  • Edge
  • iOS Safari
  • Chrome for Android

Accessibility

  • Keyboard friendly
  • Screen reader friendly

Other

  • Is useable without CSS
  • Is useable without JS
  • Flexible from small to large screens
  • No linting errors or warnings
  • JavaScript tests are passing

@nargis-sultani nargis-sultani linked an issue Feb 16, 2024 that may be closed by this pull request
@nargis-sultani nargis-sultani marked this pull request as draft February 16, 2024 16:21
Copy link

github-actions bot commented Feb 16, 2024

Coverage report

This PR does not seem to contain any modification to coverable code.

@nargis-sultani nargis-sultani marked this pull request as ready for review February 16, 2024 17:49
# comments (to avoid publishing multiple comments in the same PR)
contents: write
steps:
- uses: actions/checkout@v3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are v4 of the checkout and upload-artifact actions that have library updates and speed improvements. Probably not needed, but trying to keep those updated when I have to modify actions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update.

pyproject.toml Outdated
"INST_DB_USER=user",
"INST_DB_PWD=user",
"INST_DB_HOST=localhost:5432",
"INST_DB_NAME=fi",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need the institution DB env vars in this repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to remove those.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to test the decorated function with both /items/foo and /items/foo/, since the point of the decorator is to make those two urls hit the same endpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, sure will work on it.

name = "Test Name"

mock_get_group = mocker.patch("regtech_api_commons.oauth2.oauth2_admin.OAuth2Admin.get_group")
mock_get_group.return_value = {"id": lei, "Name": name}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the goal here would be to patch the KeycloakAdmin get_group_by_path function, similar to how you patched the update_group during the upsert. Othewise, you're bypassing our "get_group" function, which is what we're really trying to test.


result = oauth2_admin.associate_to_lei(user_id=user_id, lei=lei)
assert result is None

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here with test_associate_to_lei and test_associate_to_leis. Looks you're patching the function we're supposed to be testing, versus the KeycloakAdmin functions internal to those functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jcadam14 ! I am looking into all your comments and addressing them.

assert response.json() != {"item_id": "foo"}


def test_get_api_route_not_decorated():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add another test for the not decorated one, so we can see the response being 302 (I think, the redirect one)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry... should've read the code closer; were you just trying to test out the different ways to hook up a route? I thought you were testing out the different behaviors of router wrapper and fastapi's built-in router. In that case you can remove the redirect test since it's a manually provided redirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just trying to test whether the wrapper hits the same endpoint if a slash is added at the end of the url. I think the not decorated test is not needed here, so removed it. But I can more tests per your suggestion.

Comment on lines 25 to 48
def test_incorrect_from_claims():
test_authenticated_user = AuthenticatedUser(
claims=test_claims,
name="test_user_incorrect",
username="test_incorrect_username",
email="[email protected]",
id="test_incorrect_id",
institutions=["/incorrect_institution_1", "/incorrect_institution_2"],
)

assert AuthenticatedUser.from_claim(test_claims) != test_authenticated_user


def test_parse_institutions_correct():
assert AuthenticatedUser.parse_institutions(test_claims.get("institutions")) == ["TEST1LEI", "TEST2CHILDLEI"]
assert AuthenticatedUser.parse_institutions(institutions=None) == []


def test_parse_institutions_incorrect():
assert AuthenticatedUser.parse_institutions(test_claims.get("institutions")) != [
"/TESTLEI",
"/TEST2LEI/TEST2CHILDLEI",
]
assert AuthenticatedUser.parse_institutions(institutions=None) != ["TESTLEI"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the incorrect from claims test isn't needed; but for the institution parsing tests, instead of calling the parse_institutions method directly, pass in the claim with the different scenarios and test the user object's institutions property. i.e. test one with a claim that doesn't have the institutions field.

def test_get_claims():
token = "Test Token"

with patch.object(oauth2_admin, "_get_keys", return_value="secret"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of mocking the _get_keys, we should mock the requests.get, then you can do that mock.assert_called_with... check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lchen-2101 Thanks! Working on it.

Comment on lines 52 to 58
test_authenticated_user = AuthenticatedUser(
claims=test_claims,
name=test_claims.get("name"),
username=test_claims.get("preferred_username"),
email=test_claims.get("email"),
id=test_claims.get("sub"),
institutions=AuthenticatedUser.parse_institutions(test_claims.get("institutions")),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this one not using the from_claim?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look into it after I push the PR for ticket # 62 today.

Copy link
Collaborator

@lchen-2101 lchen-2101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lchen-2101 lchen-2101 merged commit 8823245 into main Feb 28, 2024
3 checks passed
@lchen-2101 lchen-2101 deleted the features/3_add_tests_and_coverage_with_github_actions branch February 28, 2024 19:47
@lchen-2101
Copy link
Collaborator

merged, for future reference, when you create the PR, in the comment section during the initial PR creation, you can put cloeses {the link to the issue}, it will automatically link the PR to the issue.

@nargis-sultani
Copy link
Contributor Author

Will do that, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tests and coverage with github actions
3 participants