<commit_before>package main

import (
	"github.com/BytemarkHosting/bytemark-client/lib"
	"github.com/BytemarkHosting/bytemark-client/lib/brain"
	"net"
	"strings"
	"testing"
)

func TestAddKeyCommand(t *testing.T) {
	config, c := baseTestSetup(t, false)

	config.When("Get", "token").Return("test-token")
	config.When("GetIgnoreErr", "yubikey").Return("")
	config.When("GetIgnoreErr", "user").Return("test-user")

	c.When("AuthWithToken", "test-token").Return(nil).Times(1)
	c.When("AddUserAuthorizedKey", "test-user", "ssh-rsa aaaaawhartevervAsde fake key").Times(1)

	err := global.App.Run(strings.Split("bytemark add key --user test-user ssh-rsa aaaaawhartevervAsde fake key", " "))
	if err != nil {
		t.Error(err)
	}

	if ok, err := config.Verify(); !ok {
		t.Fatal(err)
	}
	if ok, err := c.Verify(); !ok {
		t.Fatal(err)
	}
	c.Reset()
}
func TestAddIPCommand(t *testing.T) {
	config, c := baseTestSetup(t, false)

	config.When("Get", "token").Return("test-token")
	config.When("GetIgnoreErr", "yubikey").Return("")
	config.When("GetIgnoreErr", "user").Return("test-user")
	config.When("GetVirtualMachine").Return(&defVM)

	vm := lib.VirtualMachineName{VirtualMachine: "test-server"}
	c.When("ParseVirtualMachineName", "test-server", []*lib.VirtualMachineName{&defVM}).Return(&vm, nil).Times(1)

	ipcr := brain.IPCreateRequest{
		Addresses:  1,
		Family:     "ipv4",
		Reason:     "testing",
		Contiguous: false,
	}

	ip := net.ParseIP("10.10.10.10")

	ipcres := ipcr
	ipcres.IPs = []*net.IP{&ip}<commit_msg>Add additional tests for AddUserAuthorizedKey<commit_after>package main

import (
	"github.com/BytemarkHosting/bytemark-client/lib"
	"github.com/BytemarkHosting/bytemark-client/lib/brain"
	"io/ioutil"
	"net"
	"os"
	"strings"
	"testing"
)

func TestAddKeyCommand(t *testing.T) {
	config, c := baseTestSetup(t, false)

	config.When("Get", "token").Return("test-token")
	config.When("GetIgnoreErr", "yubikey").Return("")
	config.When("GetIgnoreErr", "user").Return("test-user")

	err := ioutil.WriteFile("testkey.pub", []byte("ssh-rsa aaaaawhartevervAsde fake key"), 0600)
	if err != nil {
		t.Error(err)
	}

	c.When("AuthWithToken", "test-token").Return(nil).Times(1)
	c.When("AddUserAuthorizedKey", "test-user", "ssh-rsa aaaaawhartevervAsde fake key").Times(1)
	err = global.App.Run(strings.Split("bytemark add key --user test-user ssh-rsa aaaaawhartevervAsde fake key", " "))
	if err != nil {
		t.Error(err)
	}
	if ok, err := c.Verify(); !ok {
		t.Fatal(err)
	}

	c.Reset()
	c.When("AuthWithToken", "test-token").Return(nil).Times(1)
	c.When("AddUserAuthorizedKey", "test-user", "ssh-rsa aaaaawhartevervAsde fake key").Times(1)
	err = global.App.Run([]string{"bytemark", "add", "key", "--user", "test-user", "testkey.pub"})
	if err != nil {
		t.Error(err)
	}
	if ok, err := c.Verify(); !ok {
		t.Fatal(err)
	}

	c.Reset()
	c.When("AuthWithToken", "test-token").Return(nil).Times(1)
	c.When("AddUserAuthorizedKey", "test-user", "ssh-rsa aaaaawhartevervAsde fake key").Times(1)
	err = global.App.Run([]string{"bytemark", "add", "key", "--user", "test-user", "--public-key-file", "testkey.pub"})
	if err != nil {
		t.Error(err)
	}
	if ok, err := c.Verify(); !ok {
		t.Fatal(err)
	}

	_ = os.Remove("testkey.pub")
}

func TestAddIPCommand(t *testing.T) {
	config, c := baseTestSetup(t, false)

	config.When("Get", "token").Return("test-token")
	config.When("GetIgnoreErr", "yubikey").Return("")
	config.When("GetIgnoreErr", "user").Return("test-user")
	config.When("GetVirtualMachine").Return(&defVM)

	vm := lib.VirtualMachineName{VirtualMachine: "test-server"}
	c.When("ParseVirtualMachineName", "test-server", []*lib.VirtualMachineName{&defVM}).Return(&vm, nil).Times(1)

	ipcr := brain.IPCreateRequest{
		Addresses:  1,
		Family:     "ipv4",
		Reason:     "testing",
		Contiguous: false,
	}

	ip := net.ParseIP("10.10.10.10")

	ipcres := ipcr
	ipcres.IPs = []*net.IP{&ip}
{'fraction_non_alphanumeric': 0.2029881504379186, 'fraction_numerical': 0.008758371973209686, 'mean_word_length': 4.955521472392638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84169', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1529, 'n_words': 249}