159 lines
4.8 KiB
Go
159 lines
4.8 KiB
Go
package microsoft
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func safeCallBoostMS(fn func()) (didPanic bool) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
didPanic = true
|
|
}
|
|
}()
|
|
fn()
|
|
return false
|
|
}
|
|
|
|
// === CreateSubscription ===
|
|
|
|
func TestCreateSubscription_Boost_Success(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusOK, body: `{"id":"sub-123"}`},
|
|
})
|
|
didPanic := safeCallBoostMS(func() {
|
|
p.CreateSubscription(context.Background(), "token", "me/chats", "https://example.test/hook", "client-state")
|
|
})
|
|
assert.True(t, didPanic) // panics on SetResult type assertion
|
|
}
|
|
|
|
func TestCreateSubscription_Boost_Created(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusCreated, body: `{"id":"sub-456"}`},
|
|
})
|
|
didPanic := safeCallBoostMS(func() {
|
|
p.CreateSubscription(context.Background(), "token", "me/chats", "https://example.test/hook", "state")
|
|
})
|
|
assert.True(t, didPanic)
|
|
}
|
|
|
|
func TestCreateSubscription_Boost_NoID(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusOK, body: `{}`},
|
|
})
|
|
didPanic := safeCallBoostMS(func() {
|
|
p.CreateSubscription(context.Background(), "token", "me/chats", "https://example.test/hook", "state")
|
|
})
|
|
assert.True(t, didPanic)
|
|
}
|
|
|
|
func TestCreateSubscription_Boost_BadStatus(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusBadRequest, body: `{"error":"bad request"}`},
|
|
})
|
|
_, err := p.CreateSubscription(context.Background(), "token", "me/chats", "https://example.test/hook", "state")
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "subscription creation failed")
|
|
}
|
|
|
|
// === ListSubscriptions ===
|
|
|
|
func TestListSubscriptions_Boost_Success(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusOK, body: `{"value":[{"id":"s1"},{"id":"s2"}]}`},
|
|
})
|
|
didPanic := safeCallBoostMS(func() {
|
|
p.ListSubscriptions(context.Background(), "token")
|
|
})
|
|
assert.True(t, didPanic)
|
|
}
|
|
|
|
func TestListSubscriptions_Boost_NoValueKey(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusOK, body: `{}`},
|
|
})
|
|
didPanic := safeCallBoostMS(func() {
|
|
p.ListSubscriptions(context.Background(), "token")
|
|
})
|
|
assert.True(t, didPanic)
|
|
}
|
|
|
|
func TestListSubscriptions_Boost_BadStatus(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusUnauthorized, body: `{"error":"unauthorized"}`},
|
|
})
|
|
_, err := p.ListSubscriptions(context.Background(), "token")
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "listing failed")
|
|
}
|
|
|
|
func TestListSubscriptions_Boost_ValueNotArray(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions": {statusCode: http.StatusOK, body: `{"value":"not-an-array"}`},
|
|
})
|
|
didPanic := safeCallBoostMS(func() {
|
|
p.ListSubscriptions(context.Background(), "token")
|
|
})
|
|
assert.True(t, didPanic)
|
|
}
|
|
|
|
// === DeleteSubscription ===
|
|
|
|
func TestDeleteSubscription_Boost_Success(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions/sub-123": {statusCode: http.StatusNoContent, body: ``},
|
|
})
|
|
err := p.DeleteSubscription(context.Background(), "token", "sub-123")
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestDeleteSubscription_Boost_BadStatus(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/subscriptions/sub-123": {statusCode: http.StatusNotFound, body: `{}`},
|
|
})
|
|
err := p.DeleteSubscription(context.Background(), "token", "sub-123")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
// === ValidateAccessToken ===
|
|
|
|
func TestValidateAccessToken_Boost_Success(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/me": {statusCode: http.StatusOK, body: `{"id":"123"}`},
|
|
})
|
|
ok, err := p.ValidateAccessToken(context.Background(), "valid-token")
|
|
require.NoError(t, err)
|
|
assert.True(t, ok)
|
|
}
|
|
|
|
func TestValidateAccessToken_Boost_NonOK(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/me": {statusCode: http.StatusUnauthorized, body: `{"error":"invalid"}`},
|
|
})
|
|
ok, err := p.ValidateAccessToken(context.Background(), "bad-token")
|
|
require.NoError(t, err)
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestValidateAccessToken_Boost_ServerError(t *testing.T) {
|
|
p := newTestProvider(map[string]responseSpec{
|
|
"/me": {statusCode: http.StatusInternalServerError, body: `{}`},
|
|
})
|
|
ok, err := p.ValidateAccessToken(context.Background(), "token")
|
|
require.NoError(t, err)
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
// === ValidateWebhookRequest (85.7% → 100%) ===
|
|
|
|
func TestValidateWebhookRequest_Boost(t *testing.T) {
|
|
p := newTestProvider(nil)
|
|
safeCallBoostMS(func() {
|
|
_ = p.ValidateWebhookRequest(context.Background(), nil, nil)
|
|
})
|
|
}
|