#!/usr/bin/env ruby
# $Id: tc_ext_login.rb,v 1.2 2004/05/11 13:07:12 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

require "test_common"

class TC_Login < Test::Unit::TestCase
  include TestSession

  def test_login_getmail
    @req.parse_host_path("qwik.jp", "/test/")
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @res.body
    assert_equal("http://qwik.jp/test/のメンバーですか? - qwik.jp", xml.elements["//title"].text)
    assert_equal("あなたはこのグループのメンバーではないようです。", xml.elements["//p"].text)

    @site.member.add("test@example.com")
    assert_equal(false, @site.is_open?)

    @req.cookies["user"] = nil
    @req.cookies["pass"] = nil
    @session.run
    xml = @res.body
    assert_equal("ログイン: http://qwik.jp/test/ - qwik.jp", xml.elements["//title"].text)
    assert_equal("user", xml.elements["//input"].attribute("name").to_s)

    @req.parse_host_path("qwik.jp", "/test/GetPass.html")
    @session.run
    xml = @res.body
    assert_equal("パスワード入手 - qwik.jp", xml.elements["//title"].text)
    assert_equal("mail", xml.elements["//input"].attribute("name").to_s)

    @req.query["mail"] = "test@example.com"
    @session.run
    xml = @res.body
    assert_equal("Send Password - qwik.jp", xml.elements["//title"].text)
    assert_equal("test@example.com", xml.elements["//b"].text)
    assert_equal(["test@qwik.jp", "test@example.com"], $smtp_sendmail[2..3])
    assert_match(/test@example.com/, $smtp_sendmail[4])
    assert_match(/ABAB4B32/, $smtp_sendmail[4])

    @req.parse_host_path("qwik.jp", "/test/")
    @session.run
    xml = @res.body
    assert_equal("ログイン: http://qwik.jp/test/ - qwik.jp", xml.elements["//title"].text)
  end

  def test_open_site_login
    page = @site["SiteConfig"]
    page.store(":open:true")
    @site.member.add("test@example.com")

    @req.cookies["user"] = nil
    @req.cookies["pass"] = nil
    @req.parse_host_path("qwik.jp", "/test/")
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @res.body
    assert_equal("FrontPage - qwik.jp/test", xml.elements["//title"].text)

    @req.request_method = "GET"
    @req.query["user"] = "test@example.com"
    @req.query["pass"] = "ABAB4B32"
    @req.parse_host_path("qwik.jp", "/test/")
    @session.run
    xml = @res.body
    assert_equal("cookieをセットしました - qwik.jp", xml.elements["//title"].text) # force redirect
    assert_equal("ページを表示", xml.elements["//a"].text)
#    assert_equal("http://qwik.jp/test/FrontPage.html", xml.elements["//a"].attribute("href").to_s)
    assert_equal("FrontPage.html", xml.elements["//a"].attribute("href").to_s)
    check_cookie(@res.cookies[0], "user", "test@example.com")
    check_cookie(@res.cookies[1], "pass", "ABAB4B32")
   
    @req.parse_host_path("qwik.jp", "/test/FrontPage.html")
    @req.query["user"] = nil
    @req.query["pass"] = nil
    @req.cookies["user"] = "test@example.com"
    @req.cookies["pass"] = "ABAB4B32"
    @session.run
    xml = @res.body
    assert_equal("FrontPage - qwik.jp/test", xml.elements["//title"].text)

    @req.request_method = "POST"
    @req.parse_host_path("qwik.jp", "/test/")
    @req.query["user"] = "test@example.com"
    @req.query["pass"] = "ABAB4B32"
    @req.cookies["user"] = "test@example.com"
    @req.cookies["pass"] = "ABAB4B32"
    @session.run
    xml = @res.body
#    puts xml.to_xhtml(0).u8tosjis
    assert_equal("cookieをセットしました - qwik.jp", xml.elements["//title"].text) # force redirect
  end

  def check_cookie(cookie, name, value)
    assert_instance_of(WEBrick::Cookie, cookie)
    assert_equal(name, cookie.name)
    assert_equal(value, cookie.value)
  end

  def test_logout
    @site.member.add("test@example.com")
    @req.parse_host_path("qwik.jp", "/test/LogOut.html")
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run

    xml = @session.xml
    assert_equal("LogOut - qwik.jp/test", xml.elements["//title"].text)
    assert_equal("LogOut.html", xml.elements["//form"].attribute("action").to_s)

    @req.query["confirm"] = "yes"
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run

    xml = @session.xml
#    puts xml.to_xhtml(0).u8tosjis
    assert_equal("LogOut - qwik.jp/test", xml.elements["//title"].text)
    assert_equal("LOGOUTしました。", xml.elements["//h3"].text)
  end
end

