spec/unit/ridley/resources/search_spec.rb in ridley-0.0.6 vs spec/unit/ridley/resources/search_spec.rb in ridley-0.1.0

- old
+ new

@@ -2,11 +2,20 @@ describe Ridley::Search do let(:connection) { double('connection') } let(:index) { :role } let(:query) { "*:*" } - let(:response) { double("response", body: Hash.new) } + let(:response) do + double( + "response", + body: { + rows: Array.new, + total: 0, + start: 0 + } + ) + end describe "ClassMethods" do subject { Ridley::Search } describe "::indexes" do @@ -57,9 +66,152 @@ it "sends a get request to the connection with a query parameter for 'rows'" do connection.should_receive(:get).with("search/#{index}", q: query, rows: rows).and_return(response) subject.run + end + end + + context "when ':node' is given as index" do + let(:index) { :node } + let(:response) do + double( + "response", + body: { + rows: [ + { + chef_type: "node", + json_class: "Chef::Node", + name: "ridley-one", + chef_environment: "_default", + automatic: {}, + normal: {}, + default: {}, + override: {}, + run_list: [ + "recipe[one]", + "recipe[two]" + ] + } + ], + total: 1, + start: 0 + } + ) + end + + subject { Ridley::Search.new(connection, index, query) } + + it "returns an array of Ridley::Node" do + connection.should_receive(:get).with("search/#{index}", q: query).and_return(response) + result = subject.run + + result.should be_a(Array) + result.should each be_a(Ridley::Node) + end + end + + context "when ':role' is given as index" do + let(:index) { :role } + let(:response) do + double( + "response", + body: { + rows: [ + { + chef_type: "role", + json_class: "Chef::Role", + name: "ridley-role-one", + description: "", + default_attributes: {}, + override_attributes: {}, + run_list: [], + env_run_lists: {} + } + ], + total: 1, + start: 0 + } + ) + end + + subject { Ridley::Search.new(connection, index, query) } + + it "returns an array of Ridley::Role" do + connection.should_receive(:get).with("search/#{index}", q: query).and_return(response) + result = subject.run + + result.should be_a(Array) + result.should each be_a(Ridley::Role) + end + end + + context "when ':environment' is given as index" do + let(:index) { :environment } + let(:response) do + double( + "response", + body: { + rows: [ + { + chef_type: "environment", + json_class: "Chef::Environment", + name: "ridley-env-test", + description: "ridley testing environment", + default_attributes: {}, + override_attributes: {}, + cookbook_versions: {} + } + ], + total: 1, + start: 0 + } + ) + end + + subject { Ridley::Search.new(connection, index, query) } + + it "returns an array of Ridley::Environment" do + connection.should_receive(:get).with("search/#{index}", q: query).and_return(response) + result = subject.run + + result.should be_a(Array) + result.should each be_a(Ridley::Environment) + end + end + + context "when ':client' is given as index" do + let(:index) { :client } + let(:response) do + double( + "response", + body: { + rows: [ + { + chef_type: "client", + name: nil, + admin: false, + validator: false, + certificate: "-----BEGIN CERTIFICATE-----\nMIIDOjCCAqOgAwIBAgIE47eOmDANBgkqhkiG9w0BAQUFADCBnjELMAkGA1UEBhMC\nVVMxEzARBgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1NlYXR0bGUxFjAUBgNV\nBAoMDU9wc2NvZGUsIEluYy4xHDAaBgNVBAsME0NlcnRpZmljYXRlIFNlcnZpY2Ux\nMjAwBgNVBAMMKW9wc2NvZGUuY29tL2VtYWlsQWRkcmVzcz1hdXRoQG9wc2NvZGUu\nY29tMCAXDTEyMTAwOTAwMTUxNVoYDzIxMDExMTA0MDAxNTE1WjCBnTEQMA4GA1UE\nBxMHU2VhdHRsZTETMBEGA1UECBMKV2FzaGluZ3RvbjELMAkGA1UEBhMCVVMxHDAa\nBgNVBAsTE0NlcnRpZmljYXRlIFNlcnZpY2UxFjAUBgNVBAoTDU9wc2NvZGUsIElu\nYy4xMTAvBgNVBAMUKFVSSTpodHRwOi8vb3BzY29kZS5jb20vR1VJRFMvY2xpZW50\nX2d1aWQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCqB9KEGzl7Wcm/\nwz/x8HByZANCn6WQC+R12qQso5I6nLbTNkRP668jXG3j0R5/F5i/KearAB9ePzL/\nQe3iHtwW6u1qLI1hVNFNB+I1fGu1p6fZyIOjnLn3bqsbOkBplHOIqHsp4GVSsHKb\nD32UXZDa9S9ZFXnR4iT6hUGm5895ReZG9TDiHvBpi9NJFDZXz+AQ6JuQY8UgYMMA\nm80KbO8/NJlXbRW+siRuvr+LIsi9Mx4i63pBWAN46my291rQU31PF3IB+btfGtR/\nyDWDgMSB37bTzZeOf1Dg9fpl2vIXyu3PoHER0oYmrMQbrdwAt7qCHZNuNWn51WPb\n1PHxXL1rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAGnJUVAv951fUhGyPOrl+LbQG\nqgchMwIn7oDLE863e66BYTDj7koK3jjhx3EBkrT2vt/xS4yW0ZRV1BNqfnNKWbBq\nMNQiKkYdTr+oq2O3plOg/q/M1eG1B5pxGXqvH0O76DVWQcV/svO+HQEi1n8y5UQd\n+pBJCygpuv78wPCM+c4=\n-----END CERTIFICATE-----\n", + public_key: nil, + private_key: nil, + orgname: "ridley" + } + ], + total: 1, + start: 0 + } + ) + end + + subject { Ridley::Search.new(connection, index, query) } + + it "returns an array of Ridley::Client" do + connection.should_receive(:get).with("search/#{index}", q: query).and_return(response) + result = subject.run + + result.should be_a(Array) + result.should each be_a(Ridley::Client) end end end end