|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'datadog/appsec/api_security/endpoint_collection/rails_routes_serializer' |
| 5 | + |
| 6 | +RSpec.describe Datadog::AppSec::APISecurity::EndpointCollection::RailsRoutesSerializer do |
| 7 | + describe '#to_enum' do |
| 8 | + it 'returns an Enumerator' do |
| 9 | + expect(described_class.new([]).to_enum).to be_a(Enumerator) |
| 10 | + end |
| 11 | + |
| 12 | + it 'correctly serializes routes' do |
| 13 | + routes = described_class.new([ |
| 14 | + build_route_double(method: 'GET', path: '/events') |
| 15 | + ]).to_enum |
| 16 | + |
| 17 | + expect(routes.count).to eq(1) |
| 18 | + |
| 19 | + aggregate_failures 'serialized attributes' do |
| 20 | + expect(routes.first.fetch(:type)).to eq('REST') |
| 21 | + expect(routes.first.fetch(:resource_name)).to eq('GET /events') |
| 22 | + expect(routes.first.fetch(:operation_name)).to eq('http.request') |
| 23 | + expect(routes.first.fetch(:method)).to eq('GET') |
| 24 | + expect(routes.first.fetch(:path)).to eq('/events') |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + it 'removes rails format suffix from the path' do |
| 29 | + routes = described_class.new([ |
| 30 | + build_route_double(method: 'GET', path: '/events(.:format)') |
| 31 | + ]).to_enum |
| 32 | + |
| 33 | + aggregate_failures 'path attributes' do |
| 34 | + expect(routes.first.fetch(:resource_name)).to eq('GET /events') |
| 35 | + expect(routes.first.fetch(:path)).to eq('/events') |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + it 'sets method to * for wildcard routes' do |
| 40 | + routes = described_class.new([ |
| 41 | + build_route_double(method: '*', path: '/') |
| 42 | + ]).to_enum |
| 43 | + |
| 44 | + aggregate_failures 'path attributes' do |
| 45 | + expect(routes.first.fetch(:resource_name)).to eq('* /') |
| 46 | + expect(routes.first.fetch(:method)).to eq('*') |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + it 'skips non-dispatcher routes for now' do |
| 51 | + routes = described_class.new([ |
| 52 | + build_route_double(method: nil, path: 'admin', is_dispatcher: false) |
| 53 | + ]).to_enum |
| 54 | + |
| 55 | + expect(routes.to_a).to be_empty |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def build_route_double(method:, path:, is_dispatcher: true) |
| 60 | + instance_double( |
| 61 | + 'ActionDispatch::Journey::Route', |
| 62 | + dispatcher?: is_dispatcher, |
| 63 | + verb: method, |
| 64 | + path: instance_double( |
| 65 | + 'ActionDispatch::Journey::Path::Pattern', |
| 66 | + spec: path |
| 67 | + ) |
| 68 | + ) |
| 69 | + end |
| 70 | +end |
0 commit comments