summaryrefslogtreecommitdiff
path: root/spec/unit/compliance/fetcher/automate_spec.rb
blob: a4cd0c76c39cd1edf5c1795cdf32592e9bb71cce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require "spec_helper"
require "chef/compliance/fetcher/automate"

describe Chef::Compliance::Fetcher::Automate do
  describe ".resolve" do
    before do
      Chef::Config[:data_collector] = {
        server_url: "https://automate.test/data_collector",
        token: token,
      }
    end

    let(:token) { "fake_token" }

    context "when target is a string" do
      it "should resolve a compliance URL" do
        res = Chef::Compliance::Fetcher::Automate.resolve("compliance://namespace/profile_name")

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://automate.test/compliance/profiles/namespace/profile_name/tar"
        expect(res.target).to eq(expected)
      end

      it "should resolve a compliance URL with a @ in the namespace" do
        res = Chef::Compliance::Fetcher::Automate.resolve("compliance://name@space/profile_name")

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://automate.test/compliance/profiles/name@space/profile_name/tar"
        expect(res.target).to eq(expected)
      end

      it "includes the data collector token" do
        expect(Chef::Compliance::Fetcher::Automate).to receive(:new).with(
          "https://automate.test/compliance/profiles/namespace/profile_name/tar",
          hash_including("token" => token)
        ).and_call_original

        res = Chef::Compliance::Fetcher::Automate.resolve("compliance://namespace/profile_name")

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://automate.test/compliance/profiles/namespace/profile_name/tar"
        expect(res.target).to eq(expected)
      end

      it "returns nil with a non-compliance URL" do
        res = Chef::Compliance::Fetcher::Automate.resolve("http://github.com/chef-cookbooks/audit")

        expect(res).to eq(nil)
      end
    end

    context "when target is a hash" do
      it "should resolve a target with a version" do
        res = Chef::Compliance::Fetcher::Automate.resolve(
          compliance: "namespace/profile_name",
          version: "1.2.3"
        )

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://automate.test/compliance/profiles/namespace/profile_name/version/1.2.3/tar"
        expect(res.target).to eq(expected)
      end

      it "should resolve a target without a version" do
        res = Chef::Compliance::Fetcher::Automate.resolve(
          compliance: "namespace/profile_name"
        )

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://automate.test/compliance/profiles/namespace/profile_name/tar"
        expect(res.target).to eq(expected)
      end

      it "uses url key when present" do
        res = Chef::Compliance::Fetcher::Automate.resolve(
          compliance: "namespace/profile_name",
          version: "1.2.3",
          url: "https://profile.server.test/profiles/profile_name/1.2.3"
        )

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://profile.server.test/profiles/profile_name/1.2.3"
        expect(res.target).to eq(expected)
      end

      it "does not include token in the config when url key is present" do
        expect(Chef::Compliance::Fetcher::Automate).to receive(:new).with(
          "https://profile.server.test/profiles/profile_name/1.2.3",
          hash_including("token" => nil)
        ).and_call_original

        res = Chef::Compliance::Fetcher::Automate.resolve(
          compliance: "namespace/profile_name",
          version: "1.2.3",
          url: "https://profile.server.test/profiles/profile_name/1.2.3"
        )

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://profile.server.test/profiles/profile_name/1.2.3"
        expect(res.target).to eq(expected)
      end

      it "includes the data collector token" do
        expect(Chef::Compliance::Fetcher::Automate).to receive(:new).with(
          "https://automate.test/compliance/profiles/namespace/profile_name/tar",
          hash_including("token" => token)
        ).and_call_original

        res = Chef::Compliance::Fetcher::Automate.resolve(compliance: "namespace/profile_name")

        expect(res).to be_kind_of(Chef::Compliance::Fetcher::Automate)
        expected = "https://automate.test/compliance/profiles/namespace/profile_name/tar"
        expect(res.target).to eq(expected)
      end

      it "returns nil with a non-profile Hash" do
        res = Chef::Compliance::Fetcher::Automate.resolve(
          profile: "namespace/profile_name",
          version: "1.2.3"
        )

        expect(res).to eq(nil)
      end
    end
  end
end