Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Write E2E test including api call
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Mar 15, 2024
1 parent d6a3eb8 commit f641ecb
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ on:
- '**.go'
- 'go.*'
- 'testdata/**'
workflow_dispatch:

jobs:
build:
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/ci-googleapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 📡

on:
# This test actually send request to Google API, so do not add frequently triggers as push/PR
schedule:
# Every 10:42 JST
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
- cron: '42 1 * * *'
workflow_dispatch:

jobs:
test-actual-api:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache-dependency-path: 'go.sum'
- run: go test -tags=apitest ./...
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"command": ["nixpkgs-fmt"]
}
}
},
"gopls": {
"build.buildFlags": ["-tags=apitest"]
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gwurl - "Google distributing Windows installer"'s URL

[![CI - Go Status](https://github.com/kachick/gwurl/actions/workflows/ci-go.yml/badge.svg?branch=main)](https://github.com/kachick/gwurl/actions/workflows/ci-go.yml?query=branch%3Amain+)
[![CI - E2E Status](https://github.com/kachick/gwurl/actions/workflows/ci-googleapi.yml/badge.svg?branch=main)](https://github.com/kachick/gwurl/actions/workflows/ci-googleapi.yml?query=branch%3Amain+)
[![CI - Nix Status](https://github.com/kachick/gwurl/actions/workflows/ci-nix.yml/badge.svg?branch=main)](https://github.com/kachick/gwurl/actions/workflows/ci-nix.yml?query=branch%3Amain+)

## Usage
Expand Down
3 changes: 3 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ tasks:
test:
cmds:
- go test ./...
test-all:
cmds:
- go test -tags=apitest ./...
fmt:
cmds:
- dprint fmt
Expand Down
22 changes: 22 additions & 0 deletions internal/googleapi/googleapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"slices"

"golang.org/x/xerrors"
)
Expand Down Expand Up @@ -85,3 +87,23 @@ func PostGoogleAPI(apiOs GoogleApiOs, apiApp GoogleApiApp) (Response, error) {

return responseObject, nil
}

func GetPermalinks(resp Response) ([]string, error) {
installerActionIdx := slices.IndexFunc(resp.App.UpdateCheck.Manifest.Actions, func(a Action) bool {
return a.Event == "install"
})
if installerActionIdx < 0 {
return nil, xerrors.Errorf("api didn't return installer information: %v", installerActionIdx)
}
installerFilename := resp.App.UpdateCheck.Manifest.Actions[installerActionIdx].Run

permalinks := make([]string, 0, len(resp.App.UpdateCheck.Urls))
for _, u := range resp.App.UpdateCheck.Urls {
permalink, err := url.JoinPath(u.Codebase, installerFilename)
if err != nil {
return nil, xerrors.Errorf("Cannot build final link with the result: %w", err)
}
permalinks = append(permalinks, permalink)
}
return permalinks, nil
}
104 changes: 104 additions & 0 deletions internal/googleapi/googleapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//go:build apitest
// +build apitest

package googleapi

import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestPostGoogleAPI(t *testing.T) {
testCases := []struct {
description string
input GoogleApiApp
want []string
ok bool
}{
{
description: "Google Japanese IME",
input: GoogleApiApp{
Appid: "{DDCCD2A9-025E-4142-BCEB-F467B88CF830}",
Ap: "external-stable-universal",
},
want: []string{
"http://edgedl.me.gvt1.com/edgedl/release2/kjspmop3m4hu2sbbaotsynsgja_2.29.5370.0/GoogleJapaneseInput64-2.29.5370.0.msi",
"https://edgedl.me.gvt1.com/edgedl/release2/kjspmop3m4hu2sbbaotsynsgja_2.29.5370.0/GoogleJapaneseInput64-2.29.5370.0.msi",
"http://dl.google.com/release2/kjspmop3m4hu2sbbaotsynsgja_2.29.5370.0/GoogleJapaneseInput64-2.29.5370.0.msi",
"https://dl.google.com/release2/kjspmop3m4hu2sbbaotsynsgja_2.29.5370.0/GoogleJapaneseInput64-2.29.5370.0.msi",
"http://www.google.com/dl/release2/kjspmop3m4hu2sbbaotsynsgja_2.29.5370.0/GoogleJapaneseInput64-2.29.5370.0.msi",
"https://www.google.com/dl/release2/kjspmop3m4hu2sbbaotsynsgja_2.29.5370.0/GoogleJapaneseInput64-2.29.5370.0.msi",
},
ok: true,
},
{
description: "Google Chrome",
input: GoogleApiApp{
Appid: "{8A69D345-D564-463C-AFF1-A69D9E530F96}",
Ap: "x64-stable-statsdef_1",
},
want: []string{
"http://edgedl.me.gvt1.com/edgedl/release2/chrome/adno2uyj7yhsdmrqsizskbq3um2q_122.0.6261.129/122.0.6261.129_chrome_installer.exe",
"https://edgedl.me.gvt1.com/edgedl/release2/chrome/adno2uyj7yhsdmrqsizskbq3um2q_122.0.6261.129/122.0.6261.129_chrome_installer.exe",
"http://dl.google.com/release2/chrome/adno2uyj7yhsdmrqsizskbq3um2q_122.0.6261.129/122.0.6261.129_chrome_installer.exe",
"https://dl.google.com/release2/chrome/adno2uyj7yhsdmrqsizskbq3um2q_122.0.6261.129/122.0.6261.129_chrome_installer.exe",
"http://www.google.com/dl/release2/chrome/adno2uyj7yhsdmrqsizskbq3um2q_122.0.6261.129/122.0.6261.129_chrome_installer.exe",
"https://www.google.com/dl/release2/chrome/adno2uyj7yhsdmrqsizskbq3um2q_122.0.6261.129/122.0.6261.129_chrome_installer.exe",
},
ok: true,
},
{
description: "Unknown Prams",
input: GoogleApiApp{
Appid: "foo",
Ap: "bar",
},
want: nil,
ok: false,
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
resp, err := PostGoogleAPI(GoogleApiOs{
Platform: "win",
Version: "10",
Architecture: "x64",
}, tc.input)
if err != nil {
if tc.ok {
t.Errorf("unexpected error happned: %v", err)
return
} else {
return
}
}

urls, err := GetPermalinks(resp)
if err != nil {
if tc.ok {
t.Errorf("unexpected error happned: %v", err)
return
} else {
return
}
}

if !tc.ok {
t.Errorf("expected error did not happen")
return
}

dictComp := func(a string, b string) bool {
return strings.Compare(a, b) == -1
}

if diff := cmp.Diff(tc.want, urls, cmpopts.SortSlices(dictComp)); diff != "" {
t.Errorf("wrong result: %s", diff)
}
})
}
}

0 comments on commit f641ecb

Please sign in to comment.