Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore untenantize #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/mumukit/platform/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class Basic < Mumukit::Platform::Application
def organic_uri(_organization)
uri
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that Basic apps need to respond to that method, but I added it to keep it consistent with organic_uri decision

def retenantize_in(organization, tenantized_path)
organic_url_for(organization, tenantized_path)
end
end

class Organic < Mumukit::Platform::Application
Expand All @@ -57,6 +61,11 @@ def initialize(url, organization_mapping)
@organization_mapping = organization_mapping
end

def retenantize_in(organization, tenantized_path)
untenantized_path = organization_mapping.untenantize(tenantized_path)
organic_url_for(organization, untenantized_path)
end

def organic_uri(organization)
organization_mapping.organic_uri(uri, organization)
end
Expand Down
22 changes: 11 additions & 11 deletions lib/mumukit/platform/organization_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module Base
def path_for(request)
request.path_info
end

def untenantize(path)
path
end
end

module Subdomain
Expand All @@ -48,10 +52,6 @@ def self.organic_uri(uri, organization)
def self.path_under_namespace?(_organization_name, path, namespace)
path.start_with? "/#{namespace}/"
end

def self.inorganic_path_for(request)
path_for(request)
end
end

module Path
Expand All @@ -65,17 +65,13 @@ def self.map_organization_routes!(native, framework, &block)
framework.configure_tenant_path_routes! native, &block
end

def self.path_composition_for(request)
organization, *path_parts = Pathname(path_for(request)).each_filename.to_a
def self.path_composition_for(path)
organization, *path_parts = Pathname(path).each_filename.to_a
[organization, path_parts.join('/')]
end

def self.organization_name(request, _domain)
path_composition_for(request).first
end

def self.inorganic_path_for(request)
path_composition_for(request).second
path_composition_for(path_for(request)).first
end

def self.organic_uri(uri, organization)
Expand All @@ -85,5 +81,9 @@ def self.organic_uri(uri, organization)
def self.path_under_namespace?(organization_name, path, namespace)
path.start_with? "/#{organization_name}/#{namespace}/"
end

def self.untenantize(path)
path_composition_for(path).second
end
end
end
43 changes: 27 additions & 16 deletions spec/mumukit/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe Mumukit::Platform::Application do
it { expect(Mumukit::Platform.laboratory.organic_url_for 'foo', '/foo/baz').to eq 'http://foo.localmumuki.io/foo/baz' }
it { expect(Mumukit::Platform.laboratory.retenantize_in 'an-orga', '/a_path').to eq 'http://an-orga.localmumuki.io/a_path' }

it { expect(Mumukit::Platform.classroom_ui.domain).to eq 'classroom.localmumuki.io' }
it { expect(Mumukit::Platform.classroom_ui.organic_url_for 'org', '/foo/baz').to eq 'http://org.classroom.localmumuki.io/#/foo/baz' }
Expand All @@ -17,19 +18,21 @@
describe Mumukit::Platform::Application::Basic do
context 'with subdomain mapping strategy' do
let(:mapping) { Mumukit::Platform::OrganizationMapping::Subdomain }
it { expect(Mumukit::Platform::Application::Basic.new('http://foo.com:3000').organic_url('org')).to eq 'http://foo.com:3000' }
it { expect(new_basic_app('http://foo.com:3000').organic_url('org')).to eq 'http://foo.com:3000' }
it { expect(new_basic_app('http://foo.com:3000').retenantize_in('other-orga', '/an-orga/deep')).to eq 'http://foo.com:3000/an-orga/deep' }
end

