Commit Graph

1209 Commits

Author SHA1 Message Date
Julian Herrero
46c78fc3ef Do not delete users when deleting legislation answers
When a legislation process is deleted, everything related will be
deleted, including the answers. This `dependent: :destroy` was causing
that users accounts were being accidentally deleted.
2020-07-27 14:32:22 +02:00
Javier Martín
c03ada579d Merge pull request #4061 from consul/ballot_race_condition
Fix race condition with ballot lines
2020-07-27 13:01:37 +02:00
Javi Martín
d9eeb1ad15 Improve test checking order by relevance
The test wasn't working when postgres used the English dictionary
because in English the word "what" was ignored (or, at least, not given
enough relevance) while searching. When we wrote the test, it passed
because back then we always used the Spanish dictionary. However, when
we switched to a dictionary based on the default locale (in commit
d99875cd), we had to force this test to keep using the Spanish
dictionary.

Using the Spanish dictionary in a test where all texts are in English is
strange to say the least ;). So here we're making the test a bit easier
to understand.

Since now we're only using the `:spanish_search` tag in one test, I've
decided to remove it and simply add it to that test's setup.
2020-07-14 14:49:39 +02:00
Javi Martín
d2d517059d Fix race condition with ballot lines
With two concurrent requests, it's possible to create two ballot lines
when only one of them should be created.

The reason is the code validating the line is not thread safe:

```
if ballot.amount_available(investment.heading) < investment.price.to_i
  errors.add(:money, "insufficient funds")
end
```

If the second request executes this code after the first request has
executed it but before the first request has saved the record to the
database, both records will pass this validation and both will be saved
to the database.

So we need to introduce a lock. Now when the second request tries to
lock the ballot, it finds it's already locked by the first request, and
will wait for the transaction of the first request to finish before
checking whether there are sufficient funds.

Note we need to disable transactions during the test; otherwise the
second thread will wait for the first one to finish.

Also note that we need to update a couple of tests because records are
reloaded when they're locked.

In one case, reloading the ballot causes `ballot.user` to be `nil`,
since the user is hidden. So we hide the user after creating all its
associated records (which is the scenario that would take place in real
life).

In the other case, reloading the ballot causes `ballot.user` to reload
as well. So we need to reload the user object used in the test too so it
gets the updates done on `ballot.user`.

I haven't been able to reproduce this behavior in a system test. The
following test works with Rails 5.0, but it stopped working when we
moved to system tests in commit 9427f014. After that commit, for reasons
I haven't been able to debug (reintroducing truncation with
DatabaseClaner didn't seem to affect this test, and neither did
increasing the number of threads in Puma), the two AJAX requests
executed here are no longer simultaneous; the second request waits for
the first one to finish.

scenario "Race conditions with simultaneous requests", :js do
  allow_any_instance_of(Budget::Ballot::Line).to receive(:check_sufficient_funds) do |object|
    allow(object).to receive(:check_sufficient_funds).and_call_original
    object.check_sufficient_funds
    sleep 0.3
  end

  ["First", "Second"].each do |title|
    create(:budget_investment, :selected,
      heading: california,
      price:   california.price,
      title:   title
    )
  end

  login_as(user)
  visit budget_investments_path(budget, heading_id: california.id)

  within(".budget-investment", text: "First") { click_link "Vote" }
  within(".budget-investment", text: "Second") { click_link "Vote" }

  expect(page).to have_link "Remove vote"
  expect(Budget::Ballot::Line.count).to eq 1
end
2020-07-12 22:11:40 +02:00
Javi Martín
f427c757ba Use hash conditions instead of SQL's IN
This is what we're doing in most places.
2020-07-08 18:34:58 +02:00
Javi Martín
a20622e2fa Allow remote translation tests to be run offline
The method `available_locales` in
`RemoteTranslations::Microsoft::AvailableLocales` needs to execute a
request to an external server in order to work, meaning it will fail if
the machine its executed on doesn't have an internet connection.

So we're stubbing the method in the tests using it.
2020-04-28 15:45:50 +02:00
Javi Martín
e7fcca9b47 Check remote translations locales at runtime
We were evaluating its value when the server starts. On production
enviroments, that could mean weeks or months before the available
locales are checked again, and so it would be possible to use a list
which is no longer in sync with the list provided by microsoft.
2020-04-28 15:45:32 +02:00
Javi Martín
12774c7484 Replace attribute_changed? in after callbacks
This method is deprecated in Rails 5.1 because its behavior will be
different in `before` and `after` callbacks.

We're replacing the deprecated `attribute_changed?` and `attribute_was`
methods with `saved_change_to_attribute?` and
`attribute_before_last_save` during `after_save` callbacks.

https://github.com/rails/rails/pull/32835/
2020-04-24 15:43:54 +02:00
Javi Martín
1118c732f1 Bump acts-as-taggable-on to 6.0.0
Rails 5.1 introduced certain changes in the way a record is touched when
the counter cache option is enabled in a belongs to association.

