Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(secrets): cypress tests for secrets #161

Merged
merged 9 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions cypress/fixtures/secrets_org_5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"id": 513,
"org": "github",
"repo": "*",
"team": "",
"name": "docker_username",
"value": "[secure]",
"type": "org",
"images": [],
"events": ["push"],
"allow_command": true
},
{
"id": 511,
"org": "github",
"repo": "*",
"team": "",
"name": "docker_password",
"value": "[secure]",
"type": "org",
"images": [],
"events": ["push"],
"allow_command": true
},
{
"id": 509,
"org": "github",
"repo": "*",
"team": "",
"name": "push",
"value": "[secure]",
"type": "org",
"images": [],
"events": ["push"],
"allow_command": true
},
{
"id": 511,
"org": "github",
"repo": "*",
"team": "",
"name": "pull_request",
"value": "[secure]",
"type": "org",
"images": [],
"events": ["push", "pull_request"],
"allow_command": true
},
{
"id": 509,
"org": "github",
"repo": "*",
"team": "",
"name": "deployment",
"value": "[secure]",
"type": "org",
"images": [],
"events": ["push", "pull_request", "comment", "deployment"],
"allow_command": true
}
]
4 changes: 2 additions & 2 deletions cypress/integration/add_repositories.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ context('Add Repositories', () => {
it('show a message and an alert when there is a server error', () => {
cy.wait('@error');
cy.get('.content-wrap').contains(
'There was an error fetching your available repositories... Click Refresh or try again later!',
'There was an error fetching your available repositories, please refresh or try again later!',
);
});
});
Expand All @@ -127,7 +127,7 @@ context('Add Repositories', () => {

it('show a message and an alert when the response is malformed', () => {
cy.get('.content-wrap').contains(
'There was an error fetching your available repositories... Click Refresh or try again later!',
'There was an error fetching your available repositories, please refresh or try again later!',
);

cy.get('[data-test=alerts]')
Expand Down
84 changes: 84 additions & 0 deletions cypress/integration/secrets.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2020 Target Brands, Inc. All rights reserved.
* Use of this source code is governed by the LICENSE file in this repository.
*/

context('Secrets', () => {
context('server returning secrets error', () => {
beforeEach(() => {
cy.server();
cy.route({
method: 'GET',
url: '*api/v1/secrets/native/org/github/**',
status: 500,
response: 'server error',
});
cy.login('/-/secrets/native/org/github');
});

it('secrets table should not show', () => {
cy.get('[data-test=secrets]').should('not.be.visible');
});
it('error should show', () => {
cy.get('[data-test=alerts]')
.should('exist')
.contains('Error');
});
it('error banner should show', () => {
cy.get('[data-test=secrets-error]')
.should('exist')
.contains('try again later');
});
});
context('server returning 5 secrets', () => {
beforeEach(() => {
cy.server();
cy.route(
'GET',
'*api/v1/secrets/native/org/github/**',
'fixture:secrets_org_5.json',
).as('secrets');
cy.login('/-/secrets/native/org/github');
});

it('secrets table should show', () => {
cy.get('[data-test=secrets-table]').should('be.visible');
});

it('secrets table should show 5 secrets', () => {
cy.get('[data-test=secrets-row]').should('have.length', 5);
});

it('pagination controls should not show', () => {
cy.get('[data-test=pager-previous]').should('not.be.visible');
});

context('secret', () => {
beforeEach(() => {
cy.get('[data-test=secrets-row]')
.first()
.as('firstSecret');
cy.get('[data-test=secrets-row]')
.last()
.as('lastSecret');
});
it('should show name', () => {
cy.get('@firstSecret').within(() => {
cy.get('[data-test=secrets-row-name]').contains('docker_username');
});
cy.get('@lastSecret').within(() => {
cy.get('[data-test=secrets-row-name]').contains('deployment');
});
});
it('clicking name should route to edit secret page', () => {
cy.get('@firstSecret').within(() => {
cy.get('[data-test=secrets-row-name]').click({ force: true });
cy.location('pathname').should(
'eq',
'/-/secrets/native/org/github/docker_username',
);
});
});
});
});
});
18 changes: 17 additions & 1 deletion src/elm/Errors.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ Use of this source code is governed by the LICENSE file in this repository.
--}


module Errors exposing (detailedErrorToError, detailedErrorToString)
module Errors exposing (detailedErrorToError, detailedErrorToString, viewResourceError)

import Html exposing (Html, div, p, text)
import Http exposing (Error(..))
import Http.Detailed
import Json.Decode as Decode
import Util


{-| errorDecoder : decodes error field from json
Expand Down Expand Up @@ -89,3 +91,17 @@ wrapErrorContent content =

else
" (" ++ content ++ ")"


{-| viewResourceError : renders generic error message when there is a problem fetching a resource from Vela.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper function for rendering GET failures

-}
viewResourceError : { resourceLabel : String, testLabel : String } -> Html msg
viewResourceError { resourceLabel, testLabel } =
div [ Util.testAttribute <| testLabel ++ "-error" ]
[ p []
[ text <|
"There was an error fetching "
++ resourceLabel
++ ", please refresh or try again later!"
]
]
44 changes: 21 additions & 23 deletions src/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -761,29 +761,6 @@ update msg model =
Err error ->
( model, addError error )

SecretsResponse response ->
case response of
Ok ( meta, secrets ) ->
let
secretsModel =
model.secretsModel

mergedSecrets =
case secretsModel.secrets of
Success s ->
RemoteData.succeed <| Util.mergeListsById s secrets

_ ->
RemoteData.succeed secrets

pager =
Pagination.get meta.headers
in
( { model | secretsModel = { secretsModel | secrets = mergedSecrets, pager = pager } }, Cmd.none )

Err error ->
( model, addError error )

SecretResponse response ->
case response of
Ok ( _, secret ) ->
Expand Down Expand Up @@ -837,6 +814,27 @@ update msg model =
Err error ->
( model, addError error )

SecretsResponse response ->
let
secretsModel =
model.secretsModel
in
case response of
Ok ( _, secrets ) ->
let
mergedSecrets =
case secretsModel.secrets of
Success s ->
RemoteData.succeed <| Util.mergeListsById s secrets

_ ->
RemoteData.succeed secrets
in
( { model | secretsModel = { secretsModel | secrets = mergedSecrets } }, Cmd.none )

Err error ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added account for GET failure

( { model | secretsModel = { secretsModel | secrets = toFailure error } }, addError error )

UpdateRepoEvent org repo field value ->
let
payload : UpdateRepositoryPayload
Expand Down
8 changes: 2 additions & 6 deletions src/elm/Pages/AddRepos.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Use of this source code is governed by the LICENSE file in this repository.
module Pages.AddRepos exposing (Msgs, PartialModel, view)

import Dict
import Errors exposing (viewResourceError)
import Favorites
exposing
( ToggleFavorite
Expand Down Expand Up @@ -121,12 +122,7 @@ view model actions =
loading

RemoteData.Failure _ ->
div []
[ p []
[ text <|
"There was an error fetching your available repositories... Click Refresh or try again later!"
]
]
viewResourceError { resourceLabel = "your available repositories", testLabel = "repos" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a helper class for the "try again later!" we test for



{-| viewSourceRepos : takes model and source repos and renders them based on user search
Expand Down
8 changes: 2 additions & 6 deletions src/elm/Pages/Builds.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Use of this source code is governed by the LICENSE file in this repository.

module Pages.Builds exposing (view)

import Errors exposing (viewResourceError)
import Html
exposing
( Html
Expand Down Expand Up @@ -85,9 +86,4 @@ view buildsModel now org repo maybeEvent =
largeLoader

RemoteData.Failure _ ->
div [ Util.testAttribute "builds-error" ]
[ p []
[ text <|
"There was an error fetching builds for this repository, please try again later!"
]
]
viewResourceError { resourceLabel = "builds for this repository", testLabel = "builds" }
8 changes: 2 additions & 6 deletions src/elm/Pages/Hooks.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Use of this source code is governed by the LICENSE file in this repository.
module Pages.Hooks exposing (hookStatus, receiveHookBuild, view)

import Dict
import Errors exposing (viewResourceError)
import FeatherIcons
import Html
exposing
Expand Down Expand Up @@ -82,12 +83,7 @@ view { hooks, hookBuilds, time } org repo clickAction =
Util.largeLoader

RemoteData.Failure _ ->
div [ Util.testAttribute "hooks-error" ]
[ p []
[ text <|
"There was an error fetching hooks for this repository, please try again later!"
]
]
viewResourceError { resourceLabel = "hooks for this repository", testLabel = "hooks" }


{-| hooksTable : renders hooks table
Expand Down
8 changes: 2 additions & 6 deletions src/elm/Pages/RepoSettings.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Pages.RepoSettings exposing
)

import Dict exposing (Dict)
import Errors exposing (viewResourceError)
import FeatherIcons
import Html
exposing
Expand Down Expand Up @@ -163,12 +164,7 @@ view repo inTimeout actions velaAPI velaURL =
]

Failure _ ->
div []
[ p []
[ text <|
"There was an error fetching your repo settings... Click Refresh or try again later!"
]
]
viewResourceError { resourceLabel = "your repo settings", testLabel = "settings" }


{-| access : takes model and repo and renders the settings category for updating repo access
Expand Down
19 changes: 19 additions & 0 deletions src/elm/Pages/Secrets/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Pages.Secrets.Model exposing
, SecretsResponse
, UpdateSecretResponse
, defaultSecretUpdate
, secretsResourceKey
)

import Http
Expand Down Expand Up @@ -58,6 +59,7 @@ type alias Model msg =
, engine : Engine
, type_ : SecretType
, secrets : WebData Secrets
, secret : WebData Secret
, form : SecretForm
, secretResponse : SecretResponse msg
, secretsResponse : SecretsResponse msg
Expand All @@ -67,6 +69,23 @@ type alias Model msg =
}



{- secretsResourceKey : takes Model returns maybe string for retrieving secrets based on type -}


secretsResourceKey : Model msg -> Maybe String
secretsResourceKey secretsModel =
case secretsModel.type_ of
Vela.OrgSecret ->
Nothing

Vela.RepoSecret ->
Just secretsModel.repo

Vela.SharedSecret ->
Just secretsModel.team


{-| SecretForm : record to hold potential add/update secret fields
-}
type alias SecretForm =
Expand Down
Loading