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

Create post_test.rb to test posting at /post #5605

Merged
merged 22 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/views/editor/rich.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<input
class="form-control input-lg"
type="text"
id="title-input"
placeholder="Title"
value="<%= if @node then @node.title else params[:title] end %>"
/>
Expand Down Expand Up @@ -425,4 +426,4 @@
}
console.log("Draft:", editor.data.draft);
}
</script>
</script>
2 changes: 1 addition & 1 deletion app/views/user_sessions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<input type="hidden" name="hash_params" value="" />

<div class="input-group-inline">
<button class="btn btn-primary btn-lg" type="submit" tabindex="3"><%= t('user_sessions.new.log_in') %></button>
<button id="login-button" class="btn btn-primary btn-lg" type="submit" tabindex="3"><%= t('user_sessions.new.log_in') %></button>

<div class="form-check-inline">
<label class="form-check-label" style="margin-left:12px;">
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/revisions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ scraped_image:
nid: 27
uid: 2
title: Post test review 4
body: "Lots of text and then an image: ![image preview](/url/to/image.png)"
body: "Lots of text and then an image: ![image preview](/images/pl.png)"
timestamp: <%= DateTime.new(2018,8,20).to_i %>

search_trawl:
Expand Down Expand Up @@ -340,4 +340,4 @@ purple_air_with_hyphen:
title: "This is purple-air with hyphen"
body: "Is Purple-air searched with hyphens or without hyphens?"
timestamp: <%= DateTime.new(2019,2,20).to_i %>


56 changes: 56 additions & 0 deletions test/system/post_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "application_system_test_case"
# https://guides.rubyonrails.org/testing.html#implementing-a-system-test

class PostTest < ApplicationSystemTestCase

def setup
activate_authlogic
end

test 'posting from the editor' do
visit '/'

This comment was marked as resolved.


click_on 'Login'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Here we are visiting login but on line 29 we get the message
    User was successfully created. Can you please explain this code snippet?
  2. We have same integration test here
    test 'redirect to login page if user is not logged in and then redirect back to desired page after login' do
    get '/post?tags=question:question&template=question'
    follow_redirect!
    assert_equal '/login', path
    post '/user_sessions', params: { user_session: { username: users(:jeff).username, password: 'secretive' } }
    follow_redirect!
    assert_equal '/post?tags=question:question&template=question', request.fullpath
    end
    so one of these will become redundant.
  3. In theory of Software Testing at my university, we are taught that System tests are written to validate the non-functional parameters like server load time, access time between end nodes etc. Are we also going to do those amazing stuff later on?
  4. I am having difficulty in installing the prerequisites for System tests. Can you please tell a good reference? I searched a lot but none worked during past one week.
  5. How can we use Recaptcha or Spamaway in system test? Any idea. You may require them in this pr too in case we want assert_selector('#notice', 'User was successfully created.').

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree on redundant, but I'm not sure how to create a session we can use to test logged-in stuff. I tried doing as we do in other tests, with UserSession.new() or whatever, but I think it literally isn't connecting that session we make in the back-end to an actual logged in session that Capybara can interact with on headless chrome! So, i'm trying to log in manually here... how else ought we to log in? Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we also going to do those amazing stuff later on?

My biggest interests in system tests are:

  1. testing the interaction of our complex JS with the Rails app, end-to-end, because things keep breaking here
  2. seeing screenshots!

But yeah... lots of other possibilities.

fill_in("username-login", with: "Bob")
fill_in("password-signup", with: "secretive")
click_on "Log in"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are logging in first and then going to '/post. I think it should be go to the '/post being not logged in then the person is redirected to the '/login' page then person logs in.....

What do you suggest Jeff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, good idea... let me try...


visit '/post'

fill_in("Title", with: "My new post")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill_in(x, with: Y)
I can't get what is x? X is supposed to be class or id or what? I cannot find id with Title.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X is ... i can't remember the term... maybe a locator? It's a Capybara term. I believe it can take the id, the name, or... not sure, and there's an order to it too: https://github.com/teamcapybara/capybara#the-dsl


el = find(".wk-wysiwyg") # rich text input
el.set("All about this interesting stuff")

assert_page_reloads do

find('.ple-publish').click
assert_selector('h1', text: "My new post")
assert_selector('#content', text: "All about this interesting stuff")
assert_selector('.alert-success', text: "×\nSuccess! Thank you for contributing open research, and thanks for your patience while your post is approved by community moderators and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so.")

end

end

# Utility methods:

def assert_page_reloads(message = "page should reload")
page.evaluate_script "document.body.classList.add('not-reloaded')"
yield
if has_selector? "body.not-reloaded"
assert false, message
end
end

def assert_page_does_not_reload(message = "page should not reload")
page.evaluate_script "document.body.classList.add('not-reloaded')"
yield
unless has_selector? "body.not-reloaded"
assert false, message
end
page.evaluate_script "document.body.classList.remove('not-reloaded')"
end

end

2 changes: 1 addition & 1 deletion test/unit/node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def setup

test 'should show scraped image' do
node = nodes(:scraped_image)
assert_equal '/url/to/image.png', node.scraped_image
assert_equal '/images/pl.png', node.scraped_image
end

test 'contribution graph making' do
Expand Down