// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package engine

import (
	"testing"

	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
)

// Test the export of the keys.
func TestPGPPurgeLksec(t *testing.T) {
	tc := SetupEngineTest(t, "purge")
	defer tc.Cleanup()

	createFakeUserWithPGPSibkeyPaper(tc)

	idUI := &FakeIdentifyUI{
		Proofs: make(map[string]string),
	}
	uis := libkb.UIs{
		SecretUI:   &libkb.TestSecretUI{}, // empty on purpose...shouldn't be necessary
		SaltpackUI: &fakeSaltpackUI{},
		IdentifyUI: idUI,
	}
	eng := NewPGPPurge(tc.G, keybase1.PGPPurgeArg{})
	m := NewMetaContextForTest(tc).WithUIs(uis)
	if err := RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	if len(eng.KeyFiles()) != 1 {
		t.Fatalf("number of exported key files: %d, expected 1", len(eng.KeyFiles()))
	}
}

// Test the removal of the keys.
func TestPGPPurgeRemove(t *testing.T) {
	tc := SetupEngineTest(t, "purge")
	defer tc.Cleanup()

	u := createFakeUserWithPGPSibkey(tc)

	idUI := &FakeIdentifyUI{
		Proofs: make(map[string]string),
	}
	uis := libkb.UIs{
		SecretUI:   &libkb.TestSecretUI{}, // empty on purpose...shouldn't be necessary
		SaltpackUI: &fakeSaltpackUI{},
		IdentifyUI: idUI,
	}
	eng := NewPGPPurge(tc.G, keybase1.PGPPurgeArg{DoPurge: true})
	m := NewMetaContextForTest(tc).WithUIs(uis)
	if err := RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	if len(eng.KeyFiles()) != 1 {
		t.Fatalf("number of exported key files: %d, expected 1", len(eng.KeyFiles()))
	}

	kr := libkb.NewSKBKeyringFile(tc.G, libkb.NewNormalizedUsername(u.Username))
	if err := kr.LoadAndIndex(m.Ctx()); err != nil {
		t.Fatal(err)
	}
	if kr.HasPGPKeys() {
		t.Fatal("after purge, keyring has pgp keys")
	}

	// redo, should purge 0 files

	eng = NewPGPPurge(tc.G, keybase1.PGPPurgeArg{DoPurge: true})
	if err := RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	if len(eng.KeyFiles()) != 0 {
		t.Fatalf("number of exported key files: %d, expected 0", len(eng.KeyFiles()))
	}

}

// Create a user with a synced PGP key.  PGPPurge shouldn't touch it.
func TestPGPPurgeSync(t *testing.T) {
	tc := SetupEngineTest(t, "purge")
	u1 := createFakeUserWithPGPOnly(t, tc)
	Logout(tc)
	tc.Cleanup()

	tc = SetupEngineTest(t, "purge")
	defer tc.Cleanup()

	uis := libkb.UIs{
		ProvisionUI: newTestProvisionUIPassphrase(),
		LoginUI:     &libkb.TestLoginUI{Username: u1.Username},
		LogUI:       tc.G.UI.GetLogUI(),
		SecretUI:    u1.NewSecretUI(),
		GPGUI:       &gpgtestui{},
	}
	leng := NewLogin(tc.G, keybase1.DeviceTypeV2_DESKTOP, "", keybase1.ClientType_CLI)
	m := NewMetaContextForTest(tc).WithUIs(uis)
	if err := RunEngine2(m, leng); err != nil {
		t.Fatal(err)
	}

	// user has device keys + synced 3sec pgp key

	idUI := &FakeIdentifyUI{
		Proofs: make(map[string]string),
	}
	uis = libkb.UIs{
		SecretUI:   &libkb.TestSecretUI{}, // empty on purpose...shouldn't be necessary
		SaltpackUI: &fakeSaltpackUI{},
		IdentifyUI: idUI,
	}
	eng := NewPGPPurge(tc.G, keybase1.PGPPurgeArg{})
	m = NewMetaContextForTest(tc).WithUIs(uis)
	if err := RunEngine2(m, eng); err != nil {
		t.Fatal(err)
	}

	if len(eng.KeyFiles()) != 0 {
		t.Fatalf("number of exported key files: %d, expected 0", len(eng.KeyFiles()))
	}
}
