Skip to content

Commit

Permalink
Introduce a class to represent wireless auth modes
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 24, 2021
1 parent 486ea94 commit 1fb8ed9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib/y2network/wireless_auth_mode.rb
Expand Up @@ -20,6 +20,10 @@
require "yast"

module Y2Network
# This enum-like class represents the wireless authentication modes
#
# @todo Use this class whenever one of these symbols is used: :no_encryption, :open, :shared,
# :psk, :eap.
class WirelessAuthMode
extend Yast::I18n
include Yast::I18n
Expand All @@ -33,6 +37,14 @@ def all
.map { |c| WirelessAuthMode.const_get(c) }
.select { |c| c.is_a?(WirelessAuthMode) }
end

# Returns the auth mode with the given short name
#
# @param short_name [String] Short name
# @return [WirelessAuthMode,nil] Authentication mode or nil if not found
def from_short_name(short_name)
all.find { |t| t.short_name == short_name }
end
end

# @!attribute [r] name
Expand All @@ -44,7 +56,7 @@ def all
# Constructor
#
# @param name [String] Wireless mode name
# @param short_name [String] Wireles mode short name (e.g., "ad-hoc")
# @param short_name [String] Wireles mode short name (e.g., "none")
def initialize(name, short_name)
textdomain "network"
@name = name
Expand All @@ -58,7 +70,7 @@ def to_human_string
_(name)
end

NONE = new(N_("No Encryption"))
NONE = new(N_("No Encryption"), "none")
WEP_OPEN = new(N_("WEP - Open"), "open")
WEP_SHARED = new(N_("WEP - Shared Key"), "shared")
WPA_PSK = new(N_("WPA-PSK (\"home\")"), "psk")
Expand Down
44 changes: 44 additions & 0 deletions test/y2network/wireless_auth_mode_test.rb
@@ -0,0 +1,44 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"

require "y2network/wireless_auth_mode"

describe Y2Network::WirelessAuthMode do
subject(:auth_mode) { described_class.new("custom", "custom") }

describe ".all" do
it "returns all known auth modes" do
expect(described_class.all).to_not be_empty
expect(described_class.all.first).to be_a(described_class)
end
end

describe ".from_short_name" do
it "returns auth mode with the given name" do
expect(described_class.from_short_name("none"))
.to eq(Y2Network::WirelessAuthMode::NONE)
end

it "returns nil if the given name not found" do
expect(described_class.from_short_name("dummy")).to eq nil
end
end
end

0 comments on commit 1fb8ed9

Please sign in to comment.