context 'with path mapping strategy' do
let(:mapping) { Mumukit::Platform::OrganizationMapping::Path }
it { expect(Mumukit::Platform::Application::Basic.new('http://foo.com:3000').organic_url('org')).to eq 'http://foo.com:3000' }
it { expect(Mumukit::Platform::Application::Basic.new('http://foo.com:3000/foo').organic_url('org')).to eq 'http://foo.com:3000/foo' }
it { expect(Mumukit::Platform::Application::Basic.new('http://foo.com:3000/app/').organic_url_for('an_org', 'some/long/path')).to eq 'http://foo.com:3000/app/some/long/path' }
it { expect(Mumukit::Platform::Application::Basic.new('http://foo.com:3000/another_app/').organic_url_for('another_org', 'some/long/path?with=value')).to eq 'http://foo.com:3000/another_app/some/long/path?with=value' }
it { expect(new_basic_app('http://foo.com:3000').organic_url('org')).to eq 'http://foo.com:3000' }
it { expect(new_basic_app('http://foo.com:3000/foo').organic_url('org')).to eq 'http://foo.com:3000/foo' }
it { expect(new_basic_app('http://foo.com:3000/app/').organic_url_for('an_org', 'some/long/path')).to eq 'http://foo.com:3000/app/some/long/path' }
it { expect(new_basic_app('http://foo.com:3000/another_app/').organic_url_for('another_org', 'some/long/path?with=value')).to eq 'http://foo.com:3000/another_app/some/long/path?with=value' }
it { expect(new_basic_app('http://foo.com:3000').retenantize_in('other-orga', '/an-orga/deep')).to eq 'http://foo.com:3000/an-orga/deep' }

context 'with fragments' do
context 'with prefragment-path' do
let(:fragmented) { Mumukit::Platform::Application::Basic.new('http://foo.com:3000/bibliotheca/#/') }
let(:fragmented) { new_basic_app('http://foo.com:3000/bibliotheca/#/') }

it { expect(fragmented.url).to eq 'http://foo.com:3000/bibliotheca/#/' }

Expand All @@ -41,7 +44,7 @@
end

context 'with fragment-path' do
let(:fragmented) { Mumukit::Platform::Application::Basic.new('http://foo.com:3000/#/bibliotheca/') }
let(:fragmented) { new_basic_app('http://foo.com:3000/#/bibliotheca/') }

it { expect(fragmented.url).to eq 'http://foo.com:3000/#/bibliotheca/' }

Expand All @@ -53,7 +56,7 @@
end

context 'without fragment-path' do
let(:fragmented) { Mumukit::Platform::Application::Basic.new('http://foo.com:3000/#') }
let(:fragmented) { new_basic_app('http://foo.com:3000/#') }

it { expect(fragmented.url).to eq 'http://foo.com:3000/#' }

Expand All @@ -71,19 +74,21 @@
describe Mumukit::Platform::Application::Organic do
context 'with subdomain mapping strategy' do
let(:mapping) { Mumukit::Platform::OrganizationMapping::Subdomain }
it { expect(Mumukit::Platform::Application::Organic.new('http://foo.com:3000', mapping).organic_url('bar')).to eq 'http://bar.foo.com:3000' }
it { expect(new_organic_app('http://foo.com:3000', mapping).organic_url('bar')).to eq 'http://bar.foo.com:3000' }
it { expect(new_organic_app('http://foo.com:3000', mapping).retenantize_in('bar', '/a_path/deep')).to eq 'http://bar.foo.com:3000/a_path/deep' }
end

context 'with path mapping strategy' do
let(:mapping) { Mumukit::Platform::OrganizationMapping::Path }
it { expect(Mumukit::Platform::Application::Organic.new('http://foo.com:3000', mapping).organic_url('org')).to eq 'http://foo.com:3000/org/' }
it { expect(Mumukit::Platform::Application::Organic.new('http://foo.com:3000/foo', mapping).organic_url('org')).to eq 'http://foo.com:3000/foo/org/' }
it { expect(Mumukit::Platform::Application::Organic.new('http://foo.com:3000/app/', mapping).organic_url_for('an_org', 'some/long/path')).to eq 'http://foo.com:3000/app/an_org/some/long/path' }
it { expect(Mumukit::Platform::Application::Organic.new('http://foo.com:3000/another_app/', mapping).organic_url_for('another_org', 'some/long/path?with=value')).to eq 'http://foo.com:3000/another_app/another_org/some/long/path?with=value' }
it { expect(new_organic_app('http://foo.com:3000', mapping).organic_url('org')).to eq 'http://foo.com:3000/org/' }
it { expect(new_organic_app('http://foo.com:3000/foo', mapping).organic_url('org')).to eq 'http://foo.com:3000/foo/org/' }
it { expect(new_organic_app('http://foo.com:3000/app/', mapping).organic_url_for('an_org', 'some/long/path')).to eq 'http://foo.com:3000/app/an_org/some/long/path' }
it { expect(new_organic_app('http://foo.com:3000/another_app/', mapping).organic_url_for('another_org', 'some/long/path?with=value')).to eq 'http://foo.com:3000/another_app/another_org/some/long/path?with=value' }
it { expect(new_organic_app('http://foo.com:3000', mapping).retenantize_in('other_orga', '/an_orga/deep')).to eq 'http://foo.com:3000/other_orga/deep' }

