Commit Graph

19815 Commits

Author SHA1 Message Date
Javi Martín
6e5ef9795e Track ahoy visits on the server side
In commit 96ae69fe9, we stopped using cookies to track Ahoy visits and
started using a combination of the IP and the browser agent instead.

However, since we're still using the legacy logic from Ahoy 1.x to track
visits (which we had to add in commit b5220effd), this way of tracking
visits doesn't work and counts every page visited by a user as an
independent visit.

Maybe we could migrate existing data, which uses the `visitor_id` column
so it uses the new `visit_token` and `visitor_token` columns, but
there's no mention in the Ahoy documentation regarding how to do so.

While deciding what to do about this, we found something interesting.

For two years, we've been seeing random failures in the
`system/admin/tenants_spec.rb` tests, with messages like:

```
1) Tenants Create Tenant with subdomain
     Failure/Error:
       raise TenantNotFound, <<~EXCEPTION_MESSAGE
         Could not set search path to schemas, they may be invalid:
           "#{tenant}" #{full_search_path}.
         Original error: #{exception.class}: #{exception}
       EXCEPTION_MESSAGE

     Apartment::TenantNotFound:
       Could not set search path to schemas, they may be invalid:
         "earth" "public", "shared_extensions".
       Original error:
         ActiveRecord::StatementInvalid: Could not find schema earth
```

And we've found one of the causes: the AJAX requests done by Ahoy to
track visits. Sometimes a test that creates or updates a tenant finishes
but the Ahoy AJAX request to, say, `earth.lvh.me/ahoy/visits`, is
handled by the next test, when the `earth` schema no longer exists, thus
raising an `Apartment::TenantNotFound` exception.

So by disabling these AJAX requests and tracking the visits in the
server instead, we're killing two birds in one stone: we're fixing the
bug regarding the visits count and we're reducing the flakiness in our
test suite. It looks like we're also removing the "phantom ahoy cookie"
we were getting since the mentioned commit b5220effd: an ahoy cookie was
quickly set and unset in the browser.

Note that, even though we aren't migrating any data, we're still adding
the new fields, because some tests started to fail because, when
tracking visits in the server without cookies, Ahoy expects the Visit
model to have a `visit_token` field.
2024-11-07 12:04:46 +01:00
Javi Martín
1c10ed0ec7 Remove legacy field in the visits table
The search_keyword column was removed for new installations in Ahoy
2.1.0 and stopped being used completely in Ahoy 3.0.0.

There are other columns that we use and are no longer generated by
default by Ahoy, which are: screen_height, screen_width and postal_code.
Apparently they're still used in Ahoy if they're available, though, so
we aren't removing them, at least for now.
2024-11-07 12:04:46 +01:00
Javi Martín
df2540d483 Merge pull request #5761 from consuldemocracy/officing_voters_lock
Include voter initialization in officing voters lock
2024-11-07 11:37:25 +01:00
Javi Martín
45851c74bd Include voter initialization in officing voters lock
For reasons that might or might not affect production installations, the
test checking simultaneous requests to create poll voters in the
officing voters controller wasn't behaving as expected.

The expected behavior, since commit 9a8bfac5b, is that the second
request reaching the `with_lock` part of the code waits for the first
request to finish and so this second request raises an
`ActiveRecord::RecordInvalid` exception when trying to save a voter with
the same poll and the same user as the first one.

However, 95% of the time that wasn't the case. Instead, when entering
the `@user.with_lock` block, the second request would replace its
`@voter` object with the `@voter` object saved in the same request, so
the second call to `save!` would succeed as it would simply update the
existing record.

This is a behavior that we could accept if it were consistent and
happened 100% of the time, but that isn't the case. 5% of the time, we
do get the `ActiveRecord::RecordInvalid` exception. So 5% of the time we
got a failure in the test:

