Add various deleted flags to entities | Add active to subscriptions | Add DeleteUser && DeleteChannel endpoints [skip-tests]
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
)
|
||||
|
||||
func TestCreateChannel(t *testing.T) {
|
||||
@@ -1233,3 +1234,94 @@ func TestChannelMessageCounter(t *testing.T) {
|
||||
assertCounter(6, 1, 3)
|
||||
|
||||
}
|
||||
|
||||
func TestDeleteChannel(t *testing.T) {
|
||||
ws, baseUrl, stop := tt.StartSimpleWebserver(t)
|
||||
defer stop()
|
||||
|
||||
// Initialize default data set
|
||||
data := tt.InitDefaultData(t, ws)
|
||||
|
||||
// User 16 owns channels, User 1 will subscribe
|
||||
user16 := data.User[16]
|
||||
user1 := data.User[1]
|
||||
|
||||
// Find channel "Chan2" belonging to user 16
|
||||
var chan2 tt.ChanData
|
||||
for _, ch := range user16.Channels {
|
||||
if ch.InternalName == "Chan2" {
|
||||
chan2 = ch
|
||||
break
|
||||
}
|
||||
}
|
||||
tt.AssertNotEqual(t, "Channel Chan2 ID", "", chan2.ChannelID) // Ensure channel was found
|
||||
|
||||
// --- Subscribe User 1 to User 16's Chan2 ---
|
||||
chanInfo := tt.RequestAuthGet[gin.H](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s", user16.UID, chan2.ChannelID))
|
||||
subKey := chanInfo["subscribe_key"].(string)
|
||||
|
||||
subReq := tt.RequestAuthPost[gin.H](t, user1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?chan_subscribe_key=%s", user1.UID, subKey), gin.H{
|
||||
"channel_id": chan2.ChannelID, // Provide channel ID for subscription
|
||||
})
|
||||
subscriptionID := subReq["subscription_id"].(string)
|
||||
|
||||
// Confirm subscription by owner (user 16)
|
||||
tt.RequestAuthPatch[gin.H](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions/%s", user16.UID, subscriptionID), gin.H{
|
||||
"confirmed": true,
|
||||
})
|
||||
|
||||
// --- Pre-checks ---
|
||||
|
||||
// 1. Check channel exists
|
||||
tt.RequestAuthGet[gin.H](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s", user16.UID, chan2.ChannelID))
|
||||
|
||||
// 2. Check channel messages exist (assuming mglist type from previous tests)
|
||||
type msg struct {
|
||||
MessageId string `json:"message_id"`
|
||||
}
|
||||
type mglist struct {
|
||||
Messages []msg `json:"messages"`
|
||||
}
|
||||
msgs := tt.RequestAuthGet[mglist](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", user16.UID, chan2.ChannelID))
|
||||
tt.AssertTrue(t, "pre-check messages exist", len(msgs.Messages) > 0)
|
||||
|
||||
// 3. Check subscription exists for User 1 (outgoing)
|
||||
type subobj struct {
|
||||
SubscriptionId string `json:"subscription_id"`
|
||||
}
|
||||
type sublist struct {
|
||||
Subscriptions []subobj `json:"subscriptions"`
|
||||
}
|
||||
subs1 := tt.RequestAuthGet[sublist](t, user1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?direction=outgoing", user1.UID))
|
||||
foundSub1 := langext.ArrAny(subs1.Subscriptions, func(v subobj) bool { return v.SubscriptionId == subscriptionID })
|
||||
tt.AssertTrue(t, "pre-check user1 subs outgoing", foundSub1)
|
||||
|
||||
// 4. Check subscription exists for User 16 (incoming)
|
||||
subs16 := tt.RequestAuthGet[sublist](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?direction=incoming", user16.UID))
|
||||
foundSub16 := langext.ArrAny(subs16.Subscriptions, func(v subobj) bool { return v.SubscriptionId == subscriptionID })
|
||||
tt.AssertTrue(t, "pre-check user16 subs incoming", foundSub16)
|
||||
|
||||
// --- Delete Channel ---
|
||||
tt.RequestAuthDelete[tt.Void](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s", user16.UID, chan2.ChannelID), nil)
|
||||
|
||||
// --- Post-checks ---
|
||||
|
||||
// 1. Check channel fetch fails
|
||||
tt.RequestAuthGetShouldFail(t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s", user16.UID, chan2.ChannelID), 404, apierr.CHANNEL_NOT_FOUND)
|
||||
|
||||
// 2. Check channel messages fetch fails
|
||||
tt.RequestAuthGetShouldFail(t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", user16.UID, chan2.ChannelID), 404, apierr.CHANNEL_NOT_FOUND)
|
||||
|
||||
// Check subscriber cannot fetch messages either
|
||||
tt.RequestAuthGetShouldFail(t, user1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels/%s/messages", user16.UID, chan2.ChannelID), 404, apierr.CHANNEL_NOT_FOUND) // Auth fails because subscription is gone
|
||||
|
||||
// 3. Check subscription is gone for User 1
|
||||
subs1After := tt.RequestAuthGet[sublist](t, user1.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?direction=outgoing", user1.UID))
|
||||
foundSub1After := langext.ArrAny(subs1After.Subscriptions, func(v subobj) bool { return v.SubscriptionId == subscriptionID })
|
||||
tt.AssertEqual(t, "post-check user1 subs outgoing", false, foundSub1After)
|
||||
|
||||
// 4. Check subscription is gone for User 16
|
||||
subs16After := tt.RequestAuthGet[sublist](t, user16.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/subscriptions?direction=incoming", user16.UID))
|
||||
foundSub16After := langext.ArrAny(subs16After.Subscriptions, func(v subobj) bool { return v.SubscriptionId == subscriptionID })
|
||||
tt.AssertEqual(t, "post-check user16 subs incoming", false, foundSub16After)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user