generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
9878ab0
commit 3d1cf58
Showing
21 changed files
with
393 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alter table users | ||
drop column if exists deactivated_at; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alter table users | ||
add column if not exists deactivated_at timestamp with time zone; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,10 @@ import ( | |
"github.com/broadinstitute/sherlock/sherlock/internal/models" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func (s *handlerSuite) TestUserV3Get_badSelector() { | ||
|
@@ -30,6 +32,23 @@ func (s *handlerSuite) TestUserV3Get_notFound() { | |
s.Equal(errors.NotFound, got.Type) | ||
} | ||
|
||
// TestUserV3Get_deactivated is more of a smoke test -- we actually test the code that powers this | ||
// where it's defined in the middleware package, and it technically applies across every single API | ||
// route in this package, but it's unnecessary to exhaustively test that. We just check here that | ||
// it's wired up correctly. | ||
func (s *handlerSuite) TestUserV3Get_deactivated() { | ||
// Have to switch to super admin to make this user deactivated | ||
s.SetUserForDB(utils.PointerTo(s.TestData.User_SuperAdmin())) | ||
s.NoError(s.DB.Model(utils.PointerTo(s.TestData.User_NonSuitable())).Omit(clause.Associations).Update("deactivated_at", utils.PointerTo(time.Now())).Error) | ||
|
||
var got errors.ErrorResponse | ||
code := s.HandleRequest( | ||
s.NewNonSuitableRequest("GET", "/api/users/v3/[email protected]", nil), | ||
&got) | ||
s.Equal(http.StatusForbidden, code) | ||
s.Equal(errors.Forbidden, got.Type) | ||
} | ||
|
||
func (s *handlerSuite) TestUsersV3Get_self() { | ||
for _, selector := range []string{"self", "me", s.TestData.User_Suitable().Email, fmt.Sprintf("google-id/%s", s.TestData.User_Suitable().GoogleID)} { | ||
s.Run(fmt.Sprintf("get own user via '%s'", selector), func() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
sherlock/internal/middleware/authentication/user_provider_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package authentication | ||
|
||
import ( | ||
"github.com/broadinstitute/sherlock/go-shared/pkg/utils" | ||
"github.com/broadinstitute/sherlock/sherlock/internal/config" | ||
"github.com/broadinstitute/sherlock/sherlock/internal/models" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_forbidDeactivatedUsers(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
config.LoadTestConfig() | ||
tests := []struct { | ||
name string | ||
user *models.User | ||
expectError bool | ||
}{ | ||
{ | ||
name: "nil", | ||
user: nil, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "active", | ||
user: &models.User{}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "deactivated", | ||
user: &models.User{ | ||
DeactivatedAt: utils.PointerTo(time.Now()), | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "deleted", | ||
user: &models.User{ | ||
Model: gorm.Model{DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true}}, | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "via active", | ||
user: &models.User{ | ||
Via: &models.User{}, | ||
}, | ||
}, | ||
{ | ||
name: "via deactivated", | ||
user: &models.User{ | ||
Via: &models.User{ | ||
DeactivatedAt: utils.PointerTo(time.Now()), | ||
Via: &models.User{}, | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "via deleted", | ||
user: &models.User{ | ||
Via: &models.User{ | ||
Model: gorm.Model{DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true}}, | ||
Via: &models.User{}, | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
recorder := httptest.NewRecorder() | ||
ctx, _ := gin.CreateTestContext(recorder) | ||
ctx.Set(ctxUserFieldName, tt.user) | ||
forbidDeactivatedUsers()(ctx) | ||
assert.Equal(t, tt.expectError, ctx.IsAborted()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.