Commit Graph

1273 Commits

Author SHA1 Message Date
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
Javi Martín
aace7aea02 Apply Layout/SpaceAroundOperators rubocop rule
We were a bit inconsistent when aligning equal signs vertically.
2019-10-22 17:37:51 +02:00
Javi Martín
6c45d21626 Avoid using Time.now and Date.today in zone tests
We've got a rubocop rule preventing us from using them, and the tests
are easier to read this way.
2019-10-22 17:37:48 +02:00
Javi Martín
68ca29fa8b Convert markdown to HTML on demand
We were converting markdown to HTML every time we saved a record, which
has the same problems as sanitizing HTML before saving it to the
database, particularly because the body of a legislation draft is stored
in a translations table.

Performance-wise this isn't a problem: converting a text with more than
200_000 characters takes about a milisecond on my machine.

Note we need to modify a migration generated by globalize, since the
method `create_translation_table!` would fail now that we don't define
`translates :body_html` in the model.
2019-10-21 21:32:43 +02:00
Javi Martín
7bf4e4d611 Sanitize descriptions in the views
Sanitizing descriptions before saving a record has a few drawbacks:

1. It makes the application rely on data being safe in the database. If
somehow dangerous data enters the database, the application will be
vulnerable to XSS attacks
2. It makes the code complicated
3. It isn't backwards compatible; if we decide to disallow a certain
HTML tag in the future, we'd need to sanitize existing data.

On the other hand, sanitizing the data in the view means we don't need
to triple-check dangerous HTML has already been stripped when we see the
method `auto_link_already_sanitized_html`, since now every time we use
it we sanitize the text in the same line we call this method.

We could also sanitize the data twice, both when saving to the database
and when displaying values in the view. However, doing so wouldn't make
the application safer, since we sanitize text introduced through
textarea fields but we don't sanitize text introduced through input
fields.

Finally, we could also overwrite the `description` method so it
sanitizes the text. But we're already introducing Globalize which
overwrites that method, and overwriting it again is a bit too confusing
in my humble opinion. It can also lead to hard-to-debug behaviour.
2019-10-21 21:32:02 +02:00
Javi Martín
9340d189cb Fix investments search with numbers in their title 2019-10-21 19:27:16 +02:00
Javi Martín
8aa4c630d7 Make search_by_title_or_id behave like a scope
There's no need to pass the collection of results when we use methods
like `where`.
2019-10-21 19:27:15 +02:00
Javier Martín
a9110d23e0 Merge pull request #2214 from consul/related-content-tests
Add tests for related content score
2019-10-20 18:15:45 +02:00
María Checa
c8966b99b0 Added relation score specs 2019-10-20 15:03:05 +02:00
Javi Martín
f5b60e03e1 Don't let valuators update investments
There were some confusing definitions regarding the valuation of budget
investments.

In the controller, `CommentableActions` was included, which includes the
update action.

In the abilities, a valuator was given permission to update an
investment.

However, the action to update an investment didn't work because there is
no route defined to do so.

The ability was defined so valuators could access the "edit" action,
which will not call the "update" action but the "valuate" action. Since
internally "edit" and "update" use the same permission, it worked.

But then we added permission for regular users to update budget
investments, and these permissions were allowing valuators to update
another user's investment.