```
  1) Officing::VotersController POST create does not create two records
     with two simultaneous requests
     Failure/Error: @user.with_lock { @voter.save! }

     ActiveRecord::RecordInvalid:
       Validation failed: User User has already voted
     # ./app/controllers/officing/voters_controller.rb:25:in `block in create'
     # ./app/controllers/officing/voters_controller.rb:25:in `create'
     # ./app/controllers/application_controller.rb:50:in `switch_locale'
     # ./spec/controllers/officing/voters_controller_spec.rb:15:in `block (5 levels) in <top (required)>'
```

So we're changing the `with_lock` block so it includes the
initialization of the object. This way, we get the
`ActiveRecord::RecordInvalid` exception 100% of the time.

Note that in commit 9a8bfac5b we also rescued the
`ActionDispatch::IllegalStateError` exceptions. I'm not why we were
getting those exceptions when running the tests, and I'm not sure
whether we keep getting after these changes, but it doesn't really
matter. The reason is that in Consul Democracy 2.3.0 we're going to add
a unique index to the `poll_voters` table, which (according to the tests
done in the past) will make both the `@user.lock` block and rescuing the
`ActionDispatch::IllegalStateError` unnecessary.

So, in other words, these changes will never make it to production
because this part of the code will be changed again before releasing
version 2.3.0.
2024-11-07 11:15:15 +01:00
Javi Martín
e96171541b Merge pull request #5759 from consuldemocracy/fix_hide_public_page_test
Add missing expectation in users test
2024-11-06 16:18:46 +01:00
Javi Martín
df716c3de6 Add missing expectations in users tests
One of these tests has failed in our CI with the following message:

```
1) Users Public activity user can hide public page

   Failure/Error: expect(page).to have_content("activity list private")
     expected to find text "activity list private" in "Language: \n
     \nEnglish\nDeutsch\nEspañol\nFrançais\nNederlands\nPortuguês
     brasileiro\n中文\n       Notifications\nYou are in\nMy content\nMy
     account\nSign out\nDebates\nProposals\nVoting\nCollaborative
     legislation\nParticipatory budgeting\nSDG\nHelp\nM\nManuela124\nUser has
     no public activity\nOpen government\nThis portal uses the CONSUL
     DEMOCRACY application which is open-source
     software.\nParticipation\nDecide how to shape the city you want to live
     in.\nCONSUL DEMOCRACY, 2024 Privacy Policy Terms and conditions of use
     Accessibility"
```

Note how the text "User has no public activity" is present, which is a
message that appears when the user's activity is public.

A possible explanation is that we didn't check that the request done by
the "Save changes" button had finished before continuing with the tests.
Back when we wrote this test, submitting a form in a test would always
wait for the request to be finished before continuing, but a lot has
changed since then.

So we're adding an expectation to make sure the the changes have been
saved before making a new request.

We're also rearraging the blank lines in these tests and removing the
parenthesis in `have_content` expectations to be consistent with the
expectations we're adding.
2024-11-06 15:57:25 +01:00
Javi Martín
fbcba3afdb Merge pull request #5758 from consuldemocracy/comment_author_class_tests
Remove instance_double usage in CommentsHelper tests
2024-11-06 15:56:56 +01:00
Javi Martín
7f40029a26 Fix typo in document management verification tests 2024-11-06 15:39:10 +01:00
Javi Martín
bb50f02ba1 Replace instance_double usages with double
After the previous commit, we were using `double` in many places but
were only using `instance_double` in one file. So, for consistency,
we're using `double` in this file as well.
2024-11-06 15:27:22 +01:00
Javi Martín
f721e88af8 Remove instance_double usage in CommentsHelper tests
Lately (not sure since when), from time to time we've been getting these
failures in our CI:

```
Failures:

  1) CommentsHelper#comment_author_class returns is-author if author is the commenting user
     Failure/Error: comment = instance_double(Comment, user_id: author_id)
       the Comment class does not implement the instance method: user_id
     # ./spec/helpers/comments_helper_spec.rb:48:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:40:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:39:in `block (2 levels) in <top (required)>'

  2) CommentsHelper#comment_author_class returns an empty string if commenter is not the author
     Failure/Error: comment = instance_double(Comment, user_id: author_id - 1)
       the Comment class does not implement the instance method: user_id
     # ./spec/helpers/comments_helper_spec.rb:55:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:40:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:39:in `block (2 levels) in <top (required)>'
