Skip to content

Commit

Permalink
Add login method to test if credentials are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
bforma committed Feb 1, 2024
1 parent ceaf8c7 commit 4c4fc7b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/easee/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def initialize(
@encryptor = encryptor
end

# https://developer.easee.cloud/reference/post_api-accounts-login
def login
with_error_handling do
request_access_token
end
end

# https://developer.easee.cloud/reference/post_api-chargers-id-unpair
def unpair(charger_id:, pin_code:)
post("/api/chargers/#{charger_id}/unpair", query: { pinCode: pin_code })
Expand Down
50 changes: 50 additions & 0 deletions spec/easee/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,56 @@
end
end

describe "#login" do
it "returns the access token when credentials are valid" do
user_name = "easee"
password = "money"

stub_request(:post, "https://api.easee.cloud/api/accounts/login")
.with(
body: { userName: user_name, password: }.to_json,
)
.to_return(
status: 200,
body: { accessToken: "T123" }.to_json,
headers: { "Content-Type": "application/json" },
)

client = Easee::Client.new(user_name:, password:)

expect(client.login).to eq("accessToken" => "T123")
end

it "raises InvalidCredentials when the credentials are invalid" do
user_name = "test"
password = "wrong"

stub_request(:post, "https://api.easee.cloud/api/accounts/login")
.with(
body: { userName: user_name, password: }.to_json,
)
.to_return(
status: 400,
body: {
errorCode: 100,
errorCodeName: "InvalidUserPassword",
type: nil,
title: "Username or password is invalid",
status: 400,
detail: "[Empty in production]",
instance: nil,
extensions: {},
}.to_json,
headers: { "Content-Type": "application/json" },
)

client = Easee::Client.new(user_name:, password:)

expect { client.login }
.to raise_error(Easee::Errors::InvalidCredentials)
end
end

describe "#pair" do
it "pairs a new charger" do
token_cache = ActiveSupport::Cache::MemoryStore.new
Expand Down

0 comments on commit 4c4fc7b

Please sign in to comment.