After this change, everything seems to work properly since we check
authorization in the controller itself instead of using abilities.
2019-10-18 16:24:27 +02:00
denialtorres
bb627a7117 Edit Budget Investment only in accepting phase (#3716)
This way users who made a typo can fix it before the investment is reviewed.
2019-10-18 13:59:14 +02:00
Javi Martín
6a6a8bf365 Fix milestone publication date comparison
We're storing the publication date as datetime in the database, and we
were comparing it to a date, meaning today's milestones were not being
found.
2019-10-10 02:35:20 +02:00
Javi Martín
211398bd9d Use a more expressive name for time zone tests
"With different time zone" didn't clarify which time zone was different
nor what it was different to.
2019-10-10 02:34:51 +02:00
Javi Martín
b66859945e Remove _html suffix from already sanitized texts
Using the `_html` suffix automatically marks texts as HTML safe, so
doing so on sanitized texts is redundant.

Note flash texts are not sanitized the moment they are generated, but
are sanitized when displayed in the view.
2019-10-09 19:46:47 +02:00
Javi Martín
710a1d8357 Split Poll::Booth.search in two methods
We use this method in two different scenarios. In an AJAX request, we
don't want to return every booth if the search is blank. However, in a
normal HTTP GET request, we want to return every record when the search
is empty, as we do everywhere else.

It's possible the behaviour of the AJAX call is unusual, since it
searches all booths, and not just the ones assigned to a poll. If we
changed this behaviour, we could simplify the code and remove the
`quick_search` method.
2019-10-07 14:30:39 +02:00
Javi Martín
871fa020a5 Add and apply ParenthesesAsGroupedExpression rule 2019-10-05 15:03:26 +02:00
Javi Martín
70a07c095f Add and apply Style/BlockDelimiters rubocop rule
We were already using it most of the time, but there were a few places
were we used brackets for multiline blocks.
2019-10-05 14:44:14 +02:00
Javi Martín
ad14636255 Use Tag instead of ActsAsTaggableOn::Tag
It's shorter, it's easier to extend its behaviour, and it's easier to
integrate with other parts of our application, like translations.
2019-10-05 03:38:44 +02:00
Javier Martín
fd4e007ada Merge pull request #3597 from consul/original_heading_id
Add original heading id to investments
2019-10-01 14:52:17 +02:00
Javi Martín
4c5104d03d Avoid unused variables in notification spec
We can change the code a bit so the useless assignment is either part of
the setup (where only another variable was present) or isolated in the
"action" part of the test.
2019-09-30 15:47:13 +02:00
Javi Martín
9f64129be5 Remove isolated useless assignments
These variables can be considered a block, and so removing them doesn't
make the test much harder to undestand.

Sometimes these variables formed the setup, sometimes they formed an
isolated part of the setup, and sometimes they were the part of the test
that made the test different from other tests.
2019-09-30 15:47:13 +02:00
Javi Martín
802be29773 Simplify maximum limit of direct messages specs
Now the tests are easier to understand: when the limit is 3, if you
create 3, the fourth one is invalid. If you create 2, the third one is
valid.
2019-09-30 15:47:13 +02:00
Javi Martín
d09be11a08 Make test for feed limit more explicit
The limit parameter wasn't specified in the test but in the default
value in the database, making the test hard to read.

Since now we've moved the other processes to separate tests, now we can
create four processes using `times` and keep the test simple.
2019-09-30 15:47:13 +02:00
Javi Martín
d410fcbc0e Split scope tests
In the scenario where we want to test scopes and use `match_array`, we
usually declare variables we never use, which raises a warning in the
Ruby interpreter (since the main cause for an unused variable is a
typo).

So I've decided to just split the tests into cases where every record is
returned and cases were no records are returned, just like we do in
other places.

There are several other options we've considered:

1. Don't declare unused variables, but declare the ones we use
2. Prefix unused variables with un underscore
3. Declare just one variable being an array containing all elements, and
access the elements using Array#[]
4. Don't declare any variables, and compare results against attributes
such as titles

None of these options was met with enthusiasm.
2019-09-30 15:46:58 +02:00
Javi Martín
5a84dcb534 Rename unfinished trait to open
So now it's more consistent with the name we use in the rest of the
code.
2019-09-30 15:30:40 +02:00
Javi Martín
4ead96b666 Use let only in the examples using the variables
These variables were only used in the `validations` block; the rest of
the tests re-defined them, making the code hard to follow.
2019-09-30 15:23:52 +02:00
Javi Martín
55c4a953ea Remove let only used once 2019-09-30 15:23:52 +02:00
Javi Martín
d5c0e1c8df Use let to creat users in voter spec
We remove duplication, and we better isolate useless assignments.
2019-09-30 15:23:52 +02:00
Javi Martín
c6808c9c12 Directly assign valuator groups in specs 2019-09-30 15:20:56 +02:00
Javi Martín
05f63ca08d Add trait to create an investment with a valuator 2019-09-30 15:08:27 +02:00
Javi Martín
a9159e0ec8 Directly assign valuators in specs
Creating a record and then assigning an association makes the code even
harder to follow.
2019-09-30 15:08:27 +02:00
Javi Martín
bbf8eebb80 Check lines count instead of IDs in results spec
This is easier to follow
2019-09-30 14:29:15 +02:00
Javi Martín
bd795be80e Check for attribute values in current budget tests
Assigning a variable to each budget we declare results in useless
assignments. We could just delete the three useless assignments and
leave the fourth one, but I find the code easier to read if we use the
name of the budgets to differenciate between them. This way we also keep
the code vertically aligned.
2019-09-30 14:29:15 +02:00
Javi Martín
e2b536e8f8 Simplify adding investments to ballots in specs 2019-09-30 14:10:46 +02:00
Javi Martín
19edf0466b Simplify assigning booth to a voter
Unless we're using the booth assignment for something else in the test,
when creating a voter from booth, specifying the poll and the booth is
enough to create a booth assignment.
2019-09-30 14:10:46 +02:00
Javi Martín
bd73c5f263 Use final trait for poll officer assignments
We were only using it in a few places.

I've left the current `final: true` statement in a few places where
using a trait would break vertical alignment, just in case.
2019-09-30 14:10:46 +02:00
Javi Martín
b431273869 Simplify creating officer assignments in specs
Note we usually cannot make it simple because officer assignments are
usually assigned to both a poll and a booth, and on a certain date.
However, in the few cases where the booth nor the date don't matter, we
can make the code a bit easier to read.
2019-09-30 14:10:45 +02:00