#!/usr/bin/env ruby
# $Id: tc_ext_password.rb,v 1.1 2004/04/07 06:22:02 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/page'
require 'qwik/ext_password'

class TC_PasswordPage < Test::Unit::TestCase
  def setup()
    @config = Qwik::Config.new(true)
    @dir = './test/'.path
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if ! @dir.directory?
    @pages = Qwik::Pages.new(@config, @dir)
  end

  def test_password
    page = @pages.create("1")
    page.store("test2")
    assert_equal("test2", page.load)
    text = page.embed_password("take1 {{password(testpassword)}}") # text becomes password embeded text
    assert_match(/\{\{password\(/, text)
    assert_no_match(/testpassword/, text) # there is no original pass
    page.store(text)
    assert_equal(true, page.have_password?)

    take2 = page.embed_password("take2 {{password(notcorrectpassword)}}")
    assert_equal(false, page.match_password?(take2)) # NG

    take3 = page.embed_password("take3 {{password(testpassword)}}")
    assert_equal(true, page.match_password?(take3))
    page.store(take3) # OK
    assert_match(/take3/, page.load)

    assert_equal(true, page.have_password?)
    assert_equal(32, page.password().length)
    assert_equal("testpassword".md5hex, page.password())
  end

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