|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Bls::Core::V2 do |
| 4 | + describe "#base_url" do |
| 5 | + context "when the environment is production" do |
| 6 | + it "returns the correct url" do |
| 7 | + api = Bls::Core::V2.new(environment: "production") |
| 8 | + |
| 9 | + result = api.base_url |
| 10 | + |
| 11 | + expect(result).to eq("https://core.batchlinesolutions.com") |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + context "when the environment is not production" do |
| 16 | + it "returns the correct url" do |
| 17 | + api = Bls::Core::V2.new(environment: "staging") |
| 18 | + |
| 19 | + result = api.base_url |
| 20 | + |
| 21 | + expect(result).to eq("https://core.batchlinesolutions.dev") |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe "#client" do |
| 27 | + it "returns a api client" do |
| 28 | + api = Bls::Core::V2.new |
| 29 | + build_stubbed_authenticator(api) |
| 30 | + |
| 31 | + result = api.client |
| 32 | + |
| 33 | + expect(result).to respond_to(:post) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "#authenticate" do |
| 38 | + it "builds a authenticator" do |
| 39 | + api = Bls::Core::V2.new |
| 40 | + build_stubbed_authenticator(api) |
| 41 | + |
| 42 | + api.authenticate! |
| 43 | + |
| 44 | + expect(Bls::Core::V2::Authenticator).to have_received(:build).once |
| 45 | + end |
| 46 | + |
| 47 | + it "authenticates" do |
| 48 | + api = Bls::Core::V2.new |
| 49 | + authenticator = build_stubbed_authenticator(api) |
| 50 | + |
| 51 | + api.authenticate! |
| 52 | + |
| 53 | + expect(authenticator).to have_received(:save!).once |
| 54 | + end |
| 55 | + |
| 56 | + context "when the authentication is present" do |
| 57 | + it "is authenticated" do |
| 58 | + api = Bls::Core::V2.new |
| 59 | + build_stubbed_authenticator(api) |
| 60 | + |
| 61 | + api.authenticate! |
| 62 | + |
| 63 | + expect(api).to be_authenticated |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe "reset authentication" do |
| 69 | + it "removes the present authentication" do |
| 70 | + auth = Bls::Core::Authorization::Basic.new(token: "1234") |
| 71 | + api = Bls::Core::V2.new(authentication: auth) |
| 72 | + |
| 73 | + result = api.reset_authentication |
| 74 | + |
| 75 | + expect(result.authentication).to be_blank |
| 76 | + end |
| 77 | + |
| 78 | + it "is not authenticated" do |
| 79 | + auth = Bls::Core::Authorization::Basic.new(token: "1234") |
| 80 | + api = Bls::Core::V2.new(authentication: auth) |
| 81 | + |
| 82 | + result = api.reset_authentication |
| 83 | + |
| 84 | + expect(result).not_to be_authenticated |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe ".build" do |
| 89 | + it "sets the environment" do |
| 90 | + config = build_config(environment: "preview") |
| 91 | + |
| 92 | + result = Bls::Core::V2.build(config: config) |
| 93 | + |
| 94 | + expect(result.environment).to eq("preview") |
| 95 | + end |
| 96 | + |
| 97 | + it "sets the username" do |
| 98 | + config = build_config(username: "secretUsername") |
| 99 | + |
| 100 | + result = Bls::Core::V2.build(config: config) |
| 101 | + |
| 102 | + expect(result.username).to eq("secretUsername") |
| 103 | + end |
| 104 | + |
| 105 | + it "sets the password" do |
| 106 | + config = build_config(password: "secretPassword") |
| 107 | + |
| 108 | + result = Bls::Core::V2.build(config: config) |
| 109 | + |
| 110 | + expect(result.password).to eq("secretPassword") |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + def build_config(environment: "staging", username: "string", |
| 115 | + password: "string") |
| 116 | + OpenStruct.new(environment: environment, username: username, |
| 117 | + password: password) |
| 118 | + end |
| 119 | + |
| 120 | + def build_stubbed_authenticator(api) |
| 121 | + authenticator = Bls::Core::V2::Authenticator.build(api) |
| 122 | + authentication = authenticator.authentication |
| 123 | + client = Bls::Core::ApiClient.new(authentication: authentication) |
| 124 | + allow(Bls::Core::V2::Authenticator).to receive(:build). |
| 125 | + and_return(authenticator) |
| 126 | + allow(authenticator).to receive(:save!).and_return(client) |
| 127 | + authenticator |
| 128 | + end |
| 129 | +end |
0 commit comments