```

It might be related to the upgrade of rspec-rails done in commit
6fe222148 or maybe due to a change in github actions that caused some
tests to fail, as described in commits bedcb5bca and 3e44eeaee.

What might be causing the issue is the usage of `instance_double`
stubbing different methods in different tests (not sure this is the
cause, though).

We've seen that somebody got a similar error [1] (although it might not
have been for the same reason) and one of the maintainers of rspec-mocks
replied:

> I would recommend switching to double (as you mentioned) or
> refactoring to use something more defined.

So we're simply using `double`, which is what we usually use when
stubbing objects in the tests. Doing so is faster than further
investigating why the `instance_double` isn't reliable 100% of the time.

[1] See issue 1587 in https://github.com/rspec/rspec-mocks/
2024-11-06 15:21:51 +01:00
Sebastia
d242170771 Merge pull request #5057 from consuldemocracy/only_manage_tenants
Add an option to enable the "Multitenancy management mode"
2024-11-06 14:59:50 +01:00
taitus
4972c51974 Add documentation for multitenancy management mode 2024-11-06 14:46:03 +01:00
Sebastia
c91501a056 Merge pull request #5766 from consuldemocracy/graphql-refactor
Reduce code duplication in Graphql
2024-11-06 14:10:50 +01:00
taitus
2938ced41c Add collection_field helper to BaseObject
Add a helper method in BaseObject to define fields with `connection_type`,
reducing code duplication and giving more context about the type of fields.
2024-11-06 13:51:28 +01:00
taitus
a426537b4c Add helper method to define collection and object by id fields in QueryType
- Introduce `collection_and_object_by_id_fields` in QueryType to avoid duplication.
2024-11-06 13:51:28 +01:00
taitus
d3b253dfc7 Add object_by_id_field helper to BaseObject and replace argument definitions
- Created `object_by_id_field` method in `BaseObject` to simplify the declaration of fields
with an `id` argument.
- Replaced all instances of `field ... do` blocks with `object_by_id_field` where fields require
an `id` argument across multiple types.
2024-11-06 13:51:28 +01:00
Sebastia
04356accb9 Merge pull request #5737 from consuldemocracy/dependabot/bundler/graphql-2.3.18
Bump graphql from 2.0.31 to 2.3.18
2024-11-06 13:48:34 +01:00
taitus
3227b7e184 Ensure that we are testing a belongs_to association 2024-11-06 13:29:28 +01:00
Sebastia
1c684c3daf Merge pull request #5752 from consuldemocracy/dependabot/bundler/sassc-embedded-1.80.1
Bump sassc-embedded from 1.70.1 to 1.80.1
2024-11-06 13:16:42 +01:00
dependabot[bot]
64bcedc8b2 Bump sassc-embedded from 1.70.1 to 1.80.1
Note: Since we update to 1.80.1 deprecation warnings are appear when execute the assets:precompile command.
In order to silence this deprecation, we add silence_deprecation option in sass.rb initializer.

The code has also been updated to remove the deprecation warnings that appeared related to the function
darken(), lighten() and "Using / for division" instead of the function calc().

Bumps [sassc-embedded](https://github.com/sass-contrib/sassc-embedded-shim-ruby) from 1.70.1 to 1.80.1.
- [Commits](https://github.com/sass-contrib/sassc-embedded-shim-ruby/compare/v1.70.1...v1.80.1)

---
updated-dependencies:
- dependency-name: sassc-embedded
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-11-06 13:01:58 +01:00
taitus
93b35fcecc Redirect root path requests to the tenants administration
When the `multitenancy_management_mode` is enabled.

In order to avoid infinite redirects when regular users try to access
the admin section, we're redirecting to the account page in this case.
Otherwise, the admin section would redirect to the root path, which
would redirect to the admin section, which would redirect to the root
path, and so on.
2024-11-06 11:17:58 +01:00
taitus
a5911f5c6a Modify admin layout to only manage tenants and admins
We only want to render the account link and login items in the header.
And we want only render the Multitenancy and Administrators sections in
the admin sidebar.

We include the administrators management so it's possible to give
permissions to other users to manage tenants.

In order to restrict access to other sections by typing the URL or
following a link, we're only enabling the rest of the routes when we
aren't in the multitenancy management mode.
2024-11-06 11:17:53 +01:00
taitus
1e6901ec34 Add render method to notification item component 2024-11-06 11:07:00 +01:00
taitus
1a8676f2d4 Add secret to allow the default tenant only be used to manage other tenants
Co-Authored-By: Senén Rodero <senenrodero@gmail.com>
2024-11-06 11:07:00 +01:00
Javi Martín
60dbda600d Move resolve clauses to the main routes file
We're going to add some constraints in the routes file, and if we add a
`resolve` clause inside a constraints block, we get an error saying that
"The resolve method can't be used inside a routes scope block" when
starting the application.
2024-11-06 11:07:00 +01:00
taitus
78d36dd563 Start using run_graphql_field in specific field tests
- Introduced `run_graphql_field` in tests that focus on resolving specific fields, leveraging the method added in GraphQL 2.2.0.
- Continued using `execute` for broader cases where it is still necessary to test entire GraphQL queries.
2024-11-05 19:28:34 +01:00
Javi Martín
601873f16f Merge pull request #5771 from consuldemocracy/dependabot/bundler/rails-7.0.8.6
Bump rails from 7.0.8.4 to 7.0.8.6
2024-11-02 12:06:36 +01:00
dependabot[bot]
6b80e80680 Bump rails from 7.0.8.4 to 7.0.8.6
Bumps [rails](https://github.com/rails/rails) from 7.0.8.4 to 7.0.8.6.
- [Release notes](https://github.com/rails/rails/releases)
- [Commits](https://github.com/rails/rails/compare/v7.0.8.4...v7.0.8.6)

---
updated-dependencies:
- dependency-name: rails
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-11-01 22:05:09 +00:00
Javi Martín
ee18369865 Merge pull request #5636 from coslajohn/admin_restrict
Restrict access to Admin functions by IP Address
2024-10-30 16:38:39 +01:00
CoslaJohn
424cedc0c8 Restrict access to admin functions by IP
There are many possible ways to implement this feature:

* Adding a custom middleware
* Using rack-attack with a blocklist
* Using routes constraints

We're choosing to use a controller concern with a redirect because it's
what we do to handle unauthorized cancancan exceptions.
2024-10-30 15:59:50 +01:00
Javi Martín
07202fea10 Add and apply Style/RedundantBegin rubocop rule
We're about to add code which might fall into the `RedundantBegin`
category, so we're adding the rule in order to prevent that.
2024-10-30 15:57:44 +01:00
Javi Martín
9f38ed6bae Extract method to stub secrets in tests 2024-10-30 14:50:41 +01:00
dependabot[bot]
e7f6e39679 Bump graphql from 2.0.31 to 2.3.18
Note: The parser error message format changed in GraphQL 2.2.0 due to the introduction
of a new optimized lexer and a hand-written parser (PR 4718). This commit updates
the `parser_error_raised?` method in the GraphqlController tests to correctly detect
errors using the new message format.

The previous pattern was checking for "Parse error on", but with the new version,
the error message now contains "Expected one of". This change ensures that the
tests for malformed queries continue to pass as expected.

Bumps [graphql](https://github.com/rmosolgo/graphql-ruby) from 2.0.31 to 2.3.18.
- [Release notes](https://github.com/rmosolgo/graphql-ruby/releases)
- [Changelog](https://github.com/rmosolgo/graphql-ruby/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rmosolgo/graphql-ruby/compare/v2.0.31...v2.3.18)

---
updated-dependencies:
- dependency-name: graphql
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-30 14:04:59 +01:00
taitus
62e85df83b Correctly check that the value of data is nil
The way we access "data" always returned nil
2024-10-30 14:03:51 +01:00
Sebastia
6133412ba5 Merge pull request #5749 from consuldemocracy/foundation-sites-6.8.0
Bump foundation-sites from 6.7.5 to 6.8.1
2024-10-30 12:19:29 +01:00
Sebastia
2b511b50b6 Merge pull request #5765 from consuldemocracy/markdown-it-13.0.2
Bump markdown-it from 12.3.2 to 13.0.2
2024-10-30 12:19:01 +01:00
Sebastia
1f0d311de3 Merge pull request #5748 from consuldemocracy/graphql-2.0.0
Bump graphql from 1.13.22 to 2.0.31
2024-10-30 12:09:09 +01:00
Javi Martín
3af62f452d Merge pull request #5763 from consuldemocracy/dependabot/bundler/rexml-3.3.9
Bump rexml from 3.3.8 to 3.3.9
2024-10-30 12:01:16 +01:00
taitus
ec7745d5a6 Bump markdown-it from 12.3.2 to 13.0.2 2024-10-30 09:59:27 +01:00
taitus
4d981be7bd Bump foundation-sites from 6.7.5 to 6.8.1 2024-10-30 09:16:25 +01:00
Javi Martín
7311a70bfe Merge pull request #5757 from nfourre/fix/fix-social-login-button-fill-space
Fill in space in social buttons when options are turned off
2024-10-29 10:44:16 +01:00
Nicolas Fourre
a81fba71f0 fix(social-login): social button fill space when option are turn off 2024-10-28 21:25:22 +01:00
Javi Martín
3931b43b87 Move omniauth form partial to a component
This way we simplify the view a little bit and replace some slow system
tests with faster component tests.
2024-10-28 21:23:56 +01:00
dependabot[bot]
c4be81ab43 Bump rexml from 3.3.8 to 3.3.9
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.8 to 3.3.9.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.8...v3.3.9)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-28 19:01:37 +00:00
Sebastia
58639b64fa Merge pull request #5713 from consuldemocracy/dependabot/bundler/ahoy_matey-5.2.0
Bump ahoy_matey from 5.0.2 to 5.2.0
2024-10-28 14:29:09 +01:00
Javi Martín
dcf54ee5f1 Merge pull request #5498 from consuldemocracy/toggle_switch
Use a switch control to toggle selections
2024-10-28 14:19:58 +01:00
Javi Martín
fc5103881d Use a switch to toggle visibility to valuators
Using a checkbox wasn't very intuitive because checkboxes are
checked/unchecked when clicked on even if there's an error in the
request. Usually, when checkboxes appear on a form, they don't send any
information to the server unless we click a button to send the form.

So we're using a switch instead of a checkbox, like we did to
enable/disable phases in commit 46d8bc4f0.

Note that, since we've got two switches that match the default
`dom_id(record) .toggle-switch` selector, we need to find a way to
differentiate them. We're adding the `form_class` option for that.

Also note that we're now using a separate action and removing the
JavaScript in the `update` action which assumed that AJAX requests to
this action were always related to updating the `visible_to_valuators`
attribute.
2024-10-28 13:41:55 +01:00
Javi Martín
76b0971b4a Simplify mark as visible to valuators tests
We were performing 3 requests in order to refresh the page and check the
content was still there. We can use `refresh` instead.

We're also reusing the `investment1` variable in every test, instead of
redifining it in one of them.
2024-10-28 13:41:55 +01:00
Javi Martín
00d7299e9e Extract component for visible to valuators toggling 2024-10-28 13:41:55 +01:00
Javi Martín
958c13061f Fix duplicate HTML visible to valuator IDs 2024-10-28 13:41:55 +01:00