Skip to content

Commit

Permalink
preserve cookies from api server
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Oct 24, 2023
1 parent 82b01f8 commit 1914a47
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ group :development, :test do
gem "webmock"
gem "codecov", require: false, group: "test"
gem "simplecov"
gem "dotenv"
end

# application runtime dependencies
Expand All @@ -31,3 +32,4 @@ gem "yaml-safe_load_stream",
git: "https://github.com/wr0ngway/yaml-safe_load_stream.git",
branch: "ruby_3"
gem "async"
gem "faraday-cookie_jar"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GEM
docile (1.4.0)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.6)
e2mmap (0.1.0)
faraday (1.5.1)
faraday-em_http (~> 1.0)
Expand All @@ -58,6 +59,9 @@ GEM
faraday-patron (~> 1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords (>= 0.0.4)
faraday-cookie_jar (0.0.7)
faraday (>= 0.8.0)
http-cookie (~> 1.0.0)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
Expand Down Expand Up @@ -222,6 +226,8 @@ DEPENDENCIES
cloudtruth-client!
codecov
debase
dotenv
faraday-cookie_jar
gem_logger
kubeclient
liquid
Expand Down
4 changes: 3 additions & 1 deletion lib/kubetruth/ctapi.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'uri'
require "cloudtruth-client"
require_relative 'parameter'
require 'faraday-cookie_jar'

module Kubetruth
class CtApi
Expand Down Expand Up @@ -40,7 +41,7 @@ def initialize(environment: "default", tag: nil)
@environments_mutex = Mutex.new
@projects_mutex = Mutex.new
@templates_mutex = Mutex.new

raise ArgumentError.new("CtApi has not been configured") if @@api_key.nil? || @@api_url.nil?
@api_key = @@api_key
@api_url = @@api_url
Expand All @@ -58,6 +59,7 @@ def initialize(environment: "default", tag: nil)
config.api_key = {'ApiKeyAuth' => @api_key}
config.api_key_prefix = {'ApiKeyAuth' => "Api-Key"}
config.logger = logger
config.use(:cookie_jar)
# config.debugging = logger.debug?
@client = CloudtruthClient::ApiClient.new(config)
@client.user_agent = "kubetruth/#{Kubetruth::VERSION}"
Expand Down
130 changes: 130 additions & 0 deletions spec/fixtures/vcr/Kubetruth_CtApi/_cookies/uses_session_cookie.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions spec/kubetruth/ctapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ def create_project_fixture

end

describe "#cookies" do

it "uses session cookie" do
data, status_code, headers = ctapi.apis[:environments].environments_list_with_http_info
cookies = headers["set-cookie"]
session_cookie1 = cookies.match(/sessionid=([^;]+)/)[1]
data, status_code, headers = ctapi.apis[:environments].environments_list_with_http_info
cookies = headers["set-cookie"]
session_cookie2 = cookies.match(/sessionid=([^;]+)/)[1]
expect(session_cookie1).to eq(session_cookie2)
end

end

describe "#environments" do

it "gets environments" do
Expand Down Expand Up @@ -244,7 +258,7 @@ def create_project_fixture
expect(ctapi.template("tone", project: @project_name)).to eq("tmpl1 defaultone")

sleep 2

tag = ctapi.apis[:environments].environments_tags_list(ctapi.environment_id("default"), name: "test_tag").results.first
if tag
ctapi.apis[:environments].environments_tags_update(ctapi.environment_id("default"), tag.id, CloudtruthClient::TagUpdate.new(name: "test_tag", timestamp: Time.now))
Expand All @@ -255,17 +269,17 @@ def create_project_fixture
sleep 2

ctapi.apis[:projects].projects_parameters_values_update(one_param_value.id, @one_param.id, @project_id, value_update: CloudtruthClient::ValueUpdate.new(internal_value: "newdefaultone"))

params = ctapi.parameters(project: @project_name)
expect(params.collect(&:value).sort).to eq(["defaulttwo", "newdefaultone"])
expect(ctapi.template("tone", project: @project_name)).to eq("tmpl1 newdefaultone")

ctapi_tagged = described_class.new(tag: "test_tag")
params = ctapi_tagged.parameters(project: @project_name)
expect(params.collect(&:value).sort).to eq(["defaultone", "defaulttwo"])
expect(ctapi_tagged.template("tone", project: @project_name)).to eq("tmpl1 defaultone")
end

end

end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "bundler/setup"
require 'dotenv/load'
ENV['CLOUDTRUTH_API_KEY'] ||= 'fake_api_key'
require "kubetruth"

Expand Down

0 comments on commit 1914a47

Please sign in to comment.