// TestGetScripts validates retrieval of all Script records. func TestGetScripts(t *testing.T) { const fixture = "basic.json" scr1, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to retrieve a list of scripts.") { t.Log("\tWhen using two scripts") { if err := script.Upsert(tests.Context, db, scr1); err != nil { t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a script.", tests.Success) scr2 := scr1 scr2.Name += "2" if err := script.Upsert(tests.Context, db, scr2); err != nil { t.Fatalf("\t%s\tShould be able to create a second script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a second script.", tests.Success) scripts, err := script.GetAll(tests.Context, db, nil) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the scripts : %v", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the scripts", tests.Success) var count int for _, scr := range scripts { if len(scr.Name) > len(prefix) && scr.Name[0:len(prefix)] == prefix { count++ } } // When tests are running in parallel with the query and exec package, we could // have more scripts. if count < 2 { t.Fatalf("\t%s\tShould have at least two scripts : %d : %v", tests.Failed, len(scripts), scripts) } t.Logf("\t%s\tShould have at least two scripts.", tests.Success) var found int for _, s := range scripts { if s.Name == scr1.Name || s.Name == scr2.Name { found++ } } if found != 2 { t.Errorf("\t%s\tShould have retrieve the correct scripts : found[%d]", tests.Failed, found) } else { t.Logf("\t%s\tShould have retrieve the correct scripts.", tests.Success) } } } }
// TestAPIFailureScripts validates the failure of the api using a nil session. func TestAPIFailureScripts(t *testing.T) { const fixture = "basic.json" scr1, db := setup(t, fixture) defer teardown(t, db) scrName := prefix + "_unknown" t.Log("Given the need to validate failure of API with bad session.") { t.Log("When giving a nil session") { err := script.Upsert(tests.Context, nil, scr1) if err == nil { t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err) _, err = script.GetNames(tests.Context, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetAll(tests.Context, nil, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetByName(tests.Context, nil, scrName) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetByNames(tests.Context, nil, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetLastHistoryByName(tests.Context, nil, scrName) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) err = script.Delete(tests.Context, nil, scrName) if err == nil { t.Fatalf("\t%s\tShould be refused delete by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused delete by api with bad session: %s", tests.Success, err) } } }
// List returns all the existing scripts in the system. // 200 Success, 404 Not Found, 500 Internal func (scriptHandle) List(c *web.Context) error { scrs, err := script.GetAll(c.SessionID, c.Ctx["DB"].(*db.DB), nil) if err != nil { if err == script.ErrNotFound { err = web.ErrNotFound } return err } c.Respond(scrs, http.StatusOK) return nil }