1. Don Park
  2. geomena

Source

geomena / sinatra / tests / geomena.rb

require 'geomena'
require 'test/unit'
require 'rack/test'
require 'mocha'

class MyAppTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    Geomena::Web
  end

  ## READ / SEARCH

  def test_front_page
    get '/'
    assert last_response.ok?
    assert_match 'Geomena: Wifi geolocation', last_response.body
  end

  def test_search_by_mac
    get '/search?q=00:01:02:03:04:05'
    assert last_response.redirect?
  end

  def test_search_by_essid
    get '/search?q=personaltelco'
    assert last_response.redirect?
  end

  def test_display_existing_ap
    latitude = 10
    one_ap = {"rows"=>[{"value"=>{"latitude"=>latitude.to_s}}]}
    GCouch.expects(:get_by_mac).returns(one_ap)
    get '/ap/000102030405'
    assert last_response.ok?, "HTTP Response #{last_response.status} #{last_response.body}"
    assert_match 'Access Point', last_response.body
  end

  def test_display_undefined_ap
    get '/ap/000102030405'
    assert last_response.ok?, "HTTP Response #{last_response.status} #{last_response.body}"
    assert_match '(new record)', last_response.body
  end

  def test_w3c_locate_1
    mac = "123456abcdef"
    latitude = 10
    one_ap = {"rows"=>[{"value"=>{"latitude"=>latitude.to_s}}]}
    GCouch.expects(:get_by_mac).with(mac).returns(one_ap)

    extra = {"wifi_towers" => [{"mac_address" => mac}]}
    post "/", extra.to_json
    assert_match '"latitude":"'+latitude.to_s+'"', last_response.body
  end

  def test_w3c_locate_2_one_missing
    mac1 = "123456abcdef"
    latitude1 = 10
    mac2 = "123456uvwxyz"
    latitude2 = 20
    one_ap = {"rows"=>[]}
    two_ap = {"rows"=>[{"value"=>{"latitude"=>latitude2.to_s}}]}
    GCouch.expects(:get_by_mac).with(mac1).returns(one_ap)
    GCouch.expects(:get_by_mac).with(mac2).returns(two_ap)

    extra = {"wifi_towers" => [{"mac_address" => mac1}, {"mac_address" => mac2}]}
    post "/", extra.to_json
    assert_match '"latitude":"'+latitude2.to_s+'"', last_response.body
  end

  def test_w3c_locate_2_both_missing
    mac1 = "123456abcdef"
    mac2 = "123456uvwxyz"
    no_ap = {"rows"=>[]}
    GCouch.expects(:get_by_mac).with(mac1).returns(no_ap)
    GCouch.expects(:get_by_mac).with(mac2).returns(no_ap)

    extra = {"wifi_towers" => [{"mac_address" => mac1}, {"mac_address" => mac2}]}
    post "/", extra.to_json
puts last_response.body
    assert_match "", last_response.body
  end

  def test_w3c_locate_empty
    extra = {"wifi_towers" => []}
    post "/", extra.to_json
    assert_match "", last_response.body
  end

  ## WRITE

  def test_post_new_ap
    mac = "12:34:56:ab:cd:f"
    GCouch.expects(:put)

    extra = {:latitude => "45.5",
             :longitude => "-122.6",
             :essid => "RebelBase"}
    post "/ap/#{mac}", extra
    assert last_response.redirect?
  end

end