1
0
mirror of https://github.com/cmur2/dyndnsd.git synced 2024-12-22 10:54:23 +01:00
dyndnsd/spec/daemon_spec.rb

241 lines
7.6 KiB
Ruby
Raw Normal View History

2013-04-27 14:07:14 +02:00
require 'spec_helper'
describe Dyndnsd::Daemon do
include Rack::Test::Methods
2013-04-27 14:07:14 +02:00
def app
2013-04-27 22:10:47 +02:00
Dyndnsd.logger = Logger.new(STDOUT)
Dyndnsd.logger.level = Logger::UNKNOWN
2013-04-27 14:07:14 +02:00
config = {
2013-04-27 14:59:25 +02:00
'domain' => 'example.org',
2013-04-27 14:07:14 +02:00
'users' => {
'test' => {
'password' => 'secret',
2013-04-27 15:20:08 +02:00
'hosts' => ['foo.example.org', 'bar.example.org']
2013-04-27 14:07:14 +02:00
}
}
}
db = Dyndnsd::DummyDatabase.new({})
updater = Dyndnsd::Updater::Dummy.new
daemon = Dyndnsd::Daemon.new(config, db, updater)
2018-02-23 12:54:43 +01:00
app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
app = Dyndnsd::Responder::DynDNSStyle.new(app)
Rack::Tracer.new(app, trust_incoming_span: false)
2013-04-27 14:07:14 +02:00
end
2013-04-27 14:07:14 +02:00
it 'requires authentication' do
get '/'
2016-11-30 22:07:51 +01:00
expect(last_response.status).to eq(401)
expect(last_response.body).to eq('badauth')
2013-04-27 14:07:14 +02:00
end
it 'requires configured correct credentials' do
authorize 'test', 'wrongsecret'
get '/'
expect(last_response.status).to eq(401)
expect(last_response.body).to eq('badauth')
end
2013-04-27 14:07:14 +02:00
it 'only supports GET requests' do
authorize 'test', 'secret'
post '/nic/update'
2016-11-30 22:07:51 +01:00
expect(last_response.status).to eq(405)
2013-04-27 14:07:14 +02:00
end
2013-10-08 13:25:35 +02:00
it 'provides only the /nic/update URL' do
2013-04-27 14:07:14 +02:00
authorize 'test', 'secret'
get '/other/url'
2016-11-30 22:07:51 +01:00
expect(last_response.status).to eq(404)
2013-04-27 14:07:14 +02:00
end
2013-04-27 14:07:14 +02:00
it 'requires the hostname query parameter' do
authorize 'test', 'secret'
get '/nic/update'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
2013-04-27 14:07:14 +02:00
end
it 'supports multiple hostnames in request' do
authorize 'test', 'secret'
get '/nic/update?hostname=foo.example.org,bar.example.org&myip=1.2.3.4'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq("good 1.2.3.4\ngood 1.2.3.4")
get '/nic/update?hostname=foo.example.org,bar.example.org&myip=2001:db8::1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq("good 2001:db8::1\ngood 2001:db8::1")
end
it 'rejects request if one hostname is invalid' do
authorize 'test', 'secret'
get '/nic/update?hostname=test'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
get '/nic/update?hostname=test.example.com'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
get '/nic/update?hostname=test.example.org.me'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
get '/nic/update?hostname=foo.test.example.org'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
get '/nic/update?hostname=in%20valid.example.org'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
get '/nic/update?hostname=valid.example.org,in.valid.example.org'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('notfqdn')
end
it 'rejects request if user does not own one hostname' do
2013-04-27 14:07:14 +02:00
authorize 'test', 'secret'
2013-04-27 14:07:14 +02:00
get '/nic/update?hostname=notmyhost.example.org'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('nohost')
get '/nic/update?hostname=foo.example.org,notmyhost.example.org'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('nohost')
2013-04-27 14:07:14 +02:00
end
it 'updates a host on IP change' do
2013-04-27 14:07:14 +02:00
authorize 'test', 'secret'
2013-04-27 14:07:14 +02:00
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
2013-04-27 14:37:52 +02:00
get '/nic/update?hostname=foo.example.org&myip=1.2.3.40'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('good 1.2.3.40')
get '/nic/update?hostname=foo.example.org&myip=2001:db8::1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
get '/nic/update?hostname=foo.example.org&myip=2001:db8::10'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('good 2001:db8::10')
2013-04-27 14:07:14 +02:00
end
it 'returns IP no change' do
2013-04-27 14:07:14 +02:00
authorize 'test', 'secret'
2013-04-27 14:07:14 +02:00
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
2013-04-27 14:07:14 +02:00
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('nochg 1.2.3.4')
get '/nic/update?hostname=foo.example.org&myip=2001:db8::1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
get '/nic/update?hostname=foo.example.org&myip=2001:db8::1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('nochg 2001:db8::1')
2013-04-27 14:07:14 +02:00
end
it 'outputs IP status per hostname' do
2013-04-27 15:20:08 +02:00
authorize 'test', 'secret'
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('good 1.2.3.4')
2013-04-27 15:20:08 +02:00
get '/nic/update?hostname=foo.example.org,bar.example.org&myip=1.2.3.4'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq("nochg 1.2.3.4\ngood 1.2.3.4")
get '/nic/update?hostname=foo.example.org&myip=2001:db8::1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('good 2001:db8::1')
get '/nic/update?hostname=foo.example.org,bar.example.org&myip=2001:db8::1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq("nochg 2001:db8::1\ngood 2001:db8::1")
2013-04-27 14:07:14 +02:00
end
it 'offlines a host' do
authorize 'test', 'secret'
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4'
expect(last_response).to be_ok
expect(last_response.body).to eq('good 1.2.3.4')
get '/nic/update?hostname=foo.example.org&offline=YES'
expect(last_response).to be_ok
expect(last_response.body).to eq('good ')
get '/nic/update?hostname=foo.example.org&offline=YES'
expect(last_response).to be_ok
expect(last_response.body).to eq('nochg ')
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4'
expect(last_response).to be_ok
expect(last_response.body).to eq('good 1.2.3.4')
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4&offline=YES'
expect(last_response).to be_ok
expect(last_response.body).to eq('good ')
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4&offline=YES'
expect(last_response).to be_ok
expect(last_response.body).to eq('nochg ')
end
it 'uses clients remote IP address if myip not specified' do
2013-04-27 15:44:19 +02:00
authorize 'test', 'secret'
get '/nic/update?hostname=foo.example.org'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('good 127.0.0.1')
2013-04-27 15:44:19 +02:00
end
it 'uses clients remote IP address from X-Real-IP header if behind proxy' do
authorize 'test', 'secret'
get '/nic/update?hostname=foo.example.org', '', 'HTTP_X_REAL_IP' => '10.0.0.1'
2016-11-30 22:07:51 +01:00
expect(last_response).to be_ok
expect(last_response.body).to eq('good 10.0.0.1')
get '/nic/update?hostname=foo.example.org', '', 'HTTP_X_REAL_IP' => '2001:db8::1'
expect(last_response).to be_ok
expect(last_response.body).to eq('good 2001:db8::1')
end
it 'supports an IPv4 and an IPv6 address in one request' do
authorize 'test', 'secret'
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4&myip6=2001:db8::1'
expect(last_response).to be_ok
2018-02-23 12:54:43 +01:00
expect(last_response.body).to eq('good 1.2.3.4 2001:db8::1')
get '/nic/update?hostname=foo.example.org&myip=BROKENIP&myip6=2001:db8::1'
expect(last_response).to be_ok
expect(last_response.body).to eq('nohost')
get '/nic/update?hostname=foo.example.org&myip=1.2.3.4&myip6=BROKENIP'
expect(last_response).to be_ok
expect(last_response.body).to eq('nohost')
get '/nic/update?hostname=foo.example.org&myip6=2001:db8::10'
expect(last_response).to be_ok
expect(last_response.body).to eq('nohost')
get '/nic/update?hostname=foo.example.org&myip=1.2.3.40'
expect(last_response).to be_ok
expect(last_response.body).to eq('good 1.2.3.40')
end
2013-04-27 14:07:14 +02:00
end