Skip to content

Commit

Permalink
🎨 Remove too long lines in router (lll)
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Jul 19, 2024
1 parent 3a0847d commit fe274f4
Show file tree
Hide file tree
Showing 16 changed files with 572 additions and 142 deletions.
35 changes: 28 additions & 7 deletions router/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ func TestHandler_PostAdmin(t *testing.T) {
admin := []uuid.UUID{uuid.New()}

e := echo.New()
req, err := http.NewRequest(http.MethodPost, "/api/admins", strings.NewReader(`["`+admin[0].String()+`"]`))
req, err := http.NewRequest(
http.MethodPost,
"/api/admins",
strings.NewReader(`["`+admin[0].String()+`"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand All @@ -149,7 +152,10 @@ func TestHandler_PostAdmin(t *testing.T) {
adminID := uuid.New()

e := echo.New()
req, err := http.NewRequest(http.MethodPost, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`))
req, err := http.NewRequest(
http.MethodPost,
"/api/admins",
strings.NewReader(`["`+adminID.String()+`"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -177,7 +183,10 @@ func TestHandler_PostAdmin(t *testing.T) {
adminID := uuid.New()

e := echo.New()
req, err := http.NewRequest(http.MethodPost, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`))
req, err := http.NewRequest(
http.MethodPost,
"/api/admins",
strings.NewReader(`["`+adminID.String()+`"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -210,7 +219,10 @@ func TestHandler_DeleteAdmin(t *testing.T) {
admin := []uuid.UUID{uuid.New()}

e := echo.New()
req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["`+admin[0].String()+`"]`))
req, err := http.NewRequest(
http.MethodDelete,
"/api/admins",
strings.NewReader(`["`+admin[0].String()+`"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand All @@ -235,7 +247,10 @@ func TestHandler_DeleteAdmin(t *testing.T) {
adminID := uuid.New()

e := echo.New()
req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`))
req, err := http.NewRequest(
http.MethodDelete,
"/api/admins",
strings.NewReader(`["`+adminID.String()+`"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand All @@ -261,7 +276,10 @@ func TestHandler_DeleteAdmin(t *testing.T) {
ctrl := gomock.NewController(t)

e := echo.New()
req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["invalid"]`))
req, err := http.NewRequest(
http.MethodDelete,
"/api/admins",
strings.NewReader(`["invalid"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand All @@ -281,7 +299,10 @@ func TestHandler_DeleteAdmin(t *testing.T) {
adminID := uuid.New()

e := echo.New()
req, err := http.NewRequest(http.MethodDelete, "/api/admins", strings.NewReader(`["`+adminID.String()+`"]`))
req, err := http.NewRequest(
http.MethodDelete,
"/api/admins",
strings.NewReader(`["`+adminID.String()+`"]`))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand Down
18 changes: 14 additions & 4 deletions router/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func (h Handlers) AuthCallback(c echo.Context) error {

codeVerifier, ok := sess.Values[sessionCodeVerifierKey].(string)
if !ok {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("code_verifier is not found in session"))
return echo.NewHTTPError(
http.StatusInternalServerError,
fmt.Errorf("code_verifier is not found in session"))
}

res, err := service.RequestAccessToken(code, codeVerifier)
Expand All @@ -68,7 +70,8 @@ func (h Handlers) AuthCallback(c echo.Context) error {
modelUser, err = h.Repository.GetUserByName(c.Request().Context(), u.Name)
if err != nil {
if ent.IsNotFound(err) {
modelUser, err = h.Repository.CreateUser(c.Request().Context(), u.Name, u.DisplayName, false)
modelUser, err = h.Repository.CreateUser(
c.Request().Context(), u.Name, u.DisplayName, false)
if err != nil {
h.Logger.Error("failed to create user", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
Expand Down Expand Up @@ -113,7 +116,9 @@ func (h Handlers) GeneratePKCE(c echo.Context) error {
sess.Values[sessionCodeVerifierKey] = codeVerifier

codeVerifierHash := sha256.Sum256([]byte(codeVerifier))
encoder := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").WithPadding(base64.NoPadding)
encoder := base64.
NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").
WithPadding(base64.NoPadding)

codeChallengeMethod := "S256"

Expand All @@ -122,8 +127,13 @@ func (h Handlers) GeneratePKCE(c echo.Context) error {
h.Logger.Error("failed to save session", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
// nolint:lll
to := fmt.Sprintf(
"%s/oauth2/authorize?response_type=code&client_id=%s&code_challenge=%s&code_challenge_method=%s",
service.TraQBaseURL, service.JomonClientID,
encoder.EncodeToString(codeVerifierHash[:]), codeChallengeMethod)

return c.Redirect(http.StatusFound, fmt.Sprintf("%s/oauth2/authorize?response_type=code&client_id=%s&code_challenge=%s&code_challenge_method=%s", service.TraQBaseURL, service.JomonClientID, encoder.EncodeToString(codeVerifierHash[:]), codeChallengeMethod))
return c.Redirect(http.StatusFound, to)
}

var randSrcPool = sync.Pool{
Expand Down
4 changes: 3 additions & 1 deletion router/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func TestHandler_GetCodeChallenge(t *testing.T) {
codeVerifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
codeVerifierHash := sha256.Sum256([]byte(codeVerifier))

encoder := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").WithPadding(base64.NoPadding)
encoder := base64.
NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_").
WithPadding(base64.NoPadding)
challenge := encoder.EncodeToString(codeVerifierHash[:])

assert.Equal(t, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM", challenge)
Expand Down
4 changes: 3 additions & 1 deletion router/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func (h Handlers) PostFile(c echo.Context) error {
mimetype := reqfile.Header.Get(echo.HeaderContentType)
if !acceptedMimeTypes[mimetype] {
h.Logger.Info("requested unsupported mime type", zap.String("mime-type", mimetype))
return echo.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Errorf("unsupported media type"))
return echo.NewHTTPError(
http.StatusUnsupportedMediaType,
fmt.Errorf("unsupported media type"))
}

src, err := reqfile.Open()
Expand Down
1 change: 1 addition & 0 deletions router/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/traPtitech/Jomon/testutil/random"
)

// nolint:lll
var testJpeg = `/9j/4AAQSkZJRgABAQIAOAA4AAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////2wBDAf//////////////////////////////////////////////////////////////////////////////////////wAARCAABAAEDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwBKBH//2Q`

func TestHandlers_PostFile(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion router/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ func (h Handlers) PutGroup(c echo.Context) error {
}

ctx := c.Request().Context()
updated, err := h.Repository.UpdateGroup(ctx, groupID, group.Name, group.Description, group.Budget)
updated, err := h.Repository.UpdateGroup(
ctx,
groupID, group.Name, group.Description, group.Budget)
if err != nil {
h.Logger.Error("failed to update group in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
Expand Down
Loading

0 comments on commit fe274f4

Please sign in to comment.