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

fix: identity sessions list response includes pagination headers #2763

Conversation

brahmlower
Copy link
Contributor

@brahmlower brahmlower commented Sep 28, 2022

This resolves issue #2762

This resolves the issue of missing pagination headers in the response to the /admin/identities/{id}/sessions endpoint by adding a CountSessionsByIdentity function to the persistence module, and then using that new function within the api handler to get the total records count needed for calling the x.PaginationHeader function.

The CountSessionsByIdentity function is scoped to the provided Identity ID, and respects the active query parameter if it was present.

Changelog entry:

Fixed adminListIdentitySessions endpoint response missing pagination headers

Related issue(s)

This PR resolves the following ticket, which I just opened a short bit ago to comply with the Contributions guide. That ticket details the issue more thoroughly, including the steps for reproducing the issue.

#2762

Checklist

  • I have read the contributing guidelines.
  • I have referenced an issue containing the design document if my change
    introduces a new feature. (not applicable, I think)
  • I am following the
    contributing code guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got the approval (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.
    (This is checked because technically this change fixes compliance with the existing documentation

Further Comments

I opened this PR pretty quickly after opening the linked issue, even though no feedback has been provided on said issue. After digging through the code, I was pretty confident that this omission is actually a bug and not an intentional design decision. It only took a few minutes to track through the codebase to make the necessary changes, so I figured it was worth opening the PR before receiving feedback on the issue 🤷

@CLAassistant
Copy link

CLAassistant commented Sep 28, 2022

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Sep 28, 2022

Codecov Report

Merging #2763 (d8514b5) into master (d8514b5) will not change coverage.
The diff coverage is n/a.

❗ Current head d8514b5 differs from pull request most recent head 3d10b04. Consider uploading reports for the commit 3d10b04 to get more accurate results

@@           Coverage Diff           @@
##           master    #2763   +/-   ##
=======================================
  Coverage   76.04%   76.04%           
=======================================
  Files         302      302           
  Lines       17980    17980           
=======================================
  Hits        13672    13672           
  Misses       3268     3268           
  Partials     1040     1040           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Contributor

@jonas-jonas jonas-jonas left a comment

Choose a reason for hiding this comment

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

Hi, thanks for your contribution. This is awesome 🎉

Could you add some tests, that prove your change does what it's supposed to?

@brahmlower
Copy link
Contributor Author

Could you add some tests, that prove your change does what it's supposed to?

Oh yeah totally! I got a little excited about opening the PR yesterday and completely forgot about adding tests- sorry about that 😅 Thanks for highlighting where the relevant tests should go, I'll get them added over lunch later today 👍

Copy link
Member

@zepatrik zepatrik left a comment

Choose a reason for hiding this comment

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

Thanks for coming up with this PR 👍
I have an idea how to simplify it and reduce the code you have to test 😉

@@ -76,6 +76,24 @@ func (p *Persister) ListSessionsByIdentity(ctx context.Context, iID uuid.UUID, a
return s, nil
}

func (p *Persister) CountSessionsByIdentity(ctx context.Context, iID uuid.UUID, active *bool) (int64, error) {
Copy link
Member

Choose a reason for hiding this comment

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

You can avoid this whole function and instead use the (*pop.Query).Paginator.TotalEntriesSize form the original

func (p *Persister) ListSessionsByIdentity(ctx context.Context, iID uuid.UUID, active *bool, page, perPage int, except uuid.UUID) ([]*session.Session, error) {
query similar to: https://github.com/gobuffalo/pop/blob/8919b888d580830e5f04e23b56bc860a07a2f7eb/pagination_test.go#L58-L67
So changing the signature of ListSessionsByIdentity to return (sessions []*session.Session, totalCount int64, err error)

@brahmlower brahmlower force-pushed the fix/adminListIdentitySessions-response-pagination-headers branch from 6c0a08e to 4a26981 Compare September 29, 2022 04:01
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}

x.PaginationHeader(w, urlx.AppendPaths(h.r.Config().SelfAdminURL(r.Context()), RouteCollection), total, page, perPage)
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 hadn't noticed this listSessions function yesterday when I made my first set of changes. Since it accepts the pagination query params, it seemed reasonable to add the pagination headers to this endpoint as well.

@brahmlower
Copy link
Contributor Author

brahmlower commented Sep 29, 2022

Thank you both for guidance there- I've force pushed an update with the requested changes 👍 My couple comments above are just calling out noteworthy changes in this latest push


I don't mean to distract the focus of this review, but while testing, I noticed that when the page size exactly matches the total number of items, the "prev" link will be added to the pagination with the page number -1 like so:

<http://kratos:4434/identities?page=0&per_page=2>; rel="first",<http://kratos:4434/identities?page=-1&per_page=2>; rel="prev"

I took a quick look at open issues on the ory/x repo and didn't see anything mentioning this. If this is not intentional, I can open an issue for this over on that repo. Just let me know

@brahmlower brahmlower force-pushed the fix/adminListIdentitySessions-response-pagination-headers branch from 4a26981 to e6c141d Compare September 29, 2022 05:07
@aeneasr
Copy link
Member

aeneasr commented Oct 5, 2022

Hi, could you please resolve the merge conflicts and also fix the tests? :)

@brahmlower
Copy link
Contributor Author

Hey! Really sorry for the radio silence here- yeah I am still planning to rebase this and fix up the tests. This week has been a bit hectic and time has gotten away from me is all 😅 Am hoping to dedicate more time to this tomorrow 👍

@brahmlower brahmlower force-pushed the fix/adminListIdentitySessions-response-pagination-headers branch from 1dd145f to 67c8590 Compare October 8, 2022 20:45
@brahmlower brahmlower force-pushed the fix/adminListIdentitySessions-response-pagination-headers branch from 67c8590 to 722c159 Compare October 8, 2022 22:21
@brahmlower
Copy link
Contributor Author

I think I was having an off day when I first wrote those tests 😅 I've improved them a bit and added an extra set of tests for the admin sessions list as well, so it looks like this should be set now 👍

If there's anything else yall need for reviewing this, just let me know

Copy link
Contributor

@jonas-jonas jonas-jonas left a comment

Choose a reason for hiding this comment

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

LGTM! Nice work! And sorry for the delay. :)

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Great work, thank you!

@aeneasr aeneasr merged commit 0c2efa2 into ory:master Oct 13, 2022
@brahmlower
Copy link
Contributor Author

brahmlower commented Oct 13, 2022

No problemo! Thanks for considering and accepting the contributions 😁

@brahmlower brahmlower deleted the fix/adminListIdentitySessions-response-pagination-headers branch October 13, 2022 17:40
peturgeorgievv pushed a commit to senteca/kratos-fork that referenced this pull request Jun 30, 2023
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.

None yet

5 participants