2021-06-01 10:51:35 +02:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-10-23 21:40:10 +02:00
|
|
|
"codeberg.org/genofire/golang-lib/web"
|
|
|
|
"codeberg.org/genofire/golang-lib/web/webtest"
|
2021-06-01 10:51:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIPassword(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2021-07-19 17:59:08 +02:00
|
|
|
s, err := webtest.NewWithDBSetup(Register, SetupMigration)
|
2021-06-28 13:59:27 +02:00
|
|
|
assert.NoError(err)
|
2021-06-25 12:22:19 +02:00
|
|
|
defer s.Close()
|
2021-06-01 10:51:35 +02:00
|
|
|
assert.NotNil(s)
|
|
|
|
|
|
|
|
passwordCurrent := "CHANGEME"
|
|
|
|
passwordNew := "test"
|
|
|
|
|
|
|
|
hErr := web.HTTPError{}
|
2021-06-23 14:32:19 +02:00
|
|
|
// no auth
|
2021-06-28 13:59:27 +02:00
|
|
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusUnauthorized, &hErr)
|
2021-06-28 12:14:20 +02:00
|
|
|
assert.NoError(err)
|
2021-07-22 18:16:05 +02:00
|
|
|
assert.Equal(ErrAPINoSession.Error(), hErr.Message)
|
2021-06-01 10:51:35 +02:00
|
|
|
|
2021-06-28 12:14:20 +02:00
|
|
|
err = s.TestLogin()
|
|
|
|
assert.NoError(err)
|
2021-06-01 10:51:35 +02:00
|
|
|
|
2021-06-23 14:32:19 +02:00
|
|
|
hErr = web.HTTPError{}
|
|
|
|
// invalid
|
2021-06-28 12:14:20 +02:00
|
|
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", nil, http.StatusBadRequest, &hErr)
|
|
|
|
assert.NoError(err)
|
2021-07-22 18:16:05 +02:00
|
|
|
assert.Equal(web.ErrAPIInvalidRequestFormat.Error(), hErr.Message)
|
2021-06-23 14:32:19 +02:00
|
|
|
|
2021-06-01 10:51:35 +02:00
|
|
|
res := false
|
|
|
|
// set new password
|
2021-06-28 12:14:20 +02:00
|
|
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusOK, &res)
|
|
|
|
assert.NoError(err)
|
2021-06-01 10:51:35 +02:00
|
|
|
assert.True(res)
|
|
|
|
|
|
|
|
res = false
|
|
|
|
// set old password
|
2021-06-28 12:14:20 +02:00
|
|
|
err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordCurrent, http.StatusOK, &res)
|
|
|
|
assert.NoError(err)
|
2021-06-01 10:51:35 +02:00
|
|
|
assert.True(res)
|
|
|
|
}
|