github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ui/versionmanager/version_manager_test.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package versionmanager
    19  
    20  import (
    21  	"net/http"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  
    27  	godvpnweb "github.com/mysteriumnetwork/go-dvpn-web/v2"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  var ms = &mockServer{}
    33  
    34  type mockServer struct {
    35  	capturedPath string
    36  }
    37  
    38  func (m *mockServer) SwitchUI(path string) {
    39  	m.capturedPath = path
    40  }
    41  
    42  func TestSwitchUI(t *testing.T) {
    43  	// given
    44  	tmpDIr, err := os.MkdirTemp("", "nodeuiversiontest")
    45  	assert.NoError(t, err)
    46  	defer os.RemoveAll(tmpDIr)
    47  
    48  	err = os.Mkdir(tmpDIr+"/1.1.1", 0644)
    49  	assert.NoError(t, err)
    50  
    51  	config, err := NewVersionConfig(tmpDIr)
    52  	assert.NoError(t, err)
    53  
    54  	var nvm = &VersionManager{
    55  		uiServer:      ms,
    56  		versionConfig: config,
    57  	}
    58  
    59  	// when
    60  	usedVersion, err := nvm.UsedVersion()
    61  
    62  	// then
    63  	assert.NoError(t, err)
    64  	assert.Equal(t, BundledVersionName, usedVersion.Name)
    65  
    66  	// when
    67  	err = nvm.SwitchTo(BundledVersionName)
    68  
    69  	// then
    70  	assert.NoError(t, err)
    71  	assert.Equal(t, BundledVersionName, ms.capturedPath)
    72  
    73  	// when
    74  	err = nvm.SwitchTo("non existing")
    75  
    76  	// then
    77  	assert.Error(t, err)
    78  	assert.Equal(t, BundledVersionName, ms.capturedPath)
    79  
    80  	// when
    81  	err = nvm.SwitchTo("1.1.1")
    82  	assert.NoError(t, err)
    83  
    84  	usedVersion, err = nvm.UsedVersion()
    85  	assert.NoError(t, err)
    86  
    87  	// then
    88  	assert.Equal(t, tmpDIr+"/1.1.1/build", ms.capturedPath)
    89  	assert.Equal(t, "1.1.1", usedVersion.Name)
    90  }
    91  
    92  func TestListLocal(t *testing.T) {
    93  	// given
    94  	tmpDIr, err := os.MkdirTemp("", "nodeuiversiontest")
    95  	assert.NoError(t, err)
    96  	defer os.RemoveAll(tmpDIr)
    97  
    98  	config, err := NewVersionConfig(tmpDIr)
    99  	assert.NoError(t, err)
   100  
   101  	var nvm = &VersionManager{
   102  		uiServer:      ms,
   103  		versionConfig: config,
   104  	}
   105  
   106  	// when
   107  	versions, err := nvm.ListLocalVersions()
   108  	assert.NoError(t, err)
   109  
   110  	// then
   111  	assert.Len(t, versions, 0)
   112  
   113  	// when
   114  	err = os.Mkdir(tmpDIr+"/1.1.1", 0644)
   115  	assert.NoError(t, err)
   116  	err = os.Mkdir(tmpDIr+"/1.2.2", 0644)
   117  	assert.NoError(t, err)
   118  
   119  	versions, err = nvm.ListLocalVersions()
   120  
   121  	// then
   122  	for _, version := range []string{"1.1.1", "1.2.2"} {
   123  		assert.Contains(t, versions, LocalVersion{Name: version})
   124  	}
   125  }
   126  
   127  func TestBundledVersion(t *testing.T) {
   128  	// given
   129  	tmpDIr, err := os.MkdirTemp("", "nodeuiversiontest")
   130  	assert.NoError(t, err)
   131  	defer os.RemoveAll(tmpDIr)
   132  
   133  	bundledNodeUIVersion, err := godvpnweb.Version()
   134  	assert.NoError(t, err)
   135  
   136  	config, err := NewVersionConfig(tmpDIr)
   137  	assert.NoError(t, err)
   138  
   139  	var nvm = &VersionManager{
   140  		uiServer:      ms,
   141  		versionConfig: config,
   142  	}
   143  
   144  	// when
   145  	version, err := nvm.BundledVersion()
   146  
   147  	// then
   148  	assert.NoError(t, err)
   149  	assert.Equal(t, bundledNodeUIVersion, version.Name)
   150  }
   151  
   152  func TestListLocalDirectoryRemoved(t *testing.T) {
   153  	// given
   154  	tmpDIr, err := os.MkdirTemp("", "nodeuiversiontest")
   155  	assert.NoError(t, err)
   156  	defer os.RemoveAll(tmpDIr)
   157  
   158  	config, err := NewVersionConfig(tmpDIr)
   159  	assert.NoError(t, err)
   160  
   161  	var nvm = &VersionManager{
   162  		uiServer:      ms,
   163  		versionConfig: config,
   164  	}
   165  
   166  	// when
   167  	err = os.RemoveAll(tmpDIr)
   168  	assert.NoError(t, err)
   169  
   170  	versions, err := nvm.ListLocalVersions()
   171  
   172  	// then
   173  	assert.Len(t, versions, 0)
   174  	assert.NoError(t, err)
   175  }
   176  
   177  func TestListRemoteVersions(t *testing.T) {
   178  	// given
   179  	rMap := map[string]*http.Response{
   180  		"/repos/mysteriumnetwork/dvpn-web/releases": {StatusCode: 200, Status: "200 OK", Body: &readCloser{Reader: strings.NewReader(releasesJSON)}},
   181  	}
   182  	httpMock := newHttpClientMock(rMap)
   183  
   184  	var nvm = &VersionManager{
   185  		uiServer: ms,
   186  		github:   newGithub(httpMock),
   187  	}
   188  
   189  	// expect
   190  	assert.Empty(t, nvm.releasesCache)
   191  
   192  	// when
   193  	versionsBeforeFlush, err := nvm.ListRemoteVersions(RemoteVersionRequest{})
   194  	assert.NoError(t, err)
   195  
   196  	// then
   197  	assert.NotEmpty(t, nvm.releasesCache)
   198  	assert.Equal(t, len(versionsBeforeFlush), len(nvm.releasesCache))
   199  
   200  	// when
   201  	httpMock.stub(map[string]*http.Response{
   202  		"/repos/mysteriumnetwork/dvpn-web/releases": {StatusCode: 200, Status: "200 OK", Body: &readCloser{Reader: strings.NewReader(singleReleaseJSON)}},
   203  	})
   204  	versionsBeforeFlush, err = nvm.ListRemoteVersions(RemoteVersionRequest{})
   205  	assert.NoError(t, err)
   206  
   207  	// then
   208  	assert.NotEmpty(t, nvm.releasesCache)
   209  	assert.Equal(t, len(versionsBeforeFlush), len(nvm.releasesCache))
   210  
   211  	// when
   212  	versionsFlushed, err := nvm.ListRemoteVersions(RemoteVersionRequest{FlushCache: true})
   213  	assert.NoError(t, err)
   214  	assert.NotEqual(t, len(versionsBeforeFlush), len(nvm.releasesCache))
   215  	assert.Len(t, nvm.releasesCache, len(versionsFlushed))
   216  
   217  	// and
   218  	first := versionsFlushed[0]
   219  	assert.Equal(t, "Release Notes", first.ReleaseNotes)
   220  	assert.Equal(t, "1.0.1", first.Name)
   221  
   222  	expectedTime, err := time.Parse("2006-01-02T15:04:05Z", "2021-11-22T13:17:45Z")
   223  	assert.NoError(t, err)
   224  
   225  	assert.Equal(t, expectedTime, first.PublishedAt)
   226  	assert.False(t, first.IsPreRelease)
   227  }
   228  
   229  var singleReleaseJSON = `[
   230  {
   231  		"url": "https://api.github.com/repos/mysteriumnetwork/dvpn-web/releases/53839105",
   232  		"assets_url": "https://api.github.com/repos/mysteriumnetwork/dvpn-web/releases/53839105/assets",
   233  		"upload_url": "https://uploads.github.com/repos/mysteriumnetwork/dvpn-web/releases/53839105/assets{?name,label}",
   234  		"html_url": "https://github.com/mysteriumnetwork/dvpn-web/releases/tag/1.0.1",
   235  		"id": 53839105,
   236  		"author": {
   237  			"login": "mdomasevicius",
   238  			"id": 20511486,
   239  			"node_id": "MDQ6VXNlcjIwNTExNDg2",
   240  			"avatar_url": "https://avatars.githubusercontent.com/u/20511486?v=4",
   241  			"gravatar_id": "",
   242  			"url": "https://api.github.com/users/mdomasevicius",
   243  			"html_url": "https://github.com/mdomasevicius",
   244  			"followers_url": "https://api.github.com/users/mdomasevicius/followers",
   245  			"following_url": "https://api.github.com/users/mdomasevicius/following{/other_user}",
   246  			"gists_url": "https://api.github.com/users/mdomasevicius/gists{/gist_id}",
   247  			"starred_url": "https://api.github.com/users/mdomasevicius/starred{/owner}{/repo}",
   248  			"subscriptions_url": "https://api.github.com/users/mdomasevicius/subscriptions",
   249  			"organizations_url": "https://api.github.com/users/mdomasevicius/orgs",
   250  			"repos_url": "https://api.github.com/users/mdomasevicius/repos",
   251  			"events_url": "https://api.github.com/users/mdomasevicius/events{/privacy}",
   252  			"received_events_url": "https://api.github.com/users/mdomasevicius/received_events",
   253  			"type": "User",
   254  			"site_admin": false
   255  		},
   256  		"node_id": "RE_kwDOCwMSYc4DNYUB",
   257  		"tag_name": "1.0.1",
   258  		"target_commitish": "mainnet",
   259  		"name": "1.0.1",
   260  		"draft": false,
   261  		"prerelease": false,
   262  		"created_at": "2021-11-22T13:16:34Z",
   263  		"published_at": "2021-11-22T13:17:45Z",
   264  		"assets": [
   265  			{
   266  				"url": "https://api.github.com/repos/mysteriumnetwork/dvpn-web/releases/assets/50043500",
   267  				"id": 50043500,
   268  				"node_id": "RA_kwDOCwMSYc4C-5ps",
   269  				"name": "dist.tar.gz",
   270  				"label": "",
   271  				"uploader": {
   272  					"login": "MysteriumTeam",
   273  					"id": 42934344,
   274  					"node_id": "MDQ6VXNlcjQyOTM0MzQ0",
   275  					"avatar_url": "https://avatars.githubusercontent.com/u/42934344?v=4",
   276  					"gravatar_id": "",
   277  					"url": "https://api.github.com/users/MysteriumTeam",
   278  					"html_url": "https://github.com/MysteriumTeam",
   279  					"followers_url": "https://api.github.com/users/MysteriumTeam/followers",
   280  					"following_url": "https://api.github.com/users/MysteriumTeam/following{/other_user}",
   281  					"gists_url": "https://api.github.com/users/MysteriumTeam/gists{/gist_id}",
   282  					"starred_url": "https://api.github.com/users/MysteriumTeam/starred{/owner}{/repo}",
   283  					"subscriptions_url": "https://api.github.com/users/MysteriumTeam/subscriptions",
   284  					"organizations_url": "https://api.github.com/users/MysteriumTeam/orgs",
   285  					"repos_url": "https://api.github.com/users/MysteriumTeam/repos",
   286  					"events_url": "https://api.github.com/users/MysteriumTeam/events{/privacy}",
   287  					"received_events_url": "https://api.github.com/users/MysteriumTeam/received_events",
   288  					"type": "User",
   289  					"site_admin": false
   290  				},
   291  				"content_type": "application/octet-stream",
   292  				"state": "uploaded",
   293  				"size": 1515748,
   294  				"download_count": 3,
   295  				"created_at": "2021-11-22T13:26:23Z",
   296  				"updated_at": "2021-11-22T13:26:23Z",
   297  				"browser_download_url": "https://github.com/mysteriumnetwork/dvpn-web/releases/download/1.0.1/dist.tar.gz"
   298  			}
   299  		],
   300  		"tarball_url": "https://api.github.com/repos/mysteriumnetwork/dvpn-web/tarball/1.0.1",
   301  		"zipball_url": "https://api.github.com/repos/mysteriumnetwork/dvpn-web/zipball/1.0.1",
   302  		"body": "Release Notes", 
   303  		"mentions_count": 3
   304  	}
   305  ]
   306  `