We need to upgrade acts-as-taggable-on so it keeps changing the
`updated_at` attribute when a new tag is added to a record.

Note we now need to reload the records in some cases to get the
`context_tag_list` method to return what we expect. Methods like
`context_tags` however work properly with no need to reload the record.
2020-04-23 18:49:43 +02:00
Paweł Świątkowski
d99875cde2 Get search dictionary based on I18n.default_locale (merge pull request #3856)
Implementation tries to be open for further extensions, such as deciding on
search dictionary based on configuration option or by locale set for
given user.
2020-04-12 14:22:36 +02:00
Javi Martín
958d373247 Fix duplicate records in investments by tag
When an investment had been assigned a user tag and a valuation tag with
the same name, it appeared twice when filtering by tag.

This is because by design, in order to provide compatibility with scopes
using "select" or "distinct", the method `tagged_with` doesn't select
unique records.

Forcing the query to return unique records solves the issue.
2020-04-09 21:09:28 +02:00
Andrew Sims
95c82d8777 Changes following PR review
* Internationalisation for admin fields
* Correct typos
* Additional tests
* Replace ternary with if-then statement
2020-04-09 07:11:53 +10:00
Cassiano Sampaio
1e4f539104 Add title to differentiate signature sheets 2020-04-09 07:11:52 +10:00
Andy Sims
74fbde09f1 Support creates follow (Merge pull request #3895)
* Supporting a proposal will create a follow relationship
* Only followers receive notifications
2020-04-06 15:26:47 +02:00
Srinandan Pai
14106ce800 Issue-1794: Allow to hide proposals created by admin (#3884) 2020-03-26 15:07:55 +01:00
Javier Martín
9d750dd399 Merge pull request #3917 from rockandror/improve-remote-translations
Improve remote translations
2020-02-26 17:29:14 +01:00
taitus
d853366d38 Add RemoteTranslation validations
- Validate that locale is a valid locale for RemoteTranslation Client.
- RemoteTranslation can only be created for resources that do not have the requested
language translated
2020-02-26 16:47:13 +01:00
Javier Martín
14b1f1b4d3 Merge pull request #3911 from rockandror/dashboard-email
Improve Dashboard emails
2020-02-19 19:06:53 +01:00
taitus
5608424510 Rename all ocurrences from this Setting to new key 2020-02-17 16:30:38 +01:00
Javi Martín
f223b3945f Fix filters for investments without admin/valuator
These filters were only returning investments with valuation open, but
we don't want to do that since the time we changed the interface in
order to allow users to apply several filters at the same time.
2020-02-06 14:33:36 +01:00
Javi Martín
12eee3f5d1 Avoid invalid random titles in dashboard specs
Generating a random title with `Faker::Lorem.sentence` sometimes caused
validation errors in tests because the generated sentence was more than
80 characters long.
2019-11-19 23:32:12 +01:00
Javi Martín
ed6a4a1b95 Don't generate stats for budget polls
The link to show stats for these polls is nowhere to be seen in the
application, and these stats are included in the budget stats, so it
makes sense to restrict access to them.
2019-11-09 19:34:24 +01:00
Javi Martín
f8e6e98d3a Define stats and result permissions with scopes
When defining abilities, scopes cover more cases because they can be
used to check permissions for a record and to filter a collection. Ruby
blocks can only be used to check permissions for a record.

Note the `Budget::Phase.kind_or_later` name sounds funny, probably
because we use the word "phase" for both an an attribute in the budgets
table and an object associated with the budget, and so naming methods
for a budget phase is a bit tricky.
2019-11-09 19:34:21 +01:00
Javi Martín
2029d7baa5 Limit admin access to poll stats and results
There's no reason to allow administrators to check stats and results for
a poll when it isn't finished or when results and stats are not enabled.

Now admins have the same permissions as everyone else.
2019-11-09 19:32:54 +01:00
Javi Martín
8dbad5c3d2 Simplify tests for stats and results permissions
Now these tests look like the other ability tests.
2019-11-09 15:18:58 +01:00
Javi Martín
ea2aeab383 Apply rubocop Rails/FindBy rule everywhere
We didn't detect these cases because by default the rule only searches
for offenses in `app/models/`.
2019-11-08 19:15:04 +01:00
Senén Rodero Rodríguez
a150f2161e Validate inclusion of document type in allowed document types
* Add custom message for inclusion validation to include the allowed values.
* Force user to choose document_type from select lik the one shown at verification form.
* Convert stored document_type to a human readable text
2019-11-07 16:26:19 +01:00
Javi Martín
6bbfb55586 Fix admin permissions for finished budgets
Although we weren't showing links in the views to execute certain
actions, forms could be still sent using a PUT/PATCH pull request to the
controller actions.
2019-11-06 15:35:12 +01:00
Javi Martín
d1d71f0044 Don't allow valuation if cannot edit dossier
We were adding the condition to show the form in the view. However, that
doesn't prevent users from sending a POST/PUT request to the controller
action.

We could add the condition to the controller as well, but since the
`valuate` permission is only used in one place, it's easier to restrict
that permission to valuators who can edit the dossier.
2019-11-05 23:15:16 +01:00
Javi Martín
6db0272575 Fix valuation permissions for finished budgets 2019-11-05 23:13:04 +01:00
Javi Martín
8f87753306 Simplify test for valuator abilities 2019-11-05 23:12:10 +01:00
Javi Martín
b1806dde44 Rename Budget#milestone_tags method
Since budgets now have milestone tags, the name of this method was
confusing and will conflict with the name generated by acts_as_taggable.

Note the new name could be improved too.
2019-11-01 17:12:42 +01:00
Javier Martín
6bcf8ea806 Merge pull request #3806 from consul/remove_votation_types
Remove poll votation types
2019-10-31 13:47:49 +01:00
Mohsin Khan
9fd79cefd4 Don't send newsletters to unconfirmed accounts (Merge #3781) 2019-10-30 21:15:05 +01:00
Javi Martín
af7c37634d Remove poll votation types
Unfortunately this feature wasn't properly reviewed and tested, and it
had many bugs, some of them critical and hard to fix, like validations
being skipped in concurrent requests.

So we're removing it before releasing version 1.1. We might add it back
in the future if we manage to solve the critical issues.

This commit reverts commit 836f9ba7.
2019-10-30 18:48:55 +01:00
Javi Martín
f3df3f4fbc Remove people proposal model
This model isn't used anywhere, since it was created as part of a
feature which couldn't be completed.

This commit reverts commit 46e5d6a9.
2019-10-30 02:26:42 +01:00
Javi Martín
88c70366a7 Apply rubocop rules for trailing commas 2019-10-26 20:10:33 +02:00
Javi Martín
34e66acdda Apply Style/StringLiteralsInInterpolation rubocop rule
We were already using it in most places.
2019-10-26 13:37:36 +02:00
Javi Martín
eafb4018bf Apply Style/CollectionMethods rubocop rule
We were already using `map` and `reduce` almost everywhere.
2019-10-26 13:03:49 +02:00
Javi Martín
d218e09654 Use squiggly heredocs
This is a feature which was introduced in Ruby 2.3, and makes it much
easier to write readable heredocs.
2019-10-26 13:03:48 +02:00
Javi Martín
97e826f2a4 Don't use update_attribute
This method is ambiguous. Sometimes we use it to set invalid data in
tests (which can usually be done with `update_column`), and other times
we use it instead of `update!`.

I'm removing it because, even if sometimes it could make sense to use
it, it's too similar to `update_attributes` (which is an alias for
`update` and runs validations), making it confusing.

However, there's one case where we're still using it: in the
ActsAsParanoidAliases module, we need to invoke the callbacks, which
`update_column` skips, but tests related to translations fail if we use
`update!`. The reason for this is the tests check what happens if we
restore a record without restoring its translations. But that will make
the record invalid, since there's a validation rule checking it has at
least one translation.

I'm not blacklisting any other method which skips validations because we
know they skip validations and use them anyway (hopefully with care).
2019-10-25 23:17:50 +02:00
Javi Martín
bbce3479cf Simplify touching a budget when a phase changes
The `belongs_to` method already has that option, so there's no need to
do it manually in an `after_save` callback.
2019-10-25 23:17:49 +02:00
Javi Martín
35e8e9da31 Fix validations being accidentally skipped in spec
We were trying to test a before_validation call, but the `touch` method
skips validations.
2019-10-25 20:14:40 +02:00
Javi Martín
57ef380379 Add and apply Layout/ExtraSpacing rubocop rule 2019-10-24 18:11:58 +02:00
Javi Martín
9028d82f77 Remove unnecessary enconding magic comment
This comment isn't necessary since Ruby 2.0, where UTF-8 became the
default encoding.

I've found this issue thanks to the EmptyLineAfterMagicComment rubocop
rule.
2019-10-24 17:56:03 +02:00
Javi Martín
db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00
Javi Martín
49c3402833 Use update! instead of update_attributes!
It's what we use almost everywhere, and it's shorter.
2019-10-23 14:39:31 +02:00
Javi Martín
7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00
Javi Martín
431074c99f Add save! method to ActiveModel models
This way we make it clear we expect records to be valid when we save
them, just like we do with ActiveRecord models.
2019-10-23 14:32:42 +02:00
Javi Martín
b4b20e0295 Assign attributes to test invalid updates
Using `update` is a bit ambiguous; when we do it we aren't expressing
whether we expect the update operation to succeed or fail.
2019-10-23 14:32:42 +02:00