Compare commits
2 Commits
12515c2a1e
...
d37c53c60b
| Author | SHA1 | Date | |
|---|---|---|---|
|
d37c53c60b
|
|||
|
2465b0a73a
|
@@ -8,6 +8,7 @@ import (
|
|||||||
type Action struct {
|
type Action struct {
|
||||||
ActionID int64 `json:"action_id"`
|
ActionID int64 `json:"action_id"`
|
||||||
ActionDescription string `json:"action_description"`
|
ActionDescription string `json:"action_description"`
|
||||||
|
UserID int64 `json:"user_id"`
|
||||||
EstimatedChunks int `json:"estimated_chunks"`
|
EstimatedChunks int `json:"estimated_chunks"`
|
||||||
CompletedChunks int `json:"completed_chunks"`
|
CompletedChunks int `json:"completed_chunks"`
|
||||||
CompletedOn *time.Time `json:"completed_on,omitempty"`
|
CompletedOn *time.Time `json:"completed_on,omitempty"`
|
||||||
@@ -17,22 +18,22 @@ type Action struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Actions returns all actions from the model.
|
// Actions returns all actions from the model.
|
||||||
func (m *Model) Actions() ([]*Action, error) {
|
func (m *Model) Actions(userID int) ([]*Action, error) {
|
||||||
return m.SelectActions()
|
return m.SelectActions(userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Action returns a single action from its ID
|
// Action returns a single action from its ID
|
||||||
func (m *Model) Action(id int) (*Action, error) {
|
func (m *Model) Action(id int, userID int) (*Action, error) {
|
||||||
act, err := m.SelectActionByID(id)
|
act, err := m.SelectActionByID(id, userID)
|
||||||
return act, wrapNotFound(err)
|
return act, wrapNotFound(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAction inserts a given action into the store, returning the generated ActionID. The provided ActionID is ignored.
|
// AddAction inserts a given action into the store, returning the generated ActionID. The provided ActionID is ignored.
|
||||||
func (m *Model) AddAction(action *Action) (int, error) {
|
func (m *Model) AddAction(action *Action, userID int) (int, error) {
|
||||||
return m.InsertAction(action)
|
return m.InsertAction(action, userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveAction saves and updates an action.
|
// SaveAction saves and updates an action.
|
||||||
func (m *Model) SaveAction(action *Action) error {
|
func (m *Model) SaveAction(action *Action, userID int) error {
|
||||||
return m.UpdateAction(action)
|
return m.UpdateAction(action, userID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func TestErrorModelWrapping(t *testing.T) {
|
|||||||
|
|
||||||
_, err := m.Plan(0, 0)
|
_, err := m.Plan(0, 0)
|
||||||
assert.True(models.IsNotFoundError(err))
|
assert.True(models.IsNotFoundError(err))
|
||||||
_, err = m.Action(0)
|
_, err = m.Action(0, 0)
|
||||||
assert.True(models.IsNotFoundError(err))
|
assert.True(models.IsNotFoundError(err))
|
||||||
}
|
}
|
||||||
func TestErrorModelInvalidLogin(t *testing.T) {
|
func TestErrorModelInvalidLogin(t *testing.T) {
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ import (
|
|||||||
// Store represents the backing store.
|
// Store represents the backing store.
|
||||||
type Store interface {
|
type Store interface {
|
||||||
ConnectionLive() error
|
ConnectionLive() error
|
||||||
SelectActions() ([]*Action, error)
|
SelectActions(userID int) ([]*Action, error)
|
||||||
SelectActionByID(id int) (*Action, error)
|
SelectActionByID(id int, userID int) (*Action, error)
|
||||||
InsertAction(action *Action) (int, error)
|
InsertAction(action *Action, userID int) (int, error)
|
||||||
UpdateAction(action *Action) error
|
UpdateAction(action *Action, userID int) error
|
||||||
SelectPlans(userID int) ([]*Plan, error)
|
SelectPlans(userID int) ([]*Plan, error)
|
||||||
SelectPlanByID(id int, userID int) (*Plan, error)
|
SelectPlanByID(id int, userID int) (*Plan, error)
|
||||||
InsertPlan(plan *Plan, userID int) (int, error)
|
InsertPlan(plan *Plan, userID int) (int, error)
|
||||||
SelectActionsByPlanID(plan *Plan) ([]*Action, error)
|
SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error)
|
||||||
SelectUserByUsername(username string) (*User, error)
|
SelectUserByUsername(username string) (*User, error)
|
||||||
InsertUser(user *User) (int, error)
|
InsertUser(user *User) (int, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,23 +15,23 @@ func TestModelActions(t *testing.T) {
|
|||||||
p := &models.Plan{PlanID: 6}
|
p := &models.Plan{PlanID: 6}
|
||||||
|
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
str.InsertAction(a1)
|
str.InsertAction(a1, userID)
|
||||||
str.InsertPlan(p, userID)
|
str.InsertPlan(p, userID)
|
||||||
m := models.New(str)
|
m := models.New(str)
|
||||||
|
|
||||||
actions, err := m.Actions()
|
actions, err := m.Actions(userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(actions))
|
assert.Equal(1, len(actions))
|
||||||
|
|
||||||
firstAction, err := m.Action(1)
|
firstAction, err := m.Action(1, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, firstAction.ActionID)
|
assert.EqualValues(1, firstAction.ActionID)
|
||||||
|
|
||||||
actionID, err := m.AddAction(a2)
|
actionID, err := m.AddAction(a2, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(2, actionID)
|
assert.EqualValues(2, actionID)
|
||||||
|
|
||||||
err = m.SaveAction(a1)
|
err = m.SaveAction(a1, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,8 +44,8 @@ func TestModelPlanMethods(t *testing.T) {
|
|||||||
|
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
str.InsertPlan(p, userID)
|
str.InsertPlan(p, userID)
|
||||||
str.InsertAction(a1)
|
str.InsertAction(a1, userID)
|
||||||
str.InsertAction(a2)
|
str.InsertAction(a2, userID)
|
||||||
m := models.New(str)
|
m := models.New(str)
|
||||||
|
|
||||||
plans, err := m.Plans(userID)
|
plans, err := m.Plans(userID)
|
||||||
@@ -56,7 +56,7 @@ func TestModelPlanMethods(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, firstPlan.PlanID)
|
assert.EqualValues(1, firstPlan.PlanID)
|
||||||
|
|
||||||
actions, err := m.GetActions(firstPlan)
|
actions, err := m.GetActions(firstPlan, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(1, len(actions))
|
assert.Equal(1, len(actions))
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,6 @@ func (m *Model) AddPlan(plan *Plan, userID int) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetActions returns the actions associated with a particular plan.
|
// GetActions returns the actions associated with a particular plan.
|
||||||
func (m *Model) GetActions(plan *Plan) ([]*Action, error) {
|
func (m *Model) GetActions(plan *Plan, userID int) ([]*Action, error) {
|
||||||
return m.SelectActionsByPlanID(plan)
|
return m.SelectActionsByPlanID(plan, userID)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ func TestModelUsers(t *testing.T) {
|
|||||||
user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}
|
user1 := &models.User{Username: username, DisplayName: "Ted Est", Password: []byte("$2y$05$6SVV35GX4cB4PDPhRaDD/exsL.HV8QtMMr60YL6dLyqtX4l58q.cy")}
|
||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
str.InsertPlan(p, 3)
|
str.InsertPlan(p, 3)
|
||||||
str.InsertAction(a1)
|
str.InsertAction(a1, 3)
|
||||||
str.InsertAction(a2)
|
str.InsertAction(a2, 3)
|
||||||
str.InsertUser(user1)
|
str.InsertUser(user1)
|
||||||
m := models.New(str)
|
m := models.New(str)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package routes
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/tokens"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -21,13 +22,19 @@ func NewActionRouter(m *models.Model) http.Handler {
|
|||||||
|
|
||||||
func getActionsFunc(m *models.Model) http.HandlerFunc {
|
func getActionsFunc(m *models.Model) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
userID, userErr := tokens.GetUserID(r.Context())
|
||||||
|
if userErr != nil {
|
||||||
|
unauthorizedHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
actions []*models.Action
|
actions []*models.Action
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
planIDString := r.URL.Query().Get("plan_id")
|
planIDString := r.URL.Query().Get("plan_id")
|
||||||
if planIDString == "" {
|
if planIDString == "" {
|
||||||
actions, err = m.Actions()
|
actions, err = m.Actions(userID)
|
||||||
} else {
|
} else {
|
||||||
planID, convErr := strconv.ParseInt(planIDString, 10, 64)
|
planID, convErr := strconv.ParseInt(planIDString, 10, 64)
|
||||||
if convErr != nil {
|
if convErr != nil {
|
||||||
@@ -35,7 +42,7 @@ func getActionsFunc(m *models.Model) http.HandlerFunc {
|
|||||||
err = nil
|
err = nil
|
||||||
} else {
|
} else {
|
||||||
plan := &models.Plan{PlanID: planID}
|
plan := &models.Plan{PlanID: planID}
|
||||||
actions, err = m.GetActions(plan)
|
actions, err = m.GetActions(plan, userID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -51,12 +58,20 @@ func getActionsFunc(m *models.Model) http.HandlerFunc {
|
|||||||
|
|
||||||
func getActionByIDFunc(m *models.Model) http.HandlerFunc {
|
func getActionByIDFunc(m *models.Model) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
userID, userErr := tokens.GetUserID(r.Context())
|
||||||
|
if userErr != nil {
|
||||||
|
unauthorizedHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFoundHandler(w, r)
|
notFoundHandler(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
action, err := m.Action(id)
|
|
||||||
|
action, err := m.Action(id, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsNotFoundError(err) {
|
if models.IsNotFoundError(err) {
|
||||||
notFoundHandler(w, r)
|
notFoundHandler(w, r)
|
||||||
@@ -81,11 +96,17 @@ type createActionResponse struct {
|
|||||||
func postActionFunc(m *models.Model) http.HandlerFunc {
|
func postActionFunc(m *models.Model) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
userID, err := tokens.GetUserID(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
unauthorizedHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
||||||
dec := json.NewDecoder(r.Body)
|
dec := json.NewDecoder(r.Body)
|
||||||
dec.DisallowUnknownFields()
|
dec.DisallowUnknownFields()
|
||||||
var a models.Action
|
var a models.Action
|
||||||
err := dec.Decode(&a)
|
err = dec.Decode(&a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
badRequestError(w, err)
|
badRequestError(w, err)
|
||||||
return
|
return
|
||||||
@@ -103,12 +124,12 @@ func postActionFunc(m *models.Model) http.HandlerFunc {
|
|||||||
CompletedOn: a.CompletedOn,
|
CompletedOn: a.CompletedOn,
|
||||||
PlanID: a.PlanID,
|
PlanID: a.PlanID,
|
||||||
}
|
}
|
||||||
id, err := m.AddAction(action)
|
id, err := m.AddAction(action, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
action, err = m.Action(id)
|
action, err = m.Action(id, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
@@ -134,6 +155,13 @@ type updateActionResponse struct {
|
|||||||
|
|
||||||
func putActionFunc(m *models.Model) http.HandlerFunc {
|
func putActionFunc(m *models.Model) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
userID, err := tokens.GetUserID(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
unauthorizedHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
id, err := strconv.Atoi(chi.URLParam(r, "actionid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFoundHandler(w, r)
|
notFoundHandler(w, r)
|
||||||
@@ -162,12 +190,12 @@ func putActionFunc(m *models.Model) http.HandlerFunc {
|
|||||||
PlanID: a.PlanID,
|
PlanID: a.PlanID,
|
||||||
ActionID: int64(id),
|
ActionID: int64(id),
|
||||||
}
|
}
|
||||||
err = m.SaveAction(action)
|
err = m.SaveAction(action, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
action, err = m.Action(id)
|
action, err = m.Action(id, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverError(w, err)
|
serverError(w, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func TestEmptyActions(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -41,10 +41,10 @@ func TestOneAction(t *testing.T) {
|
|||||||
completedDate, _ := time.Parse("2006-01-02", "2021-01-03")
|
completedDate, _ := time.Parse("2006-01-02", "2021-01-03")
|
||||||
a1 := &models.Action{ActionID: 3, ActionDescription: "testing", CompletedChunks: 1, CompletedOn: &completedDate, CreatedAt: &createdDate, UpdatedAt: &updatedDate, EstimatedChunks: 3, PlanID: 0}
|
a1 := &models.Action{ActionID: 3, ActionDescription: "testing", CompletedChunks: 1, CompletedOn: &completedDate, CreatedAt: &createdDate, UpdatedAt: &updatedDate, EstimatedChunks: 3, PlanID: 0}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a1)
|
m.AddAction(a1, 3)
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -59,6 +59,7 @@ func TestOneAction(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"action_id": 1,
|
"action_id": 1,
|
||||||
"action_description": "testing",
|
"action_description": "testing",
|
||||||
|
"user_id": 3,
|
||||||
"estimated_chunks": 3,
|
"estimated_chunks": 3,
|
||||||
"completed_chunks": 1,
|
"completed_chunks": 1,
|
||||||
"completed_on": "2021-01-03T00:00:00Z",
|
"completed_on": "2021-01-03T00:00:00Z",
|
||||||
@@ -79,7 +80,7 @@ func TestErrorAction(t *testing.T) {
|
|||||||
m := getErrorModel("Model always errors")
|
m := getErrorModel("Model always errors")
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -101,7 +102,7 @@ func TestEmptyActionErrorWriter(t *testing.T) {
|
|||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
rr := NewBadWriter()
|
rr := NewBadWriter()
|
||||||
|
|
||||||
@@ -121,9 +122,9 @@ func TestOneActionByID(t *testing.T) {
|
|||||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.InsertAction(a)
|
m.InsertAction(a, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/1", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -137,6 +138,7 @@ func TestOneActionByID(t *testing.T) {
|
|||||||
expected := `{
|
expected := `{
|
||||||
"action_id": 1,
|
"action_id": 1,
|
||||||
"action_description": "howdy",
|
"action_description": "howdy",
|
||||||
|
"user_id": 3,
|
||||||
"estimated_chunks": 54,
|
"estimated_chunks": 54,
|
||||||
"completed_chunks": 0,
|
"completed_chunks": 0,
|
||||||
"updated_at": "2021-01-02T00:00:00Z",
|
"updated_at": "2021-01-02T00:00:00Z",
|
||||||
@@ -155,7 +157,7 @@ func TestErrorActionByID(t *testing.T) {
|
|||||||
m := getErrorModel("Model always errors")
|
m := getErrorModel("Model always errors")
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/5", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/5", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -176,10 +178,10 @@ func TestEmptyActionErrorWriterByID(t *testing.T) {
|
|||||||
|
|
||||||
a := &models.Action{ActionID: 6}
|
a := &models.Action{ActionID: 6}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/1", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
||||||
|
|
||||||
rr := NewBadWriter()
|
rr := NewBadWriter()
|
||||||
|
|
||||||
@@ -199,7 +201,7 @@ func TestNotFoundActionByIDText(t *testing.T) {
|
|||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/wo", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/wo", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -218,7 +220,7 @@ func TestNotFoundActionByIDEmpty(t *testing.T) {
|
|||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/1", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -238,9 +240,9 @@ func TestActionsByPlanID(t *testing.T) {
|
|||||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||||
a := &models.Action{ActionID: 1, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 6}
|
a := &models.Action{ActionID: 1, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 6}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/?plan_id=6", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/?plan_id=6", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -255,6 +257,7 @@ func TestActionsByPlanID(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"action_id": 1,
|
"action_id": 1,
|
||||||
"action_description": "howdy",
|
"action_description": "howdy",
|
||||||
|
"user_id": 3,
|
||||||
"estimated_chunks": 54,
|
"estimated_chunks": 54,
|
||||||
"completed_chunks": 0,
|
"completed_chunks": 0,
|
||||||
"updated_at": "2021-01-02T00:00:00Z",
|
"updated_at": "2021-01-02T00:00:00Z",
|
||||||
@@ -274,9 +277,9 @@ func TestActionsByPlanIDInvalidID(t *testing.T) {
|
|||||||
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
updatedDate, _ := time.Parse("2006-01-02", "2021-01-02")
|
||||||
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
req, _ := http.NewRequest("GET", "/?plan_id=aoeu", nil)
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/?plan_id=aoeu", nil)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
|||||||
120
routes/actions_unauthorized_test.go
Normal file
120
routes/actions_unauthorized_test.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package routes_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/routes"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEmptyActionEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewActionRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOneActionEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddPlan(p, 3)
|
||||||
|
router := routes.NewActionRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOneActionByIDEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddPlan(p, 3)
|
||||||
|
router := routes.NewActionRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/1", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPureJSONActionEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewActionRouter(m)
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_date": "2021-01-01T00:00:00Z",
|
||||||
|
"plan_id": 1,
|
||||||
|
"user_id": 3
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", bytes.NewBuffer(data))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutActionEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
a := &models.Action{PlanID: 6}
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddAction(a, 3)
|
||||||
|
|
||||||
|
router := routes.NewActionRouter(m)
|
||||||
|
data, _ := json.Marshal(a)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "PUT", "/1", bytes.NewBuffer(data))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ func TestPureJSONPostAction(t *testing.T) {
|
|||||||
ActionDescription: "here's an action",
|
ActionDescription: "here's an action",
|
||||||
}
|
}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"action_description": "here's an action",
|
"action_description": "here's an action",
|
||||||
@@ -34,7 +34,7 @@ func TestPureJSONPostAction(t *testing.T) {
|
|||||||
"completed_on": "2021-01-01T00:00:00Z",
|
"completed_on": "2021-01-01T00:00:00Z",
|
||||||
"plan_id": 5
|
"plan_id": 5
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -52,7 +52,8 @@ func TestPureJSONPostAction(t *testing.T) {
|
|||||||
"completed_chunks": 2,
|
"completed_chunks": 2,
|
||||||
"completed_on": "2021-01-01T00:00:00Z",
|
"completed_on": "2021-01-01T00:00:00Z",
|
||||||
"plan_id": 5,
|
"plan_id": 5,
|
||||||
"action_id": 2
|
"action_id": 2,
|
||||||
|
"user_id": 3
|
||||||
},
|
},
|
||||||
"id": 2
|
"id": 2
|
||||||
}`
|
}`
|
||||||
@@ -74,7 +75,7 @@ func TestExtraFieldActionPostJSON(t *testing.T) {
|
|||||||
"plan_id": 5,
|
"plan_id": 5,
|
||||||
"sabotage": "omg"
|
"sabotage": "omg"
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -97,7 +98,7 @@ func TestEmptyBodyActionPost(t *testing.T) {
|
|||||||
m.AddPlan(p, 3)
|
m.AddPlan(p, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(``)
|
data := []byte(``)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -125,7 +126,7 @@ func TestTwoBodyActionPost(t *testing.T) {
|
|||||||
}, {
|
}, {
|
||||||
"plan_id": 6
|
"plan_id": 6
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -149,7 +150,7 @@ func TestErrorCreateAction(t *testing.T) {
|
|||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
a := &models.Action{PlanID: 6}
|
a := &models.Action{PlanID: 6}
|
||||||
data, _ := json.Marshal(a)
|
data, _ := json.Marshal(a)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -174,7 +175,7 @@ func TestErrorOnRetrieveCreateAction(t *testing.T) {
|
|||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
a := &models.Action{PlanID: 6}
|
a := &models.Action{PlanID: 6}
|
||||||
data, _ := json.Marshal(a)
|
data, _ := json.Marshal(a)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -195,11 +196,11 @@ func TestErrorWriterCreateAction(t *testing.T) {
|
|||||||
|
|
||||||
a := &models.Action{PlanID: 6}
|
a := &models.Action{PlanID: 6}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data, _ := json.Marshal(a)
|
data, _ := json.Marshal(a)
|
||||||
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := NewBadWriter()
|
rr := NewBadWriter()
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func TestPureJSONPutAction(t *testing.T) {
|
|||||||
ActionDescription: "hn",
|
ActionDescription: "hn",
|
||||||
}
|
}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(`{
|
data := []byte(`{
|
||||||
"action_description": "here's an action",
|
"action_description": "here's an action",
|
||||||
@@ -34,7 +34,7 @@ func TestPureJSONPutAction(t *testing.T) {
|
|||||||
"completed_on": "2021-01-01T00:00:00Z",
|
"completed_on": "2021-01-01T00:00:00Z",
|
||||||
"plan_id": 5
|
"plan_id": 5
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -52,7 +52,8 @@ func TestPureJSONPutAction(t *testing.T) {
|
|||||||
"completed_chunks": 2,
|
"completed_chunks": 2,
|
||||||
"completed_on": "2021-01-01T00:00:00Z",
|
"completed_on": "2021-01-01T00:00:00Z",
|
||||||
"plan_id": 5,
|
"plan_id": 5,
|
||||||
"action_id": 1
|
"action_id": 1,
|
||||||
|
"user_id": 3
|
||||||
},
|
},
|
||||||
"id": 1
|
"id": 1
|
||||||
}`
|
}`
|
||||||
@@ -71,7 +72,7 @@ func TestExtraFieldActionPutJSON(t *testing.T) {
|
|||||||
"plan_id": 5,
|
"plan_id": 5,
|
||||||
"sabotage": "omg"
|
"sabotage": "omg"
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -91,7 +92,7 @@ func TestEmptyBodyActionPut(t *testing.T) {
|
|||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data := []byte(``)
|
data := []byte(``)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -116,7 +117,7 @@ func TestTwoBodyActionPut(t *testing.T) {
|
|||||||
}, {
|
}, {
|
||||||
"plan_id": 6
|
"plan_id": 6
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -141,7 +142,7 @@ func TestBadActionIDPut(t *testing.T) {
|
|||||||
}, {
|
}, {
|
||||||
"plan_id": 6
|
"plan_id": 6
|
||||||
}`)
|
}`)
|
||||||
req, _ := http.NewRequest("PUT", "/text", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/text", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -165,7 +166,7 @@ func TestErrorUpdateAction(t *testing.T) {
|
|||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
a := &models.Action{PlanID: 6}
|
a := &models.Action{PlanID: 6}
|
||||||
data, _ := json.Marshal(a)
|
data, _ := json.Marshal(a)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
@@ -190,7 +191,7 @@ func TestErrorOnRetrieveUpdateAction(t *testing.T) {
|
|||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
a := &models.Action{PlanID: 6}
|
a := &models.Action{PlanID: 6}
|
||||||
data, _ := json.Marshal(a)
|
data, _ := json.Marshal(a)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
@@ -211,11 +212,11 @@ func TestErrorWriterUpdateAction(t *testing.T) {
|
|||||||
|
|
||||||
a := &models.Action{PlanID: 6}
|
a := &models.Action{PlanID: 6}
|
||||||
m := getEmptyModel()
|
m := getEmptyModel()
|
||||||
m.AddAction(a)
|
m.AddAction(a, 3)
|
||||||
|
|
||||||
router := routes.NewActionRouter(m)
|
router := routes.NewActionRouter(m)
|
||||||
data, _ := json.Marshal(a)
|
data, _ := json.Marshal(a)
|
||||||
req, _ := http.NewRequest("PUT", "/1", bytes.NewBuffer(data))
|
req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/1", bytes.NewBuffer(data))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
rr := NewBadWriter()
|
rr := NewBadWriter()
|
||||||
|
|||||||
@@ -5,22 +5,22 @@ import (
|
|||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *errorStore) SelectActions() ([]*models.Action, error) {
|
func (e *errorStore) SelectActions(userID int) ([]*models.Action, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) SelectActionByID(id int) (*models.Action, error) {
|
func (e *errorStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) InsertAction(action *models.Action) (int, error) {
|
func (e *errorStore) InsertAction(action *models.Action, userID int) (int, error) {
|
||||||
if e.errorOnInsert {
|
if e.errorOnInsert {
|
||||||
return 0, e.error
|
return 0, e.error
|
||||||
}
|
}
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) UpdateAction(action *models.Action) error {
|
func (e *errorStore) UpdateAction(action *models.Action, userID int) error {
|
||||||
if e.errorOnInsert {
|
if e.errorOnInsert {
|
||||||
return e.error
|
return e.error
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ func (e *errorStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
func (e *errorStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||||
return nil, e.error
|
return nil, e.error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,32 +10,33 @@ import (
|
|||||||
|
|
||||||
func TestErrorActionMethods(t *testing.T) {
|
func TestErrorActionMethods(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
userID := 2
|
||||||
str := store.GetErrorStore("error message sample", true)
|
str := store.GetErrorStore("error message sample", true)
|
||||||
str2 := store.GetErrorStore("error message sample", false)
|
str2 := store.GetErrorStore("error message sample", false)
|
||||||
str3 := store.GetErrorStoreForError(fmt.Errorf("test error"), false)
|
str3 := store.GetErrorStoreForError(fmt.Errorf("test error"), false)
|
||||||
|
|
||||||
_, err := str.InsertAction(&models.Action{})
|
_, err := str.InsertAction(&models.Action{}, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
_, err = str2.InsertAction(&models.Action{})
|
_, err = str2.InsertAction(&models.Action{}, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
_, err = str3.InsertAction(&models.Action{})
|
_, err = str3.InsertAction(&models.Action{}, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
_, err = str.SelectActionByID(8)
|
_, err = str.SelectActionByID(8, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
_, err = str2.SelectActionByID(8)
|
_, err = str2.SelectActionByID(8, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
_, err = str.SelectActions()
|
_, err = str.SelectActions(userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
_, err = str.SelectActionsByPlanID(&models.Plan{})
|
_, err = str.SelectActionsByPlanID(&models.Plan{}, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
replacementAction := &models.Action{}
|
replacementAction := &models.Action{}
|
||||||
err = str.UpdateAction(replacementAction)
|
err = str.UpdateAction(replacementAction, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
err = str2.UpdateAction(replacementAction)
|
err = str2.UpdateAction(replacementAction, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,38 +20,45 @@ func GetInMemoryStore() (models.Store, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) SelectActions() ([]*models.Action, error) {
|
func (store *inMemoryStore) SelectActions(userID int) ([]*models.Action, error) {
|
||||||
return store.actions, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
|
||||||
ret := make([]*models.Action, 0)
|
ret := make([]*models.Action, 0)
|
||||||
for _, action := range store.actions {
|
for _, action := range store.actions {
|
||||||
if int(plan.PlanID) == int(action.PlanID) {
|
if int(action.UserID) == userID {
|
||||||
ret = append(ret, action)
|
ret = append(ret, action)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) SelectActionByID(id int) (*models.Action, error) {
|
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||||
|
ret := make([]*models.Action, 0)
|
||||||
for _, action := range store.actions {
|
for _, action := range store.actions {
|
||||||
if id == int(action.ActionID) {
|
if (int(plan.PlanID) == int(action.PlanID)) && (int(action.UserID) == userID) {
|
||||||
|
ret = append(ret, action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (store *inMemoryStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
||||||
|
for _, action := range store.actions {
|
||||||
|
if id == int(action.ActionID) && (int(action.UserID) == userID) {
|
||||||
return action, nil
|
return action, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, sql.ErrNoRows
|
return nil, sql.ErrNoRows
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) InsertAction(action *models.Action) (int, error) {
|
func (store *inMemoryStore) InsertAction(action *models.Action, userID int) (int, error) {
|
||||||
id := len(store.actions) + 1
|
id := len(store.actions) + 1
|
||||||
action.ActionID = int64(id)
|
action.ActionID = int64(id)
|
||||||
|
action.UserID = int64(userID)
|
||||||
store.actions = append(store.actions, action)
|
store.actions = append(store.actions, action)
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *inMemoryStore) UpdateAction(action *models.Action) error {
|
func (store *inMemoryStore) UpdateAction(action *models.Action, userID int) error {
|
||||||
currentAction, err := store.SelectActionByID(int(action.ActionID))
|
currentAction, err := store.SelectActionByID(int(action.ActionID), userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,42 +12,45 @@ func TestInMemoryActionMethods(t *testing.T) {
|
|||||||
str, _ := store.GetInMemoryStore()
|
str, _ := store.GetInMemoryStore()
|
||||||
|
|
||||||
sampleplanid := 8
|
sampleplanid := 8
|
||||||
|
userID := 10
|
||||||
|
|
||||||
act := &models.Action{}
|
act := &models.Action{}
|
||||||
a2 := &models.Action{PlanID: sampleplanid}
|
a2 := &models.Action{PlanID: sampleplanid}
|
||||||
|
|
||||||
id, _ := str.InsertAction(act)
|
id, _ := str.InsertAction(act, userID)
|
||||||
assert.EqualValues(1, id)
|
assert.EqualValues(1, id)
|
||||||
|
|
||||||
receivedAction, err := str.SelectActionByID(id)
|
receivedAction, err := str.SelectActionByID(id, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(act, receivedAction)
|
assert.EqualValues(act, receivedAction)
|
||||||
|
_, err = str.SelectActionByID(id, userID+1)
|
||||||
|
assert.NotNil(err)
|
||||||
|
|
||||||
allactions, err := str.SelectActions()
|
allactions, err := str.SelectActions(userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, len(allactions))
|
assert.EqualValues(1, len(allactions))
|
||||||
|
|
||||||
str.InsertAction(a2)
|
str.InsertAction(a2, userID)
|
||||||
allactions, err = str.SelectActions()
|
allactions, err = str.SelectActions(userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(2, len(allactions))
|
assert.EqualValues(2, len(allactions))
|
||||||
|
|
||||||
planactions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(sampleplanid)})
|
planactions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(sampleplanid)}, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(1, len(planactions))
|
assert.EqualValues(1, len(planactions))
|
||||||
assert.Equal(a2, planactions[0])
|
assert.Equal(a2, planactions[0])
|
||||||
|
|
||||||
_, err = str.SelectActionByID(151)
|
_, err = str.SelectActionByID(151, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
sampleDescription := "snth"
|
sampleDescription := "snth"
|
||||||
replacementAction := &models.Action{ActionID: 1, ActionDescription: sampleDescription}
|
replacementAction := &models.Action{ActionID: 1, ActionDescription: sampleDescription}
|
||||||
err = str.UpdateAction(replacementAction)
|
err = str.UpdateAction(replacementAction, userID)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.Equal(sampleDescription, act.ActionDescription)
|
assert.Equal(sampleDescription, act.ActionDescription)
|
||||||
|
|
||||||
replacementAction = &models.Action{ActionID: 1235122, ActionDescription: sampleDescription}
|
replacementAction = &models.Action{ActionID: 1235122, ActionDescription: sampleDescription}
|
||||||
err = str.UpdateAction(replacementAction)
|
err = str.UpdateAction(replacementAction, userID)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS plans(
|
|||||||
CREATE TABLE IF NOT EXISTS actions(
|
CREATE TABLE IF NOT EXISTS actions(
|
||||||
action_id serial PRIMARY KEY,
|
action_id serial PRIMARY KEY,
|
||||||
action_description VARCHAR (500) NOT NULL,
|
action_description VARCHAR (500) NOT NULL,
|
||||||
|
user_id int REFERENCES users(user_id),
|
||||||
estimated_chunks SMALLINT,
|
estimated_chunks SMALLINT,
|
||||||
completed_chunks SMALLINT,
|
completed_chunks SMALLINT,
|
||||||
completed_on TIMESTAMP WITH TIME ZONE,
|
completed_on TIMESTAMP WITH TIME ZONE,
|
||||||
|
|||||||
@@ -17,41 +17,44 @@ func GetPostgresStore(db *sqlx.DB) (models.Store, error) {
|
|||||||
return &postgresStore{db: db}, nil
|
return &postgresStore{db: db}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) SelectActions() ([]*models.Action, error) {
|
func (store *postgresStore) SelectActions(userID int) ([]*models.Action, error) {
|
||||||
|
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = ?")
|
||||||
actions := make([]*models.Action, 0)
|
actions := make([]*models.Action, 0)
|
||||||
err := store.db.Select(&actions, "SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions")
|
err := store.db.Select(&actions, queryString, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return actions, nil
|
return actions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
||||||
queryString := store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ?")
|
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ? AND user_id = ?")
|
||||||
actions := make([]*models.Action, 0)
|
actions := make([]*models.Action, 0)
|
||||||
err := store.db.Select(&actions, queryString, plan.PlanID)
|
err := store.db.Select(&actions, queryString, plan.PlanID, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return actions, nil
|
return actions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) {
|
func (store *postgresStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
||||||
|
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ? AND user_id = ?")
|
||||||
action := models.Action{}
|
action := models.Action{}
|
||||||
err := store.db.Get(&action, store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ?"), id)
|
err := store.db.Get(&action, queryString, id, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &action, nil
|
return &action, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
func (store *postgresStore) InsertAction(action *models.Action, userID int) (int, error) {
|
||||||
queryString := store.db.Rebind(
|
queryString := store.db.Rebind(
|
||||||
`INSERT INTO actions (action_description,
|
`INSERT INTO actions (action_description,
|
||||||
|
user_id,
|
||||||
estimated_chunks,
|
estimated_chunks,
|
||||||
completed_chunks,
|
completed_chunks,
|
||||||
completed_on,
|
completed_on,
|
||||||
plan_id) VALUES (?, ?, ?, ?, ?) RETURNING action_id`,
|
plan_id) VALUES (?, ?, ?, ?, ?, ?) RETURNING action_id`,
|
||||||
)
|
)
|
||||||
tx := store.db.MustBegin()
|
tx := store.db.MustBegin()
|
||||||
var id int
|
var id int
|
||||||
@@ -59,6 +62,7 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
|||||||
&id,
|
&id,
|
||||||
queryString,
|
queryString,
|
||||||
action.ActionDescription,
|
action.ActionDescription,
|
||||||
|
userID,
|
||||||
action.EstimatedChunks,
|
action.EstimatedChunks,
|
||||||
action.CompletedChunks,
|
action.CompletedChunks,
|
||||||
action.CompletedOn,
|
action.CompletedOn,
|
||||||
@@ -75,16 +79,26 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
|
|||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *postgresStore) UpdateAction(action *models.Action) error {
|
func (store *postgresStore) UpdateAction(action *models.Action, userID int) error {
|
||||||
query := `UPDATE actions SET
|
query := `UPDATE actions SET
|
||||||
action_description = :action_description,
|
action_description = :action_description,
|
||||||
estimated_chunks = :estimated_chunks,
|
estimated_chunks = :estimated_chunks,
|
||||||
completed_chunks = :completed_chunks,
|
completed_chunks = :completed_chunks,
|
||||||
completed_on = :completed_on,
|
completed_on = :completed_on,
|
||||||
plan_id = :plan_id
|
plan_id = :plan_id
|
||||||
WHERE action_id = :action_id`
|
WHERE action_id = :action_id
|
||||||
|
AND user_id = :user_id`
|
||||||
tx := store.db.MustBegin()
|
tx := store.db.MustBegin()
|
||||||
_, err := store.db.NamedExec(query, action)
|
actionToUse := &models.Action{
|
||||||
|
ActionDescription: action.ActionDescription,
|
||||||
|
EstimatedChunks: action.EstimatedChunks,
|
||||||
|
CompletedChunks: action.CompletedChunks,
|
||||||
|
CompletedOn: action.CompletedOn,
|
||||||
|
PlanID: action.PlanID,
|
||||||
|
ActionID: action.ActionID,
|
||||||
|
UserID: int64(userID),
|
||||||
|
}
|
||||||
|
_, err := store.db.NamedExec(query, actionToUse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ func TestSelectActions(t *testing.T) {
|
|||||||
idToUse := 1
|
idToUse := 1
|
||||||
estChunks := 5
|
estChunks := 5
|
||||||
compChunks := 7
|
compChunks := 7
|
||||||
|
userIDToUse := 13
|
||||||
desc := "Howdy, partner."
|
desc := "Howdy, partner."
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
@@ -46,13 +47,16 @@ func TestSelectActions(t *testing.T) {
|
|||||||
"completed_on",
|
"completed_on",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
"plan_id"}).
|
"plan_id",
|
||||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
|
"user_id"}).
|
||||||
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse, userIDToUse).
|
||||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions$").WillReturnRows(rows)
|
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse, userIDToUse)
|
||||||
|
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = \$1$`).
|
||||||
|
WithArgs(userIDToUse).
|
||||||
|
WillReturnRows(rows)
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
actions, err := str.SelectActions()
|
actions, err := str.SelectActions(userIDToUse)
|
||||||
|
|
||||||
// test results
|
// test results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
@@ -66,6 +70,7 @@ func TestSelectActions(t *testing.T) {
|
|||||||
assert.Equal(createTime, *action.CreatedAt)
|
assert.Equal(createTime, *action.CreatedAt)
|
||||||
assert.Equal(updateTime, *action.UpdatedAt)
|
assert.Equal(updateTime, *action.UpdatedAt)
|
||||||
assert.Equal(idToUse, action.PlanID)
|
assert.Equal(idToUse, action.PlanID)
|
||||||
|
assert.EqualValues(userIDToUse, action.UserID)
|
||||||
|
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
t.Errorf("unfulfilled expectations: %s", err)
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
@@ -82,6 +87,7 @@ func TestSelectActionsByPlanID(t *testing.T) {
|
|||||||
idToUse := 1
|
idToUse := 1
|
||||||
estChunks := 5
|
estChunks := 5
|
||||||
compChunks := 7
|
compChunks := 7
|
||||||
|
userIDToUse := 13
|
||||||
desc := "Howdy, partner."
|
desc := "Howdy, partner."
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
@@ -89,20 +95,21 @@ func TestSelectActionsByPlanID(t *testing.T) {
|
|||||||
rows := sqlmock.NewRows([]string{
|
rows := sqlmock.NewRows([]string{
|
||||||
"action_id",
|
"action_id",
|
||||||
"action_description",
|
"action_description",
|
||||||
|
"user_id",
|
||||||
"estimated_chunks",
|
"estimated_chunks",
|
||||||
"completed_chunks",
|
"completed_chunks",
|
||||||
"completed_on",
|
"completed_on",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
"plan_id"}).
|
"plan_id"}).
|
||||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
|
AddRow(idToUse, desc, userIDToUse, estChunks, compChunks, completeTime, createTime, updateTime, idToUse).
|
||||||
AddRow(idToUse+1, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
AddRow(idToUse+1, desc, userIDToUse, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \\$1$").
|
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||||
WithArgs(idToUse).
|
WithArgs(idToUse, userIDToUse).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)})
|
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)}, userIDToUse)
|
||||||
|
|
||||||
// test results
|
// test results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
@@ -116,6 +123,7 @@ func TestSelectActionsByPlanID(t *testing.T) {
|
|||||||
assert.Equal(createTime, *action.CreatedAt)
|
assert.Equal(createTime, *action.CreatedAt)
|
||||||
assert.Equal(updateTime, *action.UpdatedAt)
|
assert.Equal(updateTime, *action.UpdatedAt)
|
||||||
assert.Equal(idToUse, action.PlanID)
|
assert.Equal(idToUse, action.PlanID)
|
||||||
|
assert.EqualValues(userIDToUse, action.UserID)
|
||||||
|
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
t.Errorf("unfulfilled expectations: %s", err)
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
@@ -127,15 +135,16 @@ func TestSelectActionsByPlanIDErr(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
idToUse := 1
|
idToUse := 1
|
||||||
|
userIDToUse := 13
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \\$1$").
|
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = \$1 AND user_id = \$2$`).
|
||||||
WithArgs(idToUse).
|
WithArgs(idToUse, userIDToUse).
|
||||||
WillReturnError(fmt.Errorf("example error"))
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)})
|
actions, err := str.SelectActionsByPlanID(&models.Plan{PlanID: int64(idToUse)}, userIDToUse)
|
||||||
|
|
||||||
// test results
|
// test results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
@@ -156,6 +165,7 @@ func TestSelectActionById(t *testing.T) {
|
|||||||
idToUse := 1
|
idToUse := 1
|
||||||
estChunks := 5
|
estChunks := 5
|
||||||
compChunks := 7
|
compChunks := 7
|
||||||
|
userIDToUse := 13
|
||||||
desc := "Howdy, partner."
|
desc := "Howdy, partner."
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
@@ -163,20 +173,21 @@ func TestSelectActionById(t *testing.T) {
|
|||||||
rows := sqlmock.NewRows([]string{
|
rows := sqlmock.NewRows([]string{
|
||||||
"action_id",
|
"action_id",
|
||||||
"action_description",
|
"action_description",
|
||||||
|
"user_id",
|
||||||
"estimated_chunks",
|
"estimated_chunks",
|
||||||
"completed_chunks",
|
"completed_chunks",
|
||||||
"completed_on",
|
"completed_on",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at",
|
||||||
"plan_id"}).
|
"plan_id"}).
|
||||||
AddRow(idToUse, desc, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
AddRow(idToUse, desc, userIDToUse, estChunks, compChunks, completeTime, createTime, updateTime, idToUse)
|
||||||
|
|
||||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \\$1").
|
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \$1 AND user_id = \$2`).
|
||||||
WithArgs(1).
|
WithArgs(1, userIDToUse).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
action, err := str.SelectActionByID(1)
|
action, err := str.SelectActionByID(1, userIDToUse)
|
||||||
|
|
||||||
// test results
|
// test results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
@@ -189,6 +200,7 @@ func TestSelectActionById(t *testing.T) {
|
|||||||
assert.Equal(createTime, *action.CreatedAt)
|
assert.Equal(createTime, *action.CreatedAt)
|
||||||
assert.Equal(updateTime, *action.UpdatedAt)
|
assert.Equal(updateTime, *action.UpdatedAt)
|
||||||
assert.Equal(idToUse, action.PlanID)
|
assert.Equal(idToUse, action.PlanID)
|
||||||
|
assert.EqualValues(userIDToUse, action.UserID)
|
||||||
|
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
t.Errorf("unfulfilled expectations: %s", err)
|
t.Errorf("unfulfilled expectations: %s", err)
|
||||||
@@ -200,9 +212,9 @@ func TestErrActions(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions$").WillReturnError(fmt.Errorf("example error"))
|
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = \$1$`).WithArgs(1).WillReturnError(fmt.Errorf("example error"))
|
||||||
// function under test
|
// function under test
|
||||||
actions, err := str.SelectActions()
|
actions, err := str.SelectActions(1)
|
||||||
// test results
|
// test results
|
||||||
assert.Nil(actions)
|
assert.Nil(actions)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
@@ -216,9 +228,11 @@ func TestErrActionByID(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
|
|
||||||
mock.ExpectQuery("^SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \\$1").WillReturnError(fmt.Errorf("example error"))
|
userIDToUse := 3
|
||||||
|
|
||||||
|
mock.ExpectQuery(`^SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = \$1 AND user_id = \$2`).WithArgs(1, userIDToUse).WillReturnError(fmt.Errorf("example error"))
|
||||||
// function under test
|
// function under test
|
||||||
action, err := str.SelectActionByID(1)
|
action, err := str.SelectActionByID(1, userIDToUse)
|
||||||
// test results
|
// test results
|
||||||
assert.Nil(action)
|
assert.Nil(action)
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
@@ -244,6 +258,8 @@ func TestInsertAction(t *testing.T) {
|
|||||||
// setup
|
// setup
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
userIDToUse := 7
|
||||||
|
|
||||||
str, mock := getDbMock(t)
|
str, mock := getDbMock(t)
|
||||||
completedOn, _ := time.Parse("2006-01-02", "2021-01-01")
|
completedOn, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
action := &models.Action{
|
action := &models.Action{
|
||||||
@@ -259,13 +275,13 @@ func TestInsertAction(t *testing.T) {
|
|||||||
rows := sqlmock.NewRows([]string{"action_id"}).AddRow(8)
|
rows := sqlmock.NewRows([]string{"action_id"}).AddRow(8)
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
mock.ExpectQuery(`^INSERT INTO actions \(action_description, user_id, estimated_chunks, completed_chunks, completed_on, plan_id\) VALUES \(\$1, \$2, \$3, \$4, \$5, \$6\) RETURNING action_id$`).
|
||||||
WithArgs("testing", 3, 6, completedOn, 5).
|
WithArgs("testing", userIDToUse, 3, 6, completedOn, 5).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
mock.ExpectCommit()
|
mock.ExpectCommit()
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
insertedId, err := str.InsertAction(action)
|
insertedId, err := str.InsertAction(action, userIDToUse)
|
||||||
// check results
|
// check results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
assert.EqualValues(idToUse, insertedId)
|
assert.EqualValues(idToUse, insertedId)
|
||||||
@@ -290,13 +306,13 @@ func TestInsertActionErr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
mock.ExpectQuery(`^INSERT INTO actions \(action_description, user_id, estimated_chunks, completed_chunks, completed_on, plan_id\) VALUES \(\$1, \$2, \$3, \$4, \$5, \$6\) RETURNING action_id$`).
|
||||||
WithArgs("testing", 3, 6, completedOn, 5).
|
WithArgs("testing", 7, 3, 6, completedOn, 5).
|
||||||
WillReturnError(fmt.Errorf("example error"))
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
mock.ExpectRollback()
|
mock.ExpectRollback()
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
_, err := str.InsertAction(action)
|
_, err := str.InsertAction(action, 7)
|
||||||
// check results
|
// check results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@@ -319,17 +335,18 @@ func TestInsertActionCommitErr(t *testing.T) {
|
|||||||
ActionDescription: "testing",
|
ActionDescription: "testing",
|
||||||
}
|
}
|
||||||
idToUse := 8
|
idToUse := 8
|
||||||
|
userIDToUse := 11
|
||||||
|
|
||||||
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
rows := sqlmock.NewRows([]string{"plan_id"}).AddRow(idToUse)
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectQuery("^INSERT INTO actions \\(action_description, estimated_chunks, completed_chunks, completed_on, plan_id\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\) RETURNING action_id$").
|
mock.ExpectQuery(`^INSERT INTO actions \(action_description, user_id, estimated_chunks, completed_chunks, completed_on, plan_id\) VALUES \(\$1, \$2, \$3, \$4, \$5, \$6\) RETURNING action_id$`).
|
||||||
WithArgs("testing", 3, 6, completedOn, 5).
|
WithArgs("testing", userIDToUse, 3, 6, completedOn, 5).
|
||||||
WillReturnRows(rows)
|
WillReturnRows(rows)
|
||||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
_, err := str.InsertAction(action)
|
_, err := str.InsertAction(action, userIDToUse)
|
||||||
// check results
|
// check results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@@ -352,6 +369,7 @@ func TestUpdateAction(t *testing.T) {
|
|||||||
ActionDescription: "testing",
|
ActionDescription: "testing",
|
||||||
ActionID: 2,
|
ActionID: 2,
|
||||||
}
|
}
|
||||||
|
userIDToUse := 31
|
||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectExec(`
|
mock.ExpectExec(`
|
||||||
@@ -361,13 +379,14 @@ func TestUpdateAction(t *testing.T) {
|
|||||||
completed_chunks = \$3,
|
completed_chunks = \$3,
|
||||||
completed_on = \$4,
|
completed_on = \$4,
|
||||||
plan_id = \$5
|
plan_id = \$5
|
||||||
WHERE action_id = \$6`).
|
WHERE action_id = \$6
|
||||||
WithArgs("testing", 3, 6, completedOn, 5, 2).
|
AND user_id = \$7`).
|
||||||
|
WithArgs("testing", 3, 6, completedOn, 5, 2, userIDToUse).
|
||||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
mock.ExpectCommit()
|
mock.ExpectCommit()
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
err := str.UpdateAction(action)
|
err := str.UpdateAction(action, userIDToUse)
|
||||||
// check results
|
// check results
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@@ -393,12 +412,12 @@ func TestUpdateActionErr(t *testing.T) {
|
|||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectExec("UPDATE actions").
|
mock.ExpectExec("UPDATE actions").
|
||||||
WithArgs("testing", 3, 6, completedOn, 5, 2).
|
WithArgs("testing", 3, 6, completedOn, 5, 2, 31).
|
||||||
WillReturnError(fmt.Errorf("example error"))
|
WillReturnError(fmt.Errorf("example error"))
|
||||||
mock.ExpectRollback()
|
mock.ExpectRollback()
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
err := str.UpdateAction(action)
|
err := str.UpdateAction(action, 31)
|
||||||
// check results
|
// check results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
@@ -424,12 +443,12 @@ func TestUpdateActionCommitErr(t *testing.T) {
|
|||||||
|
|
||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
mock.ExpectExec("UPDATE actions").
|
mock.ExpectExec("UPDATE actions").
|
||||||
WithArgs("testing", 3, 6, completedOn, 5, 2).
|
WithArgs("testing", 3, 6, completedOn, 5, 2, 31).
|
||||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
mock.ExpectCommit().WillReturnError(fmt.Errorf("another error example"))
|
||||||
|
|
||||||
// function under test
|
// function under test
|
||||||
err := str.UpdateAction(action)
|
err := str.UpdateAction(action, 31)
|
||||||
// check results
|
// check results
|
||||||
assert.NotNil(err)
|
assert.NotNil(err)
|
||||||
if err := mock.ExpectationsWereMet(); err != nil {
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user