# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

module Qwik
  module SitePassword # has relation with cmd_save
    def password
      SitePassword.get_pass(load)
    end

    def have_password?
      self.password != nil
    end

    def embed_password(v)
      new_pass = SitePassword.get_pass(v)
      return v if !new_pass
      v.sub(/\{\{password\((.+)\)\}\}/){"{{password("+$1.md5hex+")}}"}
    end

    def match_password?(v)
      return true if !have_password? # go on
      old_pass_md5 = self.password
      new_pass_md5 = SitePassword.get_pass(v) # assume already become md5
      return false if !new_pass_md5 # can not go if there is no pass
      return false if new_pass_md5 != old_pass_md5 # should be correct pass
      true # go on
    end

    def SitePassword.get_pass(str)
      return $1 if str && str =~ /\{\{password\((.+)\)\}\}/
      nil
    end
  end

  class Page
    include SitePassword
  end

  class Action
    def plg_password(pass=nil)
      ""
    end
  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

if $debug
  class TC_PasswordPage < Test::Unit::TestCase
    include TestSession

    def test_password
      @pages = Qwik::Pages.new(@config, @dir)
      page = @pages.create("1")
      page.store("test2")
      assert_equal("test2", page.load)
      # text becomes password embeded text
      text = page.embed_password("take1 {{password(testpassword)}}")
      assert_match(/\{\{password\(/, text)
      assert_no_match(/testpassword/, text) # there is no original password
      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)
      @pages.erase_all
    end

    def test_usecase_password
      t_add_user

      page = @site.create_new
      page.store("test\n")
      session("/test/1.html")
      assert_xml("test", "//div[@class='section']/p")

      session("/test/1.edit")
      assert_text("test\n", "textarea")

      session("/test/1.save?contents=test%0A{{password(test)}}%0A")
      assert_text("Page is saved.", "title")

      session("/test/1.edit")
      assert_text("test\n{{password(098f6bcd4621d373cade4e832627b4f6)}}\n",
		  "textarea")

      session("/test/1.save?contents=t2%0A{{password(test)}}%0A")
      assert_text("Page is saved.", "title")

      session("/test/1.edit")
      assert_text("t2\n{{password(098f6bcd4621d373cade4e832627b4f6)}}\n",
		  "textarea")

      session("/test/1.save?contents=t3%0A{{password(nomatch)}}%0A")
      #px("//[@class='msg']", 0)
      assert_in([[:tt, "{{password("], "Password", [:tt, ")}}"]],
		 "//div[@class='section']/p")
      assert_text("t3\n{{password(nomatch)}}\n", "textarea")

      session("/test/1.save?contents=t3%0A{{password(test)}}%0A")
      assert_text("Page is saved.", "title")
      assert_equal("t3\n{{password(098f6bcd4621d373cade4e832627b4f6)}}\n",
		   page.load)
    end
  end
end
