Files
oc-lib/models/peer/tests/peer_test.go

110 lines
3.1 KiB
Go
Raw Normal View History

2025-06-16 11:24:39 +02:00
package peer_test
import (
"testing"
"cloud.o-forge.io/core/oc-lib/models/peer"
2026-02-18 12:24:19 +01:00
"cloud.o-forge.io/core/oc-lib/tools"
"github.com/stretchr/testify/assert"
2025-06-16 11:24:39 +02:00
)
2026-02-18 12:24:19 +01:00
// ---- PeerRelation ----
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
func TestPeerRelation_String(t *testing.T) {
assert.Equal(t, "UNKNOWN", peer.NONE.String())
assert.Equal(t, "SELF", peer.SELF.String())
assert.Equal(t, "PARTNER", peer.PARTNER.String())
assert.Equal(t, "BLACKLIST", peer.BLACKLIST.String())
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeerRelation_Path(t *testing.T) {
assert.Equal(t, "unknown", peer.NONE.Path())
assert.Equal(t, "self", peer.SELF.Path())
assert.Equal(t, "partner", peer.PARTNER.Path())
assert.Equal(t, "blacklist", peer.BLACKLIST.Path())
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeerRelation_EnumIndex(t *testing.T) {
assert.Equal(t, 0, peer.NONE.EnumIndex())
assert.Equal(t, 1, peer.SELF.EnumIndex())
assert.Equal(t, 2, peer.PARTNER.EnumIndex())
assert.Equal(t, 3, peer.BLACKLIST.EnumIndex())
assert.Equal(t, 4, peer.PENDING_PARTNER.EnumIndex())
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestGetRelationPath(t *testing.T) {
assert.Equal(t, 1, peer.GetRelationPath("self"))
assert.Equal(t, 2, peer.GetRelationPath("partner"))
assert.Equal(t, 3, peer.GetRelationPath("blacklist"))
assert.Equal(t, -1, peer.GetRelationPath("nonexistent"))
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
// ---- Peer model ----
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
func TestPeer_VerifyAuth(t *testing.T) {
p := &peer.Peer{}
assert.True(t, p.VerifyAuth("get", nil))
assert.True(t, p.VerifyAuth("delete", &tools.APIRequest{}))
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeer_CanDelete(t *testing.T) {
p := &peer.Peer{}
assert.False(t, p.CanDelete())
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeer_GetAccessor(t *testing.T) {
p := &peer.Peer{}
req := &tools.APIRequest{}
acc := p.GetAccessor(req)
assert.NotNil(t, acc)
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeer_AddExecution_Deduplication(t *testing.T) {
p := &peer.Peer{}
exec := peer.PeerExecution{Method: "POST", Url: "http://peer/data", Body: "body1"}
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
p.AddExecution(exec)
assert.Len(t, p.FailedExecution, 1)
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
// Second add of same execution should not duplicate
p.AddExecution(exec)
assert.Len(t, p.FailedExecution, 1)
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
// Different execution should be added
exec2 := peer.PeerExecution{Method: "GET", Url: "http://peer/data", Body: nil}
p.AddExecution(exec2)
assert.Len(t, p.FailedExecution, 2)
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeer_RemoveExecution(t *testing.T) {
p := &peer.Peer{}
exec1 := peer.PeerExecution{Method: "POST", Url: "http://peer/a", Body: nil}
exec2 := peer.PeerExecution{Method: "DELETE", Url: "http://peer/b", Body: nil}
p.AddExecution(exec1)
p.AddExecution(exec2)
assert.Len(t, p.FailedExecution, 2)
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
p.RemoveExecution(exec1)
assert.Len(t, p.FailedExecution, 1)
assert.Equal(t, exec2, p.FailedExecution[0])
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeer_RemoveExecution_NotFound(t *testing.T) {
p := &peer.Peer{}
exec := peer.PeerExecution{Method: "POST", Url: "http://peer/x", Body: nil}
p.AddExecution(exec)
2025-06-16 11:24:39 +02:00
2026-02-18 12:24:19 +01:00
other := peer.PeerExecution{Method: "DELETE", Url: "http://other/x", Body: nil}
p.RemoveExecution(other)
assert.Len(t, p.FailedExecution, 1) // unchanged
2025-06-16 11:24:39 +02:00
}
2026-02-18 12:24:19 +01:00
func TestPeer_RemoveExecution_Empty(t *testing.T) {
p := &peer.Peer{}
// Should not panic on empty list
exec := peer.PeerExecution{Method: "GET", Url: "http://peer/x", Body: nil}
p.RemoveExecution(exec)
assert.Empty(t, p.FailedExecution)
2025-06-16 11:24:39 +02:00
}