#!/usr/bin/env ruby
# $Id: tc_ext_member.rb,v 1.10 2004/05/12 18:23:34 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_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("ABAB4B32", pw.generate("test@example.com"))
    assert_equal("7EF7A55A", 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
パスワード:	ABAB4B32

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
パスワード:	ABAB4B32

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_SiteMember < Test::Unit::TestCase
  include TestSite

  def test_member
    @user = 'test@example.com'
    member = @site.member
    page = @site.create('SiteMember')
    assert_equal(false, member.exist?(@user))
    page.store("test@example.com");	assert(!member.exist?(@user))
    page.store(",test@example.com");	assert(member.exist?(@user))
    page.store(",test@example.com,");	assert(member.exist?(@user))
    page.store(",test@example.com,a");	assert(member.exist?(@user))
    page.store(",test@example.com,a,");	assert(member.exist?(@user))
    member.remove(@user);		assert(!member.exist?(@user))
    assert_equal("", page.load)
    member.add(@user);			assert(member.exist?(@user))
    guest = 'guest@example.com'
    member.invite(guest, @user);	assert(member.exist?(guest))
    assert_equal(",test@example.com,\n,guest@example.com,test@example.com\n", page.load)
    assert_equal(["test@example.com", "guest@example.com"], member.list)
    member.remove(guest);		assert(!member.exist?(guest))
    @site.delete('SiteMember');	assert(!member.exist?(@user))
    assert_equal([], member.list)

    # test_quickml_member
    assert(!member.exist?(@user))
    store("test@example.com");		assert(member.exist?(@user))
    assert_equal(["test@example.com"], member.list)
    store("# test@example.com");	assert(!member.exist?(@user))
    store("; test@example.com");	assert(!member.exist?(@user))
    store("");				assert(!member.exist?(@user))
    assert_equal([], member.list)

    # test_obfuscate
    page = @site['SiteConfig']
    assert_match(/:open:false/, page.load)
    assert_equal(false, @site.is_open?)
    page.store(":open:true")
    assert_equal(":open:true", page.load)
    assert_equal(true, @site.is_open?)

    member.add(@user)
    member.add('guest@example.com')
    assert_equal(["test@e...", "guest@e..."], member.list)
  end

  def store(content) # quickml_member
    (@dir+",members").open("wb"){|out|
      out.puts(content)
    }
  end
end

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

  def test_member_plugin
    @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)

    assert_equal("test@example.com", "{{member_user}}".w.html_p(@site))
    assert_equal("<div><ul><li>guest@example.com</li><li>test@example.com</li></ul><p>total: 2 members</p></div>", "{{member_list}}".w.html_p(@site))
    assert_match(/<textarea/, "{{member_list_csv}}".w.html_p(@site))
    assert_match(/guest@example.com, test@example.com/, "{{member_list_csv}}".w.html_p(@site))

  end
end
