@@ -11,6 +11,7 @@ import (
11
11
"github.com/shellhub-io/shellhub/pkg/api/authorizer"
12
12
"github.com/shellhub-io/shellhub/pkg/api/requests"
13
13
storecache "github.com/shellhub-io/shellhub/pkg/cache"
14
+ cachemock "github.com/shellhub-io/shellhub/pkg/cache/mocks"
14
15
"github.com/shellhub-io/shellhub/pkg/clock"
15
16
clockmock "github.com/shellhub-io/shellhub/pkg/clock/mocks"
16
17
"github.com/shellhub-io/shellhub/pkg/envs"
@@ -1565,6 +1566,7 @@ func TestService_LeaveNamespace(t *testing.T) {
1565
1566
}
1566
1567
1567
1568
storeMock := new (storemock.Store )
1569
+ cacheMock := new (cachemock.Cache )
1568
1570
1569
1571
cases := []struct {
1570
1572
description string
@@ -1575,8 +1577,9 @@ func TestService_LeaveNamespace(t *testing.T) {
1575
1577
{
1576
1578
description : "fails when the namespace was not found" ,
1577
1579
req : & requests.LeaveNamespace {
1578
- UserID : "000000000000000000000000" ,
1579
- TenantID : "00000000-0000-4000-0000-000000000000" ,
1580
+ UserID : "000000000000000000000000" ,
1581
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1582
+ AuthenticatedTenantID : "00000000-0000-4000-0000-000000000001" ,
1580
1583
},
1581
1584
requiredMocks : func (ctx context.Context ) {
1582
1585
storeMock .
@@ -1589,8 +1592,9 @@ func TestService_LeaveNamespace(t *testing.T) {
1589
1592
{
1590
1593
description : "fails when the user is not on the namespace" ,
1591
1594
req : & requests.LeaveNamespace {
1592
- UserID : "000000000000000000000000" ,
1593
- TenantID : "00000000-0000-4000-0000-000000000000" ,
1595
+ UserID : "000000000000000000000000" ,
1596
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1597
+ AuthenticatedTenantID : "00000000-0000-4000-0000-000000000001" ,
1594
1598
},
1595
1599
requiredMocks : func (ctx context.Context ) {
1596
1600
storeMock .
@@ -1608,8 +1612,9 @@ func TestService_LeaveNamespace(t *testing.T) {
1608
1612
{
1609
1613
description : "fails when the user is owner" ,
1610
1614
req : & requests.LeaveNamespace {
1611
- UserID : "000000000000000000000000" ,
1612
- TenantID : "00000000-0000-4000-0000-000000000000" ,
1615
+ UserID : "000000000000000000000000" ,
1616
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1617
+ AuthenticatedTenantID : "00000000-0000-4000-0000-000000000001" ,
1613
1618
},
1614
1619
requiredMocks : func (ctx context.Context ) {
1615
1620
storeMock .
@@ -1632,8 +1637,9 @@ func TestService_LeaveNamespace(t *testing.T) {
1632
1637
{
1633
1638
description : "fails when cannot remove the member" ,
1634
1639
req : & requests.LeaveNamespace {
1635
- UserID : "000000000000000000000000" ,
1636
- TenantID : "00000000-0000-4000-0000-000000000000" ,
1640
+ UserID : "000000000000000000000000" ,
1641
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1642
+ AuthenticatedTenantID : "00000000-0000-4000-0000-000000000001" ,
1637
1643
},
1638
1644
requiredMocks : func (ctx context.Context ) {
1639
1645
storeMock .
@@ -1660,8 +1666,38 @@ func TestService_LeaveNamespace(t *testing.T) {
1660
1666
{
1661
1667
description : "succeeds" ,
1662
1668
req : & requests.LeaveNamespace {
1663
- UserID : "000000000000000000000000" ,
1664
- TenantID : "00000000-0000-4000-0000-000000000000" ,
1669
+ UserID : "000000000000000000000000" ,
1670
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1671
+ AuthenticatedTenantID : "00000000-0000-4000-0000-000000000001" ,
1672
+ },
1673
+ requiredMocks : func (ctx context.Context ) {
1674
+ storeMock .
1675
+ On ("NamespaceGet" , ctx , "00000000-0000-4000-0000-000000000000" ).
1676
+ Return (& models.Namespace {
1677
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1678
+ Name : "namespace" ,
1679
+ Owner : "000000000000000000000000" ,
1680
+ Members : []models.Member {
1681
+ {
1682
+ ID : "000000000000000000000000" ,
1683
+ Role : authorizer .RoleAdministrator ,
1684
+ },
1685
+ },
1686
+ }, nil ).
1687
+ Once ()
1688
+ storeMock .
1689
+ On ("NamespaceRemoveMember" , ctx , "00000000-0000-4000-0000-000000000000" , "000000000000000000000000" ).
1690
+ Return (nil ).
1691
+ Once ()
1692
+ },
1693
+ expected : Expected {err : nil },
1694
+ },
1695
+ {
1696
+ description : "succeeds when TenantID is equal to AuthenticatedTenantID" ,
1697
+ req : & requests.LeaveNamespace {
1698
+ UserID : "000000000000000000000000" ,
1699
+ TenantID : "00000000-0000-4000-0000-000000000000" ,
1700
+ AuthenticatedTenantID : "00000000-0000-4000-0000-000000000000" ,
1665
1701
},
1666
1702
requiredMocks : func (ctx context.Context ) {
1667
1703
storeMock .
@@ -1682,12 +1718,16 @@ func TestService_LeaveNamespace(t *testing.T) {
1682
1718
On ("NamespaceRemoveMember" , ctx , "00000000-0000-4000-0000-000000000000" , "000000000000000000000000" ).
1683
1719
Return (nil ).
1684
1720
Once ()
1721
+ cacheMock .
1722
+ On ("Delete" , ctx , "token_00000000-0000-4000-0000-000000000000000000000000000000000000" ).
1723
+ Return (nil ).
1724
+ Once ()
1685
1725
},
1686
1726
expected : Expected {err : nil },
1687
1727
},
1688
1728
}
1689
1729
1690
- s := NewService (storeMock , privateKey , publicKey , storecache . NewNullCache () , clientMock )
1730
+ s := NewService (storeMock , privateKey , publicKey , cacheMock , clientMock )
1691
1731
1692
1732
for _ , tc := range cases {
1693
1733
t .Run (tc .description , func (t * testing.T ) {
0 commit comments