#!/usr/bin/env ruby
# $Id: tc_ext_member.rb,v 1.7 2004/04/21 16:23:03 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.

$LOAD_PATH.unshift('../../lib')
$LOAD_PATH.unshift('../../compat')
require 'test/unit'
require 'qwik/server'

class TC_PasswordGenerator < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @config.pass_file = './password.txt'
  end

  def test_password
    pw = Qwik::PasswordGenerator.new(@config)
    assert_equal("E4268F8D", pw.generate("test@example.com"))
    assert_equal("37CF90D4", pw.generate("test@example.com", 1))
  end
end

class TC_PasswordMailGenerator < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @config.pass_file = './password.txt'
    @config.data_dir = './'
  end

  def test_getpass_mail
    with_dir('test'){
      site = Qwik::Site.new(@config, 'test')
      pmgen = Qwik::PasswordMailGenerator.new(@config, site)
      mail = pmgen.generate('test@example.com')
      assert_equal("test@qwik.jp", mail.from)
      assert_equal('test@example.com', mail.to)
      assert_equal('http://qwik.jp/ におけるパスワード', mail.subject)
      assert_equal(<<"EOT", mail.content)
http://qwik.jp/ におけるユーザー名とパスワードは、下記の通りです。

ユーザー名:	test@example.com
パスワード:	E4268F8D

http://qwik.jp/test/ に再度アクセスしてみてください。
EOT
    }
    with_dir('example.com'){
      site = Qwik::Site.new(@config, 'example.com')
      pmgen = Qwik::PasswordMailGenerator.new(@config, site)
      mail = pmgen.generate('test@example.com')
      assert_equal("info@example.com", mail.from)
      assert_equal('test@example.com', mail.to)
      assert_equal('http://example.com/ におけるパスワード', mail.subject)
      assert_equal(<<"EOT", mail.content)
http://example.com/ におけるユーザー名とパスワードは、下記の通りです。

ユーザー名:	test@example.com
パスワード:	E4268F8D

http://example.com/ に再度アクセスしてみてください。
EOT
    }
  end

  def with_dir(sitename)
    dir = ('./'+sitename+'/').path
    dir.rmtree if dir.directory?
    dir.rmdir  if dir.directory?
    dir.mkdir if !dir.directory?
    yield
    dir.rmtree if dir.directory?
    dir.rmdir  if dir.directory?
  end
end

class TC_MemberPlugin < Test::Unit::TestCase
  $KCODE = 'u'
  def setup()
    @config = Qwik::Config.new(true)
    @config.pass_file = './password.txt'
    @config.data_dir = './'
    @dir = './test/'.path
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if !@dir.directory?
    @memory = Qwik::ServerMemory.new(@config)
    @req = Qwik::Request.new(@config)
    @res = Qwik::Response.new(@config)
    @req.cookies['user'] = 'test@example.com'
    @req.cookies['pass'] = 'E4268F8D'
  end

  def test_session
    site = Qwik::Site.new(@config, 'test')
    site.member.add('test@example.com')
    @req.parse_host_path('qwik.jp', '/test/MemberAdd.html')
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
    assert_equal("MemberAdd - qwik.jp/test", xml.elements['//title'].text)
    assert_equal("MemberAdd.html", xml.elements['//form'].attribute('action').to_s)

    @req.query['tomail'] = 'guest@example.com'
    @session = Qwik::Session.new(@config, @memory, @req, @res)
    @session.run
    xml = @session.xml
#    puts xml.to_xhtml(0).u8tosjis
    assert_equal("メンバーは追加されました。", xml.elements['//h3'].text)
    assert_equal(true, site.member.exist?('guest@example.com'))
    assert_equal(",test@example.com\n,guest@example.com,test@example.com\n", site['SiteMember'].load)
  end

  def test_plugin
    # test_member

    @site = Qwik::Site.new(@config, 'test')
    @pagename = 'TestPage'
    options = Hash.new('')
    options['req']      = @req
    options['memory']   = @memory
    options['site']     = @site
    options['pagename'] = @pagename
    @plugin = Qwik::Plugin.new(@config, options)
    @site.plugin = @plugin

    page = @site.pages.create_new
    page.store("{{member_add_form}}")
    xml = page.load.w.res(@site)
    assert_equal("<body><p><div class='form'><form action='TestPage.html' method='POST'><input name='page' type='hidden' value='TestPage'/><table class='form'><tr><td class='midashi'>あなたのメール</td><td class='nakami'><span></span></td></tr><tr><td class='midashi'>追加メール</td><td class='nakami'><input name='tomail' size='must' value=''/></td></tr><tr><td class='msg' colspan='2'>追加したい人のメールアドレスを入力してください。</td></tr><tr><td class='midashi'></td><td class='nakami'><input class='submit' type='submit' value=' 追加 '/></td></tr></table></form></div></p>\n</body>", xml.to_s)
  end

  def teardown()
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end
end
