require_relative '../spec_helper' require "logstash/filters/center-client" require "logstash/filters/response" require 'webmock/rspec' describe LogStash::Filters::Empow::ClassificationCenterClient do # before(:each) do # local_classifier = instance_double(LogStash::Filters::Empow::LocalClassifier) # allow(LogStash::Filters::Empow::LocalClassifier).to receive(:new).and_return(local_classifier) # end let(:url_base) { 'http://localhost:5000' } let(:username) { 'myuser' } let(:password) { 'mypassword' } let(:pool_id) { 'mypassword' } describe "classification center api" do before(:each) do WebMock.disable_net_connect! stub_request(:post, "#{url_base}/login"). to_return(:body => "", :status => 200, :headers => { 'authorization' => 'Bearer my-token' }) mocked_cognito = double(LogStash::Filters::Empow::CognitoClient) allow(LogStash::Filters::Empow::CognitoClient).to receive(:new).and_return(mocked_cognito) allow(mocked_cognito).to receive(:authenticate).and_return("dummy token") end after(:each) do WebMock.reset! WebMock.allow_net_connect! allow(LogStash::Filters::Empow::CognitoClient).to receive(:new).and_call_original end it "test missing ids request" do stub_request(:post, "#{url_base}/classification/intent"). to_return(:body => "", :status => 204, :headers => { 'Content-Length' => 0 }) client = described_class.new(username, password, pool_id, url_base) client.authenticate res = client.classify(["req1"]) expect(res["req1"]).to be_kind_of(LogStash::Filters::Empow::FailureReponse) end it "test existing ids request" do response = '{"some":"data"}' stub_request(:post, "#{url_base}/classification/intent"). to_return(:body => response, :status => 200) client = described_class.new(username, password, pool_id, url_base) client.authenticate k1 = "req1" response_map = client.classify([k1]) res = response_map[k1].response p "res: #{res}" expect(res["some"]).to eq("data") end it "test http status 500 during request" do stub_request(:post, "#{url_base}/classification/intent"). to_return(:body => "", :status => 500) client = described_class.new(username, password, pool_id, url_base) client.authenticate res = client.classify("ids", "Snort", "1:2", nil) expect(res).to be_nil end end end