Skip to content

Commit 3764cce

Browse files
committed
Add moonraker service tests
1 parent e39dfb6 commit 3764cce

6 files changed

Lines changed: 287 additions & 1 deletion

File tree

app/services/print/moonraker_service.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def ok?
2020

2121
def upload(file:, start_print: true)
2222
raise ArgumentError unless file.mime_type.to_sym == :gcode
23-
connection.post(upload_uri, payload(file: file, start_print: start_print), headers)
23+
raise PrintHost::NotReady unless ok?
24+
response = connection.post(upload_uri, payload(file: file, start_print: start_print), headers)
25+
response.success?
2426
end
2527

2628
private

spec/cassettes/Print_MoonrakerService/with_bad_API_key/doesn_t_upload_file.yml

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/Print_MoonrakerService/with_bad_API_key/flags_connection_error.yml

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/Print_MoonrakerService/with_good_connection_details/uploads_a_file.yml

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/Print_MoonrakerService/with_good_connection_details/verifies_connection.yml

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Print::MoonrakerService, :after_first_run, :vcr do
4+
let(:endpoint) { ENV.fetch("MOONRAKER_ENDPOINT", "http://klipper.example.com") }
5+
let(:credentials) { ENV.fetch("MOONRAKER_API_KEY", "fake_api_key") }
6+
let(:print_host) {
7+
create :print_host,
8+
protocol: "moonraker",
9+
name: "Moonraker",
10+
endpoint: endpoint,
11+
credentials: credentials
12+
}
13+
let(:file) { create :model_file, filename: "test.gcode" }
14+
15+
before do
16+
VCR.configure do |c|
17+
c.filter_sensitive_data("<API_KEY>") { credentials }
18+
c.filter_sensitive_data("<ENDPOINT>") { endpoint }
19+
end
20+
end
21+
22+
context "with good connection details" do
23+
it "verifies connection" do
24+
expect(print_host.service.ok?).to be true
25+
end
26+
27+
it "uploads a file" do
28+
expect(print_host.service.upload(file: file, start_print: true)).to be true
29+
end
30+
end
31+
32+
context "with bad API key" do
33+
let(:credentials) { "bad_api_key" }
34+
35+
it "flags connection error" do
36+
expect(print_host.service.ok?).to be false
37+
end
38+
39+
it "doesn't upload file" do
40+
expect{ print_host.service.upload(file: file, start_print: true)}.to raise_error(PrintHost::NotReady)
41+
end
42+
end
43+
44+
context "with bad endpoint" do
45+
let(:endpoint) { "http://dev.null.local" }
46+
47+
it "flags connection error" do
48+
expect(print_host.service.ok?).to be false
49+
end
50+
51+
it "doesn't upload file" do
52+
expect{ print_host.service.upload(file: file, start_print: true)}.to raise_error(PrintHost::NotReady)
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)