Skip to content

Commit

Permalink
Merge pull request #21 from qedi-r/rspec-tests
Browse files Browse the repository at this point in the history
Add basic RSpec tests
  • Loading branch information
ryanckulp authored Feb 5, 2025
2 parents 5c55ce3 + 4b82af7 commit 9f6c768
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ end

group :development, :test do
gem 'debug'
gem 'nokogiri'
gem "rspec"
gem "rack-test"
gem "database_cleaner-active_record"
end

37 changes: 35 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
database_cleaner-active_record (2.2.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.4.1)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
dotenv (3.1.7)
drb (2.2.1)
forme (2.6.0)
Expand All @@ -62,11 +67,16 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2024.1203)
mini_magick (4.12.0)
mini_portile2 (2.8.8)
minitest (5.25.4)
mustermann (3.0.3)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.4)
pg (1.5.9)
nokogiri (1.18.2)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.18.2-x86_64-darwin)
racc (~> 1.4)
pry (0.15.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand All @@ -80,6 +90,7 @@ GEM
concurrent-ruby (>= 1.1, < 1.4)
mime-types (>= 3.0)
websocket-driver (>= 0.6.0)
racc (1.8.1)
rack (3.1.8)
rack-protection (4.1.1)
base64 (>= 0.1.0)
Expand All @@ -88,13 +99,28 @@ GEM
rack-session (2.1.0)
base64 (>= 0.1.0)
rack (>= 3.0.0)
rack-test (2.2.0)
rack (>= 1.3)
rackup (2.2.1)
rack (>= 3)
rake (13.2.1)
rdoc (6.8.1)
psych (>= 4.0.0)
reline (0.5.12)
io-console (~> 0.5)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.2)
ruby2_keywords (0.0.5)
securerandom (0.4.0)
sinatra (4.1.1)
Expand All @@ -107,6 +133,9 @@ GEM
sinatra-activerecord (2.0.28)
activerecord (>= 4.1)
sinatra (>= 1.0)
sqlite3 (2.5.0)
mini_portile2 (~> 2.8.0)
sqlite3 (2.5.0-x86_64-darwin)
stringio (3.1.2)
tilt (2.6.0)
timeout (0.4.2)
Expand All @@ -124,19 +153,23 @@ PLATFORMS

DEPENDENCIES
activerecord
database_cleaner-active_record (~> 2.2)
debug
dotenv
ferrum!
forme
json
mini_magick (~> 4.12.0)
pg
nokogiri
pry
puma
puppeteer-ruby (~> 0.45.6)
rack-test
rackup
rake
rspec
sinatra-activerecord
sqlite3

BUNDLED WITH
2.5.6
6 changes: 5 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_forme(model, is_edit, attrs={}, options={})
attrs[:method] = :post
options = TailwindConfig.options.merge(options)
if model && model.persisted?
attrs[:action] += "/#{@device.id}"
attrs[:action] += "/#{model.id}" if model.id
options[:before] = -> (form) {
TailwindConfig.before.call(form)
form.to_s << '<input name="_method" value="patch" type="hidden"/>'
Expand Down Expand Up @@ -83,6 +83,10 @@ def devices_form(device)
redirect to('/devices')
end

get '/' do
erb :"index"
end

# FIRMWARE SETUP
get '/api/setup/' do
content_type :json
Expand Down
3 changes: 3 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ production:
# username: your_username
# password: your_password
<% end %>
test:
adapter: sqlite3
database: ":memory:"
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.active_record.maintain_test_schema = true
54 changes: 54 additions & 0 deletions spec/app_spec.rb
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
17 changes: 17 additions & 0 deletions spec/display_spec.rb
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
21 changes: 21 additions & 0 deletions spec/setup_spec.rb
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
57 changes: 57 additions & 0 deletions spec/spec_helper.rb
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
1 change: 1 addition & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To set up your device ...

0 comments on commit 9f6c768

Please sign in to comment.