Commit Graph

260 Commits

Author SHA1 Message Date
Javi Martín
fb88e0b77c Fix number of new notifications
We were displaying the total number of notifications with a message "You
have N unread notifications", but were using the total number of
notifications instead of the unread ones.
2021-02-18 14:45:48 +01:00
Javi Martín
4c289f2489 Simplify notification item HTML
Other than simplifying the view, we can write tests using `click_link`,
which makes the tests more robust. Clicking the `.icon-notification`
element was causing some tests to fail when defining a window height of
750 pixels in the `admin_budgets` branch.
2021-02-16 23:21:51 +01:00
Javi Martín
7489f16633 Try to avoid exception after flaggable tests
We had one exception running test suite [1]:

```
  1) Commenting legislation questions Merged comment threads View
comments of annotations in an included range
     Failure/Error: count: annotation.comments.roots.count) %>

     ActionView::Template::Error:
       PG::ProtocolViolation: ERROR:  bind message supplies 0
parameters, but prepared statement "" requires 2
       : SELECT COUNT(*) FROM "comments" WHERE "comments"."hidden_at" IS
NULL AND "comments"."commentable_id" = $1 AND
"comments"."commentable_type" = $2 AND "comments"."ancestry" IS NULL
```

Debugging shows this test was run right after the flaggable specs.
There's a chance these exceptions take place because the test is
accessing the database after the browser (chromedriver) process has
already accessed the database.

This is just an attempt at fixing the issue; I don't know whether these
changes will fix it since I've only seen this exception on Github
Actions (never on my machine). Worst case scenario, we're encouraging
good practices of system tests: test things from the user's point of
view.

[1] https://github.com/consul/consul/runs/1856245992
2021-02-08 20:23:08 +01:00
taitus
1a58fcf2a2 Make tests independent of Setting["org_name"]
So tests won't fail when an institution changes the default organization
name.

The tests are also easier to understand now, since it's more obvious
where the "CONSUL" text is coming from.
2020-12-08 18:56:33 +01:00
Javi Martín
3da4ee00b8 Simplify tests requiring admin login
We were repeating the same code over and over (with a few variants) to
setup tests which require an administrator. We can use a tag and
simplify the code.
2020-12-02 15:33:19 +01:00
Javi Martín
99dad7a7b6 Don't mix links and actions in an admin table
In some tables, we had "actions", and some columns were also links
pointing to some places. Having both of them at the same time is
confusing, particularly since traditionally the links in the columns
pointed to the same place as some of the actions (although that's not
the case since commit 48db31cd).

We're still keeping links in tables which don't have an action column.
For instance, the proposals table has a "select" button which would be
harder to use if we had action buttons next to it.
2020-11-03 14:58:02 +01:00
Javi Martín
74d4fcd087 Fix RSpec/LetSetup rubocop offense
It was detected after updating rubocop-rspec.
2020-10-25 14:26:00 +01:00
Javi Martín
d2d95c8df7 Remove unused variables
Detected thanks to the RSpec/LetSetup rule after updating rubocop-rspec.
2020-10-25 14:26:00 +01:00
Javi Martín
dd7d6440ec Add and apply Capybara/VisibilityMatcher rule
This rule was added in rubocop-rspec 1.39.0. The `visible: false` option
is equivalent to `visible: :all`, but we generally use it meaning
`visible: :hidden` for positive expectations and `visible: :all` for
negative expectations.

The only exceptations are tests where we count the number of map icons
present. I'm assuming in this case we care about the number of map icons
independently on whether they are currently shown in the map, so I'm
keeping the `visible: :all` behavior in this case.
2020-10-25 14:23:53 +01:00
Javi Martín
6088334dbf Remove redundant visibility matcher usages
By default, Capybara only finds visible elements, so adding the
`visible: true` option is usually redundant.

We were using it sometimes to make it an obvious contrast with another
test using `visible: false`. However, from the user's perspective, we
don't care whether the element has been removed from the DOM or has been
hidden, so we can just test that the visible selector can't be found.

Besides, using `visible: false` means the test will also pass if the
element is present and visible. However, we want the test to fail if the
element is visible. That's why a couple of JavaScript-dependant tests
were passing even when JavaScript was disabled.
2020-10-25 14:23:53 +01:00
Javi Martín
b27d0a8e92 Add and apply ConstantDefinitionInBlock rule
This rule was added in Rubocop 0.91.0. A similar rule named
LeakyConstantDeclaration was added in rubocop-rspec 1.34.0.

Note using the FILENAMES constant did not result in an offense using the
ConstantDefinitionInBlock rule but did result in an offense using the
LeakyConstantDeclaration rule. I've simplified the code to get rid of
the constant; not sure why we were adding a constant with `||=` in the
middle of a spec.
2020-10-23 12:04:22 +02:00
Senén Rodero Rodríguez
a986028794 Get map center longitude from correct place 2020-09-14 14:54:30 +02:00
Senén Rodero Rodríguez
b9ce68bc82 Set marker coordinates as map center when map location fields has valid coordinates
When a user recovers a page from browser history where placed a
marker in different map pane (visible map layer) marker was
successfully added to the map but the map center is the one
defined at Settings map properties so the marker was not visible
to the user.

