|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require_relative "../lib/svix" |
| 4 | +require 'subprocess' |
4 | 5 |
|
5 | | -token = ENV["SVIX_TOKEN"] |
6 | | -server_url = ENV["SVIX_SERVER_URL"] |
7 | | -test_client = nil |
| 6 | +SVIX_ORG_ID = "org_svix_ruby_tests" |
| 7 | +DOCKER_COMPOSE_DIR = File.join(File.dirname(__FILE__), '..', '..', 'client-integration-tests') |
8 | 8 |
|
9 | | -if token.nil? || server_url.nil? |
10 | | - warn("Unable to instantiate test client without both `SVIX_TOKEN` and `SVIX_SERVER_URL`") |
11 | | -else |
12 | | - opts = Svix::SvixOptions.new(false, server_url) |
13 | | - test_client = Svix::Client.new(token, opts) |
| 9 | +def docker_compose(*args) |
| 10 | + args = ['docker', 'compose', *args] |
| 11 | + process = Subprocess::Process.new(args, {cwd: DOCKER_COMPOSE_DIR, stdout: Subprocess::PIPE, stderr: Subprocess::PIPE}) |
| 12 | + stdout, stderr = process.communicate |
| 13 | + status = process.wait |
| 14 | + if !status.success? |
| 15 | + $stderr.puts "#{args} failed with status #{status}" |
| 16 | + $stderr.puts stderr |
| 17 | + raise "Called process failed" |
| 18 | + end |
| 19 | + stdout |
| 20 | +end |
| 21 | + |
| 22 | +def docker_generate_token |
| 23 | + raw_output = docker_compose('exec', '-T', 'backend', 'svix-server', 'jwt', 'generate', SVIX_ORG_ID) |
| 24 | + if (md = /Token \(Bearer\): (.*)/.match(raw_output)) |
| 25 | + md[1] |
| 26 | + else |
| 27 | + raise "Failed to generate JWT: #{raw_output}" |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +def docker_compose_port_for(service_name, container_port) |
| 32 | + docker_compose('ps', '--format=json').split("\n").filter_map do |line| |
| 33 | + json = JSON.parse(line) |
| 34 | + if json["Service"] == service_name |
| 35 | + if port_row = json["Publishers"].find { |row| row["TargetPort"] == container_port } |
| 36 | + port_row["PublishedPort"] |
| 37 | + end |
| 38 | + end |
| 39 | + end.first |
14 | 40 | end |
15 | 41 |
|
| 42 | + |
16 | 43 | RSpec.describe Svix::Client do |
17 | | - describe "Endpoint CRUD", {if: !test_client.nil?} do |
| 44 | + before :all do |
| 45 | + if ENV["SVIX_TOKEN"].nil? |
| 46 | + docker_compose "up", "-d", "--quiet-pull" |
| 47 | + $token = docker_generate_token |
| 48 | + $server_url = "http://localhost:#{docker_compose_port_for("backend", 8071)}" |
| 49 | + else |
| 50 | + $token = ENV["SVIX_TOKEN"] |
| 51 | + $server_url = ENV["SVIX_SERVER_URL"] |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + after :all do |
| 56 | + if ENV["SVIX_TOKEN"].nil? |
| 57 | + docker_compose("down", "-v") |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + before :each do |
| 62 | + if ENV["SVIX_TOKEN"].nil? |
| 63 | + docker_compose('exec', '-T', 'backend', 'svix-server', 'wipe', '--yes-i-know-what-im-doing', SVIX_ORG_ID) |
| 64 | + end |
| 65 | + |
| 66 | + opts = Svix::SvixOptions.new(false, $server_url) |
| 67 | + @test_client = Svix::Client.new($token, opts) |
| 68 | + end |
| 69 | + |
| 70 | + describe "Endpoint CRUD" do |
18 | 71 | it "seems to work okay" do |
19 | | - app = test_client.application.create(Svix::ApplicationIn.new(name: "App")) |
| 72 | + app = @test_client.application.create(Svix::ApplicationIn.new(name: "App")) |
20 | 73 |
|
21 | 74 | begin |
22 | | - test_client.event_type.create(Svix::EventTypeIn.new(name: "event.started", description: "Something started")) |
| 75 | + @test_client.event_type.create(Svix::EventTypeIn.new(name: "event.started", description: "Something started")) |
23 | 76 | rescue Svix::ApiError => err |
24 | 77 | # Conflicts are expected from test run to test run, but other statuses are not. |
25 | 78 | expect(err.code).to(eq(409)) |
26 | 79 | end |
27 | 80 |
|
28 | 81 | begin |
29 | | - test_client.event_type.create(Svix::EventTypeIn.new(name: "event.ended", description: "Something ended")) |
| 82 | + @test_client.event_type.create(Svix::EventTypeIn.new(name: "event.ended", description: "Something ended")) |
30 | 83 | rescue Svix::ApiError => err |
31 | 84 | # Conflicts are expected from test run to test run, but other statuses are not. |
32 | 85 | expect(err.code).to(eq(409)) |
33 | 86 | end |
34 | 87 |
|
35 | | - ep = test_client.endpoint.create( |
| 88 | + ep = @test_client.endpoint.create( |
36 | 89 | app.id, |
37 | 90 | Svix::EndpointIn.new(url: "https://example.svix.com/", channels: %w[ch0 ch1]) |
38 | 91 | ) |
39 | 92 |
|
40 | 93 | expect(ep.channels.to_set).to(eq(%w[ch0 ch1].to_set)) |
41 | 94 | expect(ep.event_types).to(be_nil) |
42 | 95 |
|
43 | | - ep_patched = test_client.endpoint.patch( |
| 96 | + ep_patched = @test_client.endpoint.patch( |
44 | 97 | app.id, |
45 | 98 | ep.id, |
46 | 99 | Svix::EndpointPatch.new(event_types: %w[event.started event.ended]) |
|
50 | 103 | expect(ep_patched.event_types.to_set).to(eq(%w[event.started event.ended].to_set)) |
51 | 104 |
|
52 | 105 | # If the serialization is handling empty response bodies, this should not throw an exception |
53 | | - test_client.endpoint.delete(app.id, ep.id) |
| 106 | + @test_client.endpoint.delete(app.id, ep.id) |
54 | 107 | end |
55 | 108 | end |
56 | 109 | end |
0 commit comments