context 'with fragments' do
context 'with prefragment-path' do
let(:fragmented) { Mumukit::Platform::Application::Organic.new('http://foo.com:3000/classroom/#/', mapping) }
let(:fragmented) { new_organic_app('http://foo.com:3000/classroom/#/', mapping) }

it { expect(fragmented.url).to eq 'http://foo.com:3000/classroom/#/' }

Expand All @@ -95,7 +100,7 @@
end

context 'with fragment-path' do
let(:fragmented) { Mumukit::Platform::Application::Organic.new('http://foo.com:3000/#/classroom/', mapping) }
let(:fragmented) { new_organic_app('http://foo.com:3000/#/classroom/', mapping) }

it { expect(fragmented.url).to eq 'http://foo.com:3000/#/classroom/' }

Expand All @@ -107,7 +112,7 @@
end

context 'without fragment-path' do
let(:fragmented) { Mumukit::Platform::Application::Organic.new('http://foo.com:3000/#', mapping) }
let(:fragmented) { new_organic_app('http://foo.com:3000/#', mapping) }

it { expect(fragmented.url).to eq 'http://foo.com:3000/#' }

Expand All @@ -121,3 +126,9 @@
end
end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real benefit here, just to keep the tests less verbose

%w(basic organic).each do |app_type|
define_method("new_#{app_type}_app") do |*params|
"Mumukit::Platform::Application::#{app_type.capitalize}".constantize.new *params
end
end
17 changes: 9 additions & 8 deletions spec/mumukit/organization_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@
it { expect(subject.path_under_namespace? 'central', '/central/foo/bar', 'central').to be true }
end

describe '#untenantize' do
it { expect(subject.untenantize('/a_route/nested/deep')).to eq '/a_route/nested/deep' }
end

context 'on non central' do
let(:request) { new_rack_request 'http', 'foo.something.com', '80', '/bar/other' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'foo' }
it { expect(subject.inorganic_path_for(request)).to eq '/bar/other' }
end

context 'on explicit central' do
let(:request) { new_rack_request 'http', 'central.something.com', '80', '/bar/res' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'central' }
it { expect(subject.inorganic_path_for(request)).to eq '/bar/res' }
it { expect(subject.implicit_organization?(request, 'something.com')).to be false }
end

context 'on implicit central' do
let(:request) { new_rack_request 'http', 'something.com', '80', '/bar/other' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'central' }
it { expect(subject.inorganic_path_for(request)).to eq '/bar/other' }
it { expect(subject.implicit_organization?(request, 'something.com')).to be true }
end
end
Expand All @@ -57,33 +58,33 @@
it { expect(subject.path_under_namespace? 'central', '/central/foo/bar', 'baz').to be false }
end

describe '#untenantize' do
it { expect(subject.untenantize('/orga/a_route/nested/deep')).to eq 'a_route/nested/deep' }
it { expect(subject.untenantize('/orga')).to eq '' }
end

context 'on non central' do
let(:request) { new_rack_request 'http', 'something.com', '80', '/foo' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'foo' }
it { expect(subject.inorganic_path_for(request)).to eq '' }
end

context 'on non central with extra path' do
let(:request) { new_rack_request 'http', 'something.com', '80', '/foo/bar' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'foo' }
it { expect(subject.inorganic_path_for(request)).to eq 'bar' }
end

context 'on non central with extra path and params' do
let(:request) { new_rack_request 'http', 'something.com', '80', '/foo/bar/other?param=val' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'foo' }
it { expect(subject.inorganic_path_for(request)).to eq 'bar/other?param=val' }
end

context 'on non central with trailing slash' do
let(:request) { new_rack_request 'http', 'something.com', '80', '/foo/' }
it { expect(subject.inorganic_path_for(request)).to eq '' }
end

context 'on central' do
let(:request) { new_rack_request 'http', 'something.com', '80', '/central/' }
it { expect(subject.organization_name(request, 'something.com')).to eq 'central' }
it { expect(subject.inorganic_path_for(request)).to eq '' }
end
end
end