Now when map_location form has valid coordinates we use them
instead of default map center settings. This will avoid the user to
have to rellocate the marker (or find the correct pane where the
marker was added) if already placed.
2020-08-12 10:10:58 +02:00
Senén Rodero Rodríguez
6aa94a787c Use map location form latitude, longitude and zoom when valid
When using an editable map is better to load marker latitude, longitude and
map zoom from form fields so we can show the marker at latest position defined
by user when the page was restored from browser history.

To reproduce this behavior:
0. Undo this commit
1. Go to new proposal page
2. Place the proposal map marker
3. Go away to any other page
4. Restore new proposal page from browser history.

At this point you should not see the recently placed marker.

The same thing happens when editing a proposal.
2020-08-12 10:10:58 +02:00
Senén Rodero Rodríguez
289426c1c3 Destroy maps before leaving the current page
If we do not do this a map could be initialized twice times or more
when restoring a page with a map causing weird UI effects and
loading some map layers also twice times or more.

Need to add a maps array to be able to store all initialized
(visible) maps so we can destroy them when needed. Notice that
we are destroying maps also when admin settings tabs changes
(only visible ones), this is again to avoid to re-initialize map more
than once when users navigate through settings tabs, another
option to the settings issue could be to detect if the map was
already initialized to skip uneeded initialization.
2020-08-12 10:10:58 +02:00
Javi Martín
4f30720593 Fix flagging/unflagging in the admin section
We weren't adding the HTML id our JavaScript expects, and so the page
didn't update the flag element.
2020-07-07 23:39:21 +02:00
Javi Martín
014ccd8374 Use shared specs to flag comments 2020-07-07 23:39:21 +02:00
Javi Martín
91da038b27 Extract shared tests to flag/unflag a record 2020-07-07 22:56:17 +02:00
Javi Martín
7563b7f4d1 Simplify polymorphic routes in shared specs
Now we get rid of the "hierarchy" methods and use standard Rails methods
except in the routes definitions themselves.
2020-06-15 11:54:05 +02:00
Javi Martín
334b57501b Simplify uses of polymorphic admin nested routes 2020-06-11 18:39:57 +02:00
Javi Martín
f5c8d5eea6 Submit the form after the image is attached
We were submitting the form without checking the AJAX request to attach
the image had finished, so sometimes two requests were executed at the
same time. Sometimes this made InvisibleCaptcha to go crazy and report
the form was submitted too quickly.

Checking the first AJAX request has finished before submitting the form
solves the problem.
2020-05-14 00:12:14 +02:00
Javi Martín
dddd5dd808 Check the DOM after attaching file has succeeded
We were checking `expect_document_has_title(0, "My Title")`, which was
already true before the AJAX request generated by `attach_file` had
finished.

That meant the AJAX request sometimes was handled after this test had
finished, affecting the following test and causing it to fail because
its cookie was overwritten and so `current_user` was set to `nil`.

In the test checking the filename is present, a similar scenario was
taking place: we were updating the `.file-name` element in the `change`
event of `fileupload` (using `App.Documentable.setFilename`); that is,
when the AJAX request started. And so the test passed before the request
was finished, causing the same issue.
2020-05-13 18:55:45 +02:00
Javi Martín
61a63ddd59 Use label text instead of ID to fill in comments
This way tests are easier to read (and easier to write).
2020-05-12 23:57:57 +02:00
Javi Martín
5d362ced1f Fix duplicate HTML IDs in comment forms
Since there were many on the page, the resulting HTML was invalid.
2020-05-12 23:57:57 +02:00
Javi Martín
9427f01442 Use system specs instead of feature specs
We get rid of database cleaner, and JavaScript tests are faster because
between tests we now rollback transactions instead of truncating the
database.
2020-04-24 15:43:54 +02:00
Javi Martín
95a90b1895 Simplify method to calculate document max size
Since we're only doing the convertion from bytes to megabytes in one
place, IMHO adding an extra method makes the code harder to read.

This way we don't have do include the DocumentsHelper in the specs
anymore, reducing the risk of possible method naming collisions.
2020-04-16 12:08:09 +02:00
Javi Martín
2cd4696244 Don't include unneeded helpers in tests
Including them might lead to conflicts since two methods might have the
same name. For example, we're getting some exceptions when taking
screenshots of a failing test, because the method `image_path` from
`ActionView::Helpers::AssetUrlHelper` has the same name as a method used
to save the screenshot.

Besides, we were including all helpers in places were only the `dom_id`
method is used, and in other places where no helper methods were used at
all. So we can just invoke `ActionView::RecordIdentifier.dom_id`
directly.
2020-04-16 12:08:09 +02:00
Javi Martín
7491f44d54 Remove unused shared specs
These tests aren't used since commit 4db54092.
2020-04-16 00:00:38 +02:00
Javi Martín
2cdc6a1b1b Check CKEditor is filled properly in tests
It looks like sometimes, particularly when the first thing we do after
loading a page is filling the CKEditor fields and submitting the form,
CKEditor doesn't have enough time to format the text, and so it's sent
as plain text instead of HTML. This behaviour can be reproduced on my
local machine after upgrading to Rails 5.1, with the test "Admin Active
polls Add" failing 100% of the time.

Checking CKEditor has been filled in correctly solves the issue.
2020-04-10 17:11:56 +02:00
Javi Martín
971571b54b Simplify testing followables flash messages
Checking the whole text is tricky because the text has a `<br>` tag, and
now Capybara doesn't normalize whitespace by default anymore.

Here are a couple more options we could use:

```
expect(page).to have_content strip_tags(message.gsub(/\s*<br>\s*/,"\n"))

expect(page).to have_content strip_tags(message), normalize_ws: true
```

But then developers would wonder why we're doing all this, and would
need an extra effort to fully understand the test.

Since the tests are only checking the presence of the flash message,
checking a relevant part of the test is enough, works with any version
of Capybara, and makes the test easy to follow.
2020-04-06 20:11:44 +02: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
taitus
086e38c969 Improve display remote translation button
- Do not display remote translations button when API key is not configured
2020-02-26 12:30:17 +01:00
Senén Rodero Rodríguez
5fa02f604b Fix specs after new Deutsch translations
New deutsch translations of remote translations
interface have broke these specs where we were
using English translations at specs to do the checks
while the spec interface was in deutsch and now we
have deutsch translations for the interface application
is not returning english fallbacks anymore and a lot of
specs of this file fails.

This commits also changes the alternative language
used at spec from deutsch to spanish which is
maintaned by code not through Crowdin, so if any
developer update current spanish translations for the
user interface this specs will fail.
2019-12-18 16:50:48 +01:00
Javi Martín
849c081a1b Simplify the way we attach documents in tests
Now we do it the same way we attach images in `nested_imageable`.

Now we don't need to execute some JavaScript in the test, which by the
way was causing an error when upgrading to jQuery 3.
2019-11-07 15:58:49 +01:00
Javi Martín
d015362299 Remove obsolete poltergeist code
We moved to chromedriver a long time ago.
2019-11-07 15:58:49 +01:00
Javi Martín
ac6d50e06b Remove tracker role
The current tracking section had a few issues:

* When browsing as an admin, this section becomes useless since no
investments are shown
* Browsing investments in the admin section, you're suddenly redirected
to the tracking section, making navigation confusing
* One test related to the officing dashboard failed due to these changes
and had been commented
* Several views and controller methods were copied from other sections,
leading to duplication and making the code harder to maintain
* Tracking routes were defined for proposals and legislation processes,
but in the tracking section only investments were shown
* Probably many more things, since these issues were detected after only
an hour reviewing and testing the code

So we're removing this untested section before releasing version 1.1. We
might add it back afterwards.
2019-11-01 20:08:46 +01:00
Javi Martín
8e3bfa0d72 Apply Style/RedundantReturn rubocop rule
We were already following this pattern most of the time.
2019-10-26 13:26:35 +02:00
Javi Martín
57ef380379 Add and apply Layout/ExtraSpacing rubocop rule 2019-10-24 18:11:58 +02:00
Javi Martín
d938077813 Remove empty lines around if and when body
Unfortunately, there doesn't seem to be a Rubocop rule for this one.
I've found these empty lines thanks to Rufo.
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
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
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
6b1864fbcd Sanitize translations instead of using _html
Using the `_html` suffix in an i18n key is the same as using `html_safe`
on it, which means that translation could potentially be used for XSS
attacks.
2019-10-09 19:46:47 +02:00
Javi Martín
447c970eb3 Remove unused block arguments
There are more cases where we have unused block arguments, but I'm only
changing the obvious ones.
2019-10-05 15:03:26 +02:00
taitus
11d8bd6a3d Stub the response to an external request 2019-10-01 13:20:09 +02:00
Javi Martín
5ff1162038 Simplify creating follows in specs
While in theory we wouldn't need to use the `transient` nor the
`after(:create)` because there's already a `has_many :through`
association with followers, Factory Bot / ActiveRecord don't
automatically associate the followable, resulting in an invalid record
exception.
2019-09-30 13:34:31 +02:00
Javi Martín
dcc9a6b561 Use symbols instead of constants for behaviours
This way we can be more flexible about the factory we can pass as
parameter.
2019-09-29 22:31:13 +02:00
Javi Martín
7b0771106e Remove isolated useless assignments
These variables are not surrounded by other assignments, and so they can
safely be removed without making it harder to read the code vertically.
2019-09-25 12:43:44 +02:00
Javi Martín
5cbd160f11 Move login_as after creating data for a test
It doesn't really affect the tests, but it helps isolate useless
assignments, and we already did it this way 94% of the time.
2019-09-25 12:43:44 +02:00
Javi Martín
175163adb6 Use named parameter in mappable shared example
Boolean arguments are harder to read, so we added a useless variable.
This is the perfect scenario for a named parameter.
2019-09-25 12:43:44 +02:00