-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from qedi-r/rspec-tests
Add basic RSpec tests
- Loading branch information
Showing
11 changed files
with
200 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.active_record.maintain_test_schema = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require_relative 'spec_helper' | ||
|
||
RSpec.describe 'App base path tests' do | ||
it 'test_it_has_root_path' do | ||
browser = get '/' | ||
expect(browser.status).to be(200) | ||
expect(browser.body).to match("To set up your device") | ||
end | ||
|
||
it 'test_it_has_device_list_path' do | ||
doc = get_and_parse '/devices' | ||
expect(doc.at('a[href="/devices/new"]').text.strip).to eq('Add New') | ||
end | ||
end | ||
|
||
RSpec.describe 'API path tests' do | ||
it 'test_it_has_api_setup_path' do | ||
_, body = get_json '/api/setup/' | ||
expect(body['message']).to eq('MAC Address not registered') | ||
end | ||
|
||
it 'test_it_has_api_setup_path_with_a_device' do | ||
mac = 'aa:bb:cc:00:00:01' | ||
dev = Device.create!({ | ||
name: "Test Trmnl", mac_address: mac | ||
}) | ||
header("ID", mac) | ||
_, body = get_json '/api/setup/' | ||
expect(body['api_key']).to eq(dev.api_key) | ||
expect(body['friendly_id']).to eq(dev.friendly_id) | ||
expect(body['image_url']).to eq(ENV['BASE_URL'] + '/images/setup/setup-logo.bmp') | ||
expect(body['message']).to eq("Welcome to TRMNL BYOS") | ||
end | ||
end | ||
|
||
RSpec.describe 'Display path tests' do | ||
it 'test_it_has_api_setup_path' do | ||
_, body = get_json '/api/setup/' | ||
expect(body['message']).to eq('MAC Address not registered') | ||
end | ||
|
||
it 'test_it_has_api_setup_path_with_a_device' do | ||
mac = 'aa:bb:cc:00:00:01' | ||
dev = Device.create!({ | ||
name: "Test Trmnl", mac_address: mac | ||
}) | ||
header("ID", mac) | ||
_, body = get_json '/api/setup/' | ||
expect(body['api_key']).to eq(dev.api_key) | ||
expect(body['friendly_id']).to eq(dev.friendly_id) | ||
expect(body['image_url']).to eq(ENV['BASE_URL'] + '/images/setup/setup-logo.bmp') | ||
expect(body['message']).to eq("Welcome to TRMNL BYOS") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require_relative 'spec_helper' | ||
|
||
RSpec.describe 'Display path tests' do | ||
it 'test_it_has_api_display_path' do | ||
_, body = get_json '/api/display/' | ||
expect(body['status']).to eq(404) | ||
end | ||
|
||
it 'test_it_has_a_display_path_with_a_device' do | ||
dev = Device.create!({ | ||
name: "Test Trmnl", mac_address: 'aa:ab:ac:00:00:01' | ||
}) | ||
header("ACCESS_TOKEN", dev.api_key) | ||
_, body = get_json '/api/display/' | ||
expect(body['reset_firmware']).to eq(false) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require_relative 'spec_helper' | ||
|
||
RSpec.describe 'API setup path tests' do | ||
it 'test_it_has_api_setup_path' do | ||
_, body = get_json '/api/setup/' | ||
expect(body['message']).to eq('MAC Address not registered') | ||
end | ||
|
||
it 'test_it_has_api_setup_path_with_a_device' do | ||
mac = 'aa:bb:cc:00:00:01' | ||
dev = Device.create!({ | ||
name: "Test Trmnl", mac_address: mac | ||
}) | ||
header("ID", mac) | ||
_, body = get_json '/api/setup/' | ||
expect(body['api_key']).to eq(dev.api_key) | ||
expect(body['friendly_id']).to eq(dev.friendly_id) | ||
expect(body['image_url']).to eq(ENV['BASE_URL'] + '/images/setup/setup-logo.bmp') | ||
expect(body['message']).to eq("Welcome to TRMNL BYOS") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
ENV['APP_ENV'] = 'test' | ||
ENV['RACK_ENV'] = 'test' | ||
|
||
require 'active_record' | ||
require 'rack/test' | ||
require 'nokogiri' | ||
require 'database_cleaner/active_record' | ||
require_relative "../app" | ||
|
||
|
||
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration | ||
RSpec.configure do |config| | ||
config.include Rack::Test::Methods | ||
|
||
ActiveRecord::Base.establish_connection(:test) | ||
ActiveRecord::Schema.verbose = false | ||
load "db/schema.rb" | ||
|
||
config.expect_with :rspec do |expectations| | ||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true | ||
end | ||
|
||
config.mock_with :rspec do |mocks| | ||
mocks.verify_partial_doubles = true | ||
end | ||
|
||
config.shared_context_metadata_behavior = :apply_to_host_groups | ||
|
||
DatabaseCleaner.strategy = :truncation | ||
config.after do | ||
DatabaseCleaner.clean | ||
end | ||
|
||
def app | ||
Sinatra::Application | ||
end | ||
|
||
def get_and_parse(page, query_params={}, env={}) | ||
get(page, query_params, env) | ||
@doc = Nokogiri::HTML(last_response.body) | ||
return @doc | ||
end | ||
|
||
def get_json(page, query_params={}, env={}) | ||
get(page, query_params, env) | ||
@doc = last_response | ||
expect(@doc.headers['content-type']).to eq("application/json") | ||
return [@doc, JSON.parse(@doc.body)] | ||
end | ||
|
||
def post_json(page, data, params={}, env={}) | ||
post(page, JSON.generate(data), params, env) | ||
@doc = last_response | ||
expect(@doc.headers['content-type']).to eq("application/json") | ||
return [@doc, JSON.parse(@doc.body)] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
To set up your device ... |