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

refactor: check validate_organization! on graphql resolve when RequiredOrganization included #1853

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

josemoyab
Copy link
Contributor

@josemoyab josemoyab commented Apr 8, 2024

Context

Following the convention over configuration and DRY (Don't Repeat Yourself) principles of Ruby on Rails design paradigm, this PR implements automatic validation of validate_organization! when including the RequiredOrganization concern on GraphQL query/mutation resolvers

Description

  • Automatically triggers validate_organization! for resolve when RequiredOrganization is included.
  • Updates the spec for Mutations::Invites::Revoke to use a user within the organization.
  • Enhances the spec for Mutations::Wallets::Update to include the current organization in the GraphQL mutation.
  • Additionally, updates the expirationAt check to prevent false positives when tests run for more than 1 second.

@josemoyab josemoyab marked this pull request as draft April 8, 2024 10:08
@josemoyab josemoyab force-pushed the clean-resolvers branch 2 times, most recently from 328f3a1 to 1ec2727 Compare April 8, 2024 11:09
@josemoyab josemoyab marked this pull request as ready for review April 8, 2024 11:11
@josemoyab josemoyab marked this pull request as draft April 8, 2024 12:15
@josemoyab josemoyab marked this pull request as ready for review April 8, 2024 15:55
@josemoyab josemoyab changed the title refactor: check validate_organization! on graphql resolve when RequiredOrganization include refactor: check validate_organization! on graphql resolve when RequiredOrganization included Apr 8, 2024
@vincent-pochet
Copy link
Collaborator

Really good @josemoyab! Thank you very much for this contribution

@vincent-pochet vincent-pochet merged commit d7acfa9 into getlago:main Apr 9, 2024
2 of 3 checks passed
julienbourdeau added a commit that referenced this pull request Apr 29, 2024
## Context

Granular permissions are being added to Lago

## Description

A first PR added all the permission system foundations to Lago
(#1926). A second PR added
permissions to all GraphQL mutations
(#1941).


- As I'm working on adding it to Resolvers, I realized I needed the
exact the thing as mutations so I extracted some logic into
`CanRequirePermissions` concern.
- Not all resolvers were extending our custom BaseResolvers, now they
do.
- As I was testing my new concern, I added tests to
`AuthenticableApiUser` and `RequiredOrganization` concerns

When I was editing tests for mutations, I add to edit MANY tests
re-testing _"without current user"_ and _"without current
organization"_. I believe those tests are redundant and we can now
simply tests of the Mutation/Resolver includes the correct concern. The
behavior is tested once in the concern. See e48109d for single example.

Recently @josemoyab introduced [a great
refactor](#1853) of
`RequiredOrganization` to avoid calling `validate_organization!` in
every `resolve` method. I'm taking this one step further by removing the
meta programming and using the `ready?` method to be consistent with
other concerns.

I'll deal with `AuthenticableCustomerPortalUser` concern separately as
this PR is already huge and it doesn't affect my work on RBAC.

### About execution order

Each `ready?` method will be executed in order, starting by the last
concern added, all the way to the first concern added.

```ruby
class CreateThing < BaseMutation
    include AuthenticableApiUser
    include RequiredOrganization

    ...
end
```

In the above snippet, we'll call `RequiredOrganization#ready?` first,
then `AuthenticableApiUser#ready?`. In this case, you'll never get to
the `AuthenticableApiUser` error because `RequiredOrganization#ready?`
also checks that the current_user is part of the current_organization.
Without a current_user, this check will never pass.

**This is the reason why I had to edit all mutation tests in the PR**.
Previously, the `AuthenticableApiUser#ready?` would run first, then
`resolve` would automatically call `current_organization!`.

### Nota Bene

We don't need to add dedicated tests case for current_user and
curent_organization in every new mutation or resolver.
drejc pushed a commit to fliqa-io/lago-api that referenced this pull request May 15, 2024
drejc pushed a commit to fliqa-io/lago-api that referenced this pull request May 15, 2024
## Context

Granular permissions are being added to Lago

## Description

A first PR added all the permission system foundations to Lago
(getlago#1926). A second PR added
permissions to all GraphQL mutations
(getlago#1941).


- As I'm working on adding it to Resolvers, I realized I needed the
exact the thing as mutations so I extracted some logic into
`CanRequirePermissions` concern.
- Not all resolvers were extending our custom BaseResolvers, now they
do.
- As I was testing my new concern, I added tests to
`AuthenticableApiUser` and `RequiredOrganization` concerns

When I was editing tests for mutations, I add to edit MANY tests
re-testing _"without current user"_ and _"without current
organization"_. I believe those tests are redundant and we can now
simply tests of the Mutation/Resolver includes the correct concern. The
behavior is tested once in the concern. See e48109d for single example.

Recently @josemoyab introduced [a great
refactor](getlago#1853) of
`RequiredOrganization` to avoid calling `validate_organization!` in
every `resolve` method. I'm taking this one step further by removing the
meta programming and using the `ready?` method to be consistent with
other concerns.

I'll deal with `AuthenticableCustomerPortalUser` concern separately as
this PR is already huge and it doesn't affect my work on RBAC.

### About execution order

Each `ready?` method will be executed in order, starting by the last
concern added, all the way to the first concern added.

```ruby
class CreateThing < BaseMutation
    include AuthenticableApiUser
    include RequiredOrganization

    ...
end
```

In the above snippet, we'll call `RequiredOrganization#ready?` first,
then `AuthenticableApiUser#ready?`. In this case, you'll never get to
the `AuthenticableApiUser` error because `RequiredOrganization#ready?`
also checks that the current_user is part of the current_organization.
Without a current_user, this check will never pass.

**This is the reason why I had to edit all mutation tests in the PR**.
Previously, the `AuthenticableApiUser#ready?` would run first, then
`resolve` would automatically call `current_organization!`.

### Nota Bene

We don't need to add dedicated tests case for current_user and
curent_organization in every new mutation or resolver.
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

2 participants