store unknown servers in the invitation process answer and finalize + server storage fixes and testing
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2024-06-05 14:45:01 +02:00
parent c0fd74f1e6
commit af55df1ff5
4 changed files with 172 additions and 4 deletions

View File

@ -21,7 +21,7 @@ func TestGetUid(t *testing.T) {
}
func TestStoreServer(t *testing.T) {
GetConfig().SetMemPass("test")
createId()
ss := ServerStorage{DbFile: "test.db"}
srv := Server{
Name: "test",
@ -49,6 +49,7 @@ func TestStoreServer(t *testing.T) {
}
func TestLoadServersFromUids(t *testing.T) {
createId()
GetConfig().SetMemPass("test")
ss := ServerStorage{DbFile: "test.db"}
srv := Server{
@ -76,7 +77,7 @@ func TestLoadServersFromUids(t *testing.T) {
}
func TestLoadServerCardsFromUids(t *testing.T) {
GetConfig().SetMemPass("test")
createId()
ss := ServerStorage{DbFile: "test.db"}
srv := Server{
Name: "test",
@ -101,3 +102,123 @@ func TestLoadServerCardsFromUids(t *testing.T) {
// recursively remove the test.db folder
os.RemoveAll("test.db")
}
func TestServerExists(t *testing.T) {
createId()
ss := ServerStorage{DbFile: "test.db"}
server := &Server{
Name: "test",
Url: "http://localhost:8080",
PublicKey: meowlib.NewKeyPair().Public,
}
// Check if server exists before storing it
exists, err := ss.ServerExists(server)
if err != nil {
t.Errorf("Failed to check if server exists: %v", err)
}
if exists {
t.Errorf("Server exists before storing it")
}
// Store the server
err = ss.StoreServer(server)
if err != nil {
t.Errorf("Failed to store server: %v", err)
}
// Check if server exists after storing it
exists, err = ss.ServerExists(server)
if err != nil {
t.Errorf("Failed to check if server exists: %v", err)
}
if !exists {
t.Errorf("Server does not exist after storing it")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}
func TestStoreServerIfNotExists(t *testing.T) {
createId()
ss := ServerStorage{DbFile: "test.db"}
server := &Server{
Name: "test",
Url: "http://localhost:8080",
PublicKey: meowlib.NewKeyPair().Public,
}
// Check if server exists before storing it
exists, err := ss.ServerExists(server)
if err != nil {
t.Errorf("Failed to check if server exists: %v", err)
}
if exists {
t.Errorf("Server exists before storing it")
}
// Store the server if it does not exist
err = ss.StoreServerIfNotExists(server)
if err != nil {
t.Errorf("Failed to store server: %v", err)
}
// Check if server exists after storing it
exists, err = ss.ServerExists(server)
if err != nil {
t.Errorf("Failed to check if server exists: %v", err)
}
if !exists {
t.Errorf("Server does not exist after storing it")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}
func TestStoreServerIfNotExists_ServerExists(t *testing.T) {
createId()
ss := ServerStorage{DbFile: "test.db"}
server := &Server{
Name: "test",
Url: "http://localhost:8080",
PublicKey: meowlib.NewKeyPair().Public,
}
// Store the server
err := ss.StoreServer(server)
if err != nil {
t.Errorf("Failed to store server: %v", err)
}
// Store the server again with a different public key
newServer := &Server{
Name: "test",
Url: "http://localhost:8080",
PublicKey: meowlib.NewKeyPair().Public,
}
err = ss.StoreServerIfNotExists(newServer)
if err != nil {
t.Errorf("Failed to store server: %v", err)
}
// Retrieve the server and check if the public key has not changed
storedServer, err := ss.LoadServer(server.GetServerCard().GetUid())
if err != nil {
t.Errorf("Failed to get server: %v", err)
}
if storedServer.PublicKey != server.PublicKey {
t.Errorf("Public key